您好,登錄后才能下訂單哦!
本篇簡(jiǎn)單介紹一下,Unity中MVC框架的簡(jiǎn)單應(yīng)用。MVC自1982年被設(shè)計(jì)出來(lái),至今都有著很大比重的使用率,特別是目前軟件及游戲迭代此之快的情況下。高效的增刪功能低耦合又小巧靈活的框架MVC,深受廣大ProgramDesigner的喜愛(ài)。
數(shù)據(jù)是程序的靈魂,視圖是看清靈魂的眼睛,控制器從中撥開(kāi)迷霧,這就形成了現(xiàn)在的MVC。
簡(jiǎn)單實(shí)例:Model
using UnityEngine;
using System.Collections;
/// <summary>
/// 模型委托(當(dāng)用戶(hù)信息發(fā)生變化時(shí)執(zhí)行)
/// </summary>
public delegate void OnValueChange (int val);
public class PlayerMsgModel
{
//玩家等級(jí)
private int playerLevel;
//玩家經(jīng)驗(yàn)
private int playerExperience;
//玩家升級(jí)經(jīng)驗(yàn)
private int playerFullExperience;
//金幣數(shù)量
private int goldNum;
//聲明委托對(duì)象,接收當(dāng)?shù)燃?jí)發(fā)生變化時(shí),觸發(fā)的事件
public OnValueChange OnLevelChange;
//聲明委托對(duì)象,接收當(dāng)經(jīng)驗(yàn)發(fā)生變化時(shí),觸發(fā)的事件
public OnValueChange OnExperienceChange;
//聲明委托對(duì)象,接收當(dāng)升級(jí)經(jīng)驗(yàn)發(fā)生變化時(shí),觸發(fā)的事件
public OnValueChange OnFullExperienceChange;
//聲明委托對(duì)象,接收當(dāng)金幣數(shù)量發(fā)生變化時(shí),觸發(fā)的事件
public OnValueChange OnGoldNumChange;
//單例
private static PlayerMsgModel mod;
public static PlayerMsgModel GetMod ()
{
if (mod == null) {
mod = new PlayerMsgModel ();
}
return mod;
}
private PlayerMsgModel ()
{
}
/// <summary>
/// 玩家等級(jí)屬性
/// </summary>
/// <value>The player level.</value>
public int PlayerLevel {
get {
return playerLevel;
}
set {
playerLevel = value;
//如果委托對(duì)象不為空
if (OnLevelChange != null) {
//執(zhí)行委托
OnLevelChange (playerLevel);
}
}
}
/// <summary>
/// 玩家經(jīng)驗(yàn)屬性
/// </summary>
/// <value>The player experience.</value>
public int PlayerExperience {
get {
return playerExperience;
}
set {
playerExperience = value;
if (OnExperienceChange != null) {
OnExperienceChange (playerExperience);
}
}
}
/// <summary>
/// 玩家升級(jí)經(jīng)驗(yàn)屬性
/// </summary>
/// <value>The player full experience.</value>
public int PlayerFullExperience {
get {
return playerFullExperience;
}
set {
playerFullExperience = value;
if (OnFullExperienceChange != null) {
OnFullExperienceChange (playerFullExperience);
}
}
}
/// <summary>
/// 金幣數(shù)量屬性
/// </summary>
/// <value>The gold number.</value>
public int GoldNum {
get {
return goldNum;
}
set {
goldNum = value;
if (OnGoldNumChange != null) {
OnGoldNumChange (goldNum);
}
}
}
}
簡(jiǎn)單實(shí)例:View
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerMsgView : MonoBehaviour
{
//UI
public Text playerLevel;
public Text playerExperience;
public Text goldNum;
public Button experienceUpButton;
void Start ()
{
//委托事件綁定
PlayerMsgModel.GetMod ().OnLevelChange += SetLevel;
//委托事件綁定
PlayerMsgModel.GetMod ().OnExperienceChange += SetExperience;
PlayerMsgModel.GetMod ().OnFullExperienceChange += SetFullExperience;
PlayerMsgModel.GetMod ().OnGoldNumChange += SetGoldNum;
//View綁定按鈕控制功能
experienceUpButton.onClick.AddListener (
PlayerMsgController.controller.OnExperienceUpButtonClick);
}
//修改UILevel值
public void SetLevel (int level)
{
playerLevel.text = level.ToString ();
}
//修改UI經(jīng)驗(yàn)值
public void SetExperience (int experience)
{
//將字符串以“/”拆開(kāi)
string[] str = playerExperience.text.Split (new char []{ '/' });
//用新的經(jīng)驗(yàn)值重組
playerExperience.text = experience + "/" + str [1];
}
public void SetFullExperience (int fullExiperience)
{
string[] str = playerExperience.text.Split (new char []{ '/' });
playerExperience.text = str [0] + "/" + fullExiperience;
}
public void SetGoldNum (int goldn)
{
goldNum.text = goldn.ToString ();
}
}
簡(jiǎn)單實(shí)例:Controller
using UnityEngine;
using System.Collections;
public class PlayerMsgController : MonoBehaviour
{
public static PlayerMsgController controller;
private int levelUpValue = 20;
void Awake ()
{
controller = this;
}
void Start ()
{
PlayerMsgModel.GetMod ().PlayerLevel = 1;
PlayerMsgModel.GetMod ().PlayerExperience = 0;
PlayerMsgModel.GetMod ().PlayerFullExperience = 100;
PlayerMsgModel.GetMod ().GoldNum = 0;
}
/// <summary>
/// 提升經(jīng)驗(yàn)按鈕點(diǎn)擊事件
/// </summary>
public void OnExperienceUpButtonClick ()
{
PlayerMsgModel.GetMod ().PlayerExperience += levelUpValue;
if (PlayerMsgModel.GetMod ().PlayerExperience
>= PlayerMsgModel.GetMod ().PlayerFullExperience) {
PlayerMsgModel.GetMod ().PlayerLevel += 1;
PlayerMsgModel.GetMod ().PlayerFullExperience +=
200 * PlayerMsgModel.GetMod ().PlayerLevel;
levelUpValue += 20;
if (PlayerMsgModel.GetMod ().PlayerLevel % 3 == 0) {
PlayerMsgModel.GetMod ().GoldNum +=
100 * PlayerMsgModel.GetMod ().PlayerLevel;
}
}
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。