Unity中的MVC思想

MVC全名是Model View Controller

模型(model) - 视图(view) - 控制器(controller) 的缩写

1.MVC的历史--主要用于软件和网页开发

2.MVC的基本概念--数据、界面、业务逻辑分离

3.MVC在游戏中的应用-非必须的UI系统开发框架

使用MVC

Model   数据

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.Events;

public class ModelPlayer : BaseManager<ModelPlayer>

{

    private string playerName;

    public string PlayerName

    {

        get

        {

            return playerName;

        }

    }

    private int lev;

    public int Lev

    {

        get

        {

            return lev;

        }

    }

    private int money;

    public int Money

    {

        get

        {

            return money;

        }

    }

    private int gem;

    public int Gem

    {

        get

        {

            return gem;

        }

    }

    private int power;

    public int Power

    {

        get

        {

            return power;

        }

    }

    private int hp;

    public int Hp

    {

        get

        {

            return hp;

        }

    }

    private int atk;

    public int Atk

    {

        get

        {

            return atk;

        }

    }

    private int def;

    public int Def

    {

        get

        {

            return def;

        }

    }

    private int crit;

    public int Crit

    {

        get

        {

            return crit;

        }

    }

    private int miss;

    public int Miss

    {

        get

        {

            return miss;

        }

    }

    private int luck;

    public int Luck

    {

        get

        {

            return luck;

        }

    }

    //通知外部更新的事件

    //通过它和外部建立联系 而不是直接获取外部的面板

    private event UnityAction<ModelPlayer> updateEvent;

    //通过单例模式 来达到数据的唯一 性

    private static ModelPlayer data = null;

    public static ModelPlayer Data

    {

        get

        {

            if (data == null)

            {

                data = new ModelPlayer();

                data.Init();

            }

            return data;

        }

    }

    //数据操作

    //初始化

    public void Init()

    {

        playerName = PlayerPrefs.GetString("PlayerName", "耳东");

        lev = PlayerPrefs.GetInt("PlayerLev", 1);

        money = PlayerPrefs.GetInt("PlayerMoney", 9999);

        gem = PlayerPrefs.GetInt("PlayerGem", 8888);

        power = PlayerPrefs.GetInt("PlayerPower", 99);

        hp = PlayerPrefs.GetInt("PlayerHp", 100);

        atk = PlayerPrefs.GetInt("PlayerAtk", 20);

        def = PlayerPrefs.GetInt("playerDef", 10);

        crit = PlayerPrefs.GetInt("PlayerCrit", 20);

        miss = PlayerPrefs.GetInt("playerMiss", 10);

        luck = PlayerPrefs.GetInt("PlayerLuck", 40);

    }

    //更新  升级

    public void LevUp()

    {

        //升级  改变内容

        lev += 1;

        hp += lev;

        atk += lev;

        def += lev;

        crit += lev;

        miss += lev;

        luck += lev;

        SaveData();

    }

    //保存

    public void SaveData()

    {

        PlayerPrefs.SetString("PlayerName", playerName);

        PlayerPrefs.SetInt("PlayerLev", lev);

        PlayerPrefs.SetInt("PlayerMoney", money);

        PlayerPrefs.SetInt("PlayerGem", gem);

        PlayerPrefs.SetInt("PlayerPower", power);

        PlayerPrefs.SetInt("PlayerHp", hp);

        PlayerPrefs.SetInt("PlayerAtk", atk);

        PlayerPrefs.SetInt("playerDef", def);

        PlayerPrefs.SetInt("PlayerCrit", crit);

        PlayerPrefs.SetInt("playerMiss", miss);

        PlayerPrefs.SetInt("PlayerLuck", luck);

        UpdateInfo();

    }

    public void AddEventListener(UnityAction<ModelPlayer> function)

    {

        updateEvent += function;

    }

    public void RemoveEventListener(UnityAction<ModelPlayer> function)

    {

        updateEvent -= function;

    }

    //通知外部更新数据的方法

    private void UpdateInfo()

    {

        if (updateEvent != null)

        {

            updateEvent(this);

        }

    }

}

View   面板

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

public class MainView : MonoBehaviour

{

    public Button btnRole;

    public Text txtName;

    public Text txtmoney;

    public Text txtlev;

    public Text txtgem;

