中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

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

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

蓬溪县| 胶南市| 隆化县| 淳安县| 开平市| 武强县| 拉孜县| 河北区| 甘孜| 当阳市| 张家川| 阿勒泰市| 宣汉县| 温州市| 禹州市| 云林县| 灌云县| 浦江县| 凯里市| 富川| 辽宁省| 大冶市| 秭归县| 东乡县| 日照市| 太仆寺旗| 通榆县| 上思县| 胶南市| 砀山县| 区。| 乡宁县| 巴南区| 镇赉县| 城市| 台中市| 株洲县| 丹巴县| 缙云县| 绥江县| 霍州市|