亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Unity中MVC的簡單應用

發布時間:2020-05-08 17:25:24 來源:網絡 閱讀:1231 作者:寧金峰 欄目:建站服務器

本篇簡單介紹一下,UnityMVC框架的簡單應用。MVC1982年被設計出來,至今都有著很大比重的使用率,特別是目前軟件及游戲迭代此之快的情況下。高效的增刪功能低耦合又小巧靈活的框架MVC,深受廣大ProgramDesigner的喜愛。

Unity中MVC的簡單應用

數據是程序的靈魂,視圖是看清靈魂的眼睛,控制器從中撥開迷霧,這就形成了現在的MVC。

Unity中MVC的簡單應用

簡單實例:Model

using UnityEngine;

using System.Collections;

 

/// <summary>

/// 模型委托(當用戶信息發生變化時執行)

/// </summary>

public delegate void OnValueChange (int val);

 

public class PlayerMsgModel

{

    //玩家等級

    private int playerLevel;

    //玩家經驗

    private int playerExperience;

    //玩家升級經驗

    private int playerFullExperience;

    //金幣數量

    private int goldNum;

    //聲明委托對象,接收當等級發生變化時,觸發的事件

    public OnValueChange OnLevelChange;

    //聲明委托對象,接收當經驗發生變化時,觸發的事件

    public OnValueChange OnExperienceChange;

    //聲明委托對象,接收當升級經驗發生變化時,觸發的事件

    public OnValueChange OnFullExperienceChange;

    //聲明委托對象,接收當金幣數量發生變化時,觸發的事件

    public OnValueChange OnGoldNumChange;

 

    //單例

    private static PlayerMsgModel mod;

 

    public static PlayerMsgModel GetMod ()

    {

        if (mod == null) {

            mod = new PlayerMsgModel ();

        }

        return mod;

    }

 

    private PlayerMsgModel ()

    {

    }

 

    /// <summary>

    /// 玩家等級屬性

    /// </summary>

    /// <value>The player level.</value>

    public int PlayerLevel {

        get {

            return playerLevel;

        }

        set {

            playerLevel = value;

            //如果委托對象不為空

            if (OnLevelChange != null) {

                //執行委托

                OnLevelChange (playerLevel);

            }

        }

    }

 

    /// <summary>

    /// 玩家經驗屬性

    /// </summary>

    /// <value>The player experience.</value>

    public int PlayerExperience {

        get {

            return playerExperience;

        }

        set {

            playerExperience = value;

            if (OnExperienceChange != null) {

                OnExperienceChange (playerExperience);

            }

        }

    }

 

    /// <summary>

    /// 玩家升級經驗屬性

    /// </summary>

    /// <value>The player full experience.</value>

    public int PlayerFullExperience {

        get {

            return playerFullExperience;

        }

        set {

            playerFullExperience = value;

            if (OnFullExperienceChange != null) {

                OnFullExperienceChange (playerFullExperience);

            }

        }

    }

 

    /// <summary>

    /// 金幣數量屬性

    /// </summary>

    /// <value>The gold number.</value>

    public int GoldNum {

        get {

            return goldNum;

        }

        set {

            goldNum = value;

            if (OnGoldNumChange != null) {

                OnGoldNumChange (goldNum);

            }

        }

    }

}

簡單實例: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經驗值

    public void SetExperience (int experience)

    {

        //將字符串以“/”拆開

        string[] str = playerExperience.text.Split (new char []{ '/' });

        //用新的經驗值重組

        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 ();

    }

 

}


簡單實例: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>

    /// 提升經驗按鈕點擊事件

    /// </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;

            }

        }

    }

}


 

 



 


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

洛宁县| 志丹县| 光山县| 博爱县| 玉林市| 庆云县| 香港| 平昌县| 宁国市| 海安县| 肃宁县| 射洪县| 新民市| 洛阳市| 江津市| 天祝| 西藏| 龙泉市| 巨野县| 广南县| 增城市| 郎溪县| 新巴尔虎左旗| 台山市| 成武县| 固镇县| 于都县| 慈溪市| 舒兰市| 那曲县| 安达市| 黔西| 原阳县| 巴彦县| 宁明县| 本溪市| 台山市| 临潭县| 河池市| 临夏县| 岢岚县|