    public Text txtpower;

    public void UpDateInfo(ModelPlayer data)

    {

        txtName.text ="角色:"+ data.PlayerName;

        txtlev.text= "等级:LV." + data.Lev;

        txtmoney.text = "金币:"+data.Money.ToString();

        txtgem.text = "钻石:"+data.Gem.ToString();

        txtpower.text ="能量:"+ data.Power.ToString();

    }

}

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

public class RoleView : MonoBehaviour

{

    public Button btnLevUp;

    public Button btnClose;

    public Text txtlev;

    public Text txthp;

    public Text txtatk;

    public Text txtdef;

    public Text txtcrit;

    public Text txtmiss;

    public Text txtluck;

    public void UpDateInfo(ModelPlayer data)

    {

        txtlev.text ="等级:LV."+ data.Lev;

        txthp.text = "血量:"+data.Hp.ToString();

        txtdef.text ="防御力:"+ data.Def.ToString();

        txtatk.text="攻击力:"+ data.Atk.ToString();

        txtcrit.text ="暴击率:"+ data.Crit.ToString();

        txtmiss.text ="闪避率:"+ data.Miss.ToString();

        txtluck.text ="幸运值:"+ data.Luck.ToString();

    }

}

Controller    逻辑

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class MainController : MonoBehaviour

{

    //界面的显示隐藏

    //界面 事件的监听  来处理对应的业务逻辑

    //界面的更新

    private MainView mainView;

    private static MainController controller = null;

    public static MainController Controller

    {

        get

        {

            return controller;

        }

    }

    public static void ShowMe()

    {

        if (controller == null)

        {

            //实例化面板对象

            GameObject res = Resources.Load<GameObject>("UI/MainPanel");

            GameObject obj = Instantiate(res);

            obj.name = res.name;

            //设置它的父对象 为Canvas

            obj.transform.SetParent(GameObject.Find("Canvas").transform, false);

            controller = obj.GetOrAddComponent<MainController>();

        }

        controller.gameObject.SetActive(true);

    }

    public static void HideMe()

    {

        if (controller != null)

            controller.gameObject.SetActive(false);

    }

    private void Start()

    {

        mainView = GetComponent<MainView>();

        mainView.UpDateInfo(ModelPlayer.Data);

        mainView.btnRole.onClick.AddListener(RoleOnClick);

        ModelPlayer.Data.AddEventListener(UpDateInfo);

    }

    private void RoleOnClick()

    {

        RoleController.ShowMe();

    }

    private void UpDateInfo(ModelPlayer data)

    {

        if (mainView != null)

            mainView.UpDateInfo(data);

    }

    private void OnDestroy()

    {

        ModelPlayer.Data.RemoveEventListener(UpDateInfo);

    }

}

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class RoleController : MonoBehaviour

{

    private RoleView roleView;

    private static RoleController controller = null;

    public static RoleController Controller

    {

        get

        {

            return controller;

        }

    }

    public static void ShowMe()

    {

        if (controller == null)

        {

            //实例化面板对象

            GameObject res = Resources.Load<GameObject>("UI/RolePanel");

            GameObject obj = Instantiate(res);

            obj.name = res.name;

            //设置它的父对象 为Canvas

            obj.transform.SetParent(GameObject.Find("Canvas").transform, false);

            controller = obj.GetOrAddComponent<RoleController>();

        }

        controller.gameObject.SetActive(true);

    }

    public static void HideMe()

    {

        if (controller != null)

            controller.gameObject.SetActive(false);

    }

    private void Start()

    {

        roleView = GetComponent<RoleView>();

        roleView.UpDateInfo(ModelPlayer.Data);

        roleView.btnLevUp.onClick.AddListener(LevUpOnClick);

        roleView.btnClose.onClick.AddListener(CloseOnClick);

        ModelPlayer.Data.AddEventListener(UpDateInfo);

    }

    private void LevUpOnClick()

    {

        ModelPlayer.Data.LevUp();

    }

    private void CloseOnClick()

    {

        HideMe();

    }

    private void UpDateInfo(ModelPlayer data)

    {

        if (roleView != null)

            roleView.UpDateInfo(data);

    }

    private void OnDestroy()

    {

        ModelPlayer.Data.RemoveEventListener(UpDateInfo);

    }

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容