Unity中MVC的简单应用-创新互联

本篇简单介绍一下,Unity中MVC框架的简单应用。MVC自1982年被设计出来,至今都有着很大比重的使用率,特别是目前软件及游戏迭代此之快的情况下。高效的增删功能低耦合又小巧灵活的框架MVC,深受广大ProgramDesigner的喜爱。

创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站制作、网站设计、杂多网络推广、成都小程序开发、杂多网络营销、杂多企业策划、杂多品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们大的嘉奖;创新互联为所有大学生创业者提供杂多建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

Unity中MVC的简单应用

数据是程序的灵魂,视图是看清灵魂的眼睛,控制器从中拨开迷雾,这就形成了现在的MVC。

Unity中MVC的简单应用

简单实例:Model

using UnityEngine;

using System.Collections;

///

/// 模型委托(当用户信息发生变化时执行)

///

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

    {

    }

    ///

    /// 玩家等级属性

    ///

    /// The player level.

    public int PlayerLevel {

        get {

            return playerLevel;

        }

        set {

            playerLevel = value;

            //如果委托对象不为空

            if (OnLevelChange != null) {

                //执行委托

                OnLevelChange (playerLevel);

            }

        }

    }

    ///

    /// 玩家经验属性

    ///

    /// The player experience.

    public int PlayerExperience {

        get {

            return playerExperience;

        }

        set {

            playerExperience = value;

            if (OnExperienceChange != null) {

                OnExperienceChange (playerExperience);

            }

        }

    }

    ///

    /// 玩家升级经验属性

    ///

    /// The player full experience.

    public int PlayerFullExperience {

        get {

            return playerFullExperience;

        }

        set {

            playerFullExperience = value;

            if (OnFullExperienceChange != null) {

                OnFullExperienceChange (playerFullExperience);

            }

        }

    }

    ///

    /// 金币数量属性

    ///

    /// The gold number.

    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;

    }

    ///

    /// 提升经验按钮点击事件

    ///

    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;

            }

        }

    }

}

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前题目:Unity中MVC的简单应用-创新互联
本文来源:http://azwzsj.com/article/deidph.html