赞
踩
目录
搭建如下的UI界面,并制作成预制体,并放于Resources\Prefabs目录。
创建三个脚本:Model.cs、View.cs、Controller.cs。并将View.cs、Controller.cs挂于预制体上。
- using System;
- using UnityEngine;
-
- public class Model
- {
- // 单例
- private static Model instance;
- public static Model Instance
- {
- get
- {
- if (instance == null)
- {
- instance = new Model();
- instance.Init();
- }
- return instance;
- }
- }
-
-
- // 数据
- private int coinCount;
- public int CoinCount
- {
- get { return coinCount; }
- }
-
-
- // 数据操作:初始化、更新、保存
- private void Init()
- {
- coinCount = PlayerPrefs.GetInt("CoinCount", 0);
- }
- public void UpdateCoinCount()
- {
- ++coinCount;
- Save();
- }
- private void Save()
- {
- PlayerPrefs.SetInt("CoinCount", coinCount);
- CallEvent();
- }
-
-
- // 事件:用于通知Controller进行更新View
- private event Action<Model> ModelEvent;
- public void AddEventListener(Action<Model> function)
- {
- ModelEvent += function;
- }
- public void RemoveEventListener(Action<Model> function)
- {
- ModelEvent -= function;
- }
- private void CallEvent()
- {
- ModelEvent?.Invoke(this);
- }
- }

- using UnityEngine;
- using UnityEngine.UI;
-
- public class View : MonoBehaviour
- {
- //提供UI控件
- public Text coinText;
-
- public Button addCoinButton;
-
- //提供更新UI控件的方法
- public void UpdateCoin(Model model)
- {
- coinText.text = model.CoinCount.ToString();
- }
- }

一个UI预制体对应一个View、一个Controller。
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class Controller : MonoBehaviour
- {
- //controller控制view
- private View view;
-
- private static Controller controller;
- public static Controller Instance
- {
- get
- {
- return controller;
- }
- }
-
- //1.面板显示隐藏
- public static void Show()
- {
- if (controller == null)
- {
- var temp = Resources.Load<GameObject>("Prefabs/UIPanel");
- var go = Instantiate(temp);
- go.transform.parent = GameObject.Find("Canvas").transform;
- go.transform.localPosition = Vector3.zero;
- go.transform.localScale = Vector3.one;
- controller = go.GetComponent<Controller>();
- }
- controller.gameObject.SetActive(true);
-
- }
-
- public static void Hide()
- {
- if (controller != null)
- {
- controller.gameObject.SetActive(false);
- }
- }
-
- void Start()
- {
- view = GetComponent<View>();
-
- //2.首次更新面板数据
- view.UpdateCoin(Model.Instance);
-
-
- //3.事件监听,更新数据
- view.addCoinButton.onClick.AddListener(() =>
- {
- Model.Instance.UpdateCoinCount();
- });
- Model.Instance.AddEventListener(UpdateCoin);
- }
-
- private void UpdateCoin(Model model)
- {
- //2.更新面板数据
- view.UpdateCoin(model);
- }
-
-
- private void OnDestroy()
- {
- Model.Instance.RemoveEventListener(UpdateCoin);
- }
- }

- using UnityEngine;
-
- public class TestMVC : MonoBehaviour
- {
- void Update()
- {
- if (Input.GetMouseButtonDown(0))
- {
- Controller.Show();
- }
- if (Input.GetMouseButtonDown(1))
- {
- Controller.Hide();
- }
- }
- }

- private event Action<Model> ModelEvent;
- public void AddEventListener(Action<Model> function)
- {
- ModelEvent += function;
- }
- public void RemoveEventListener(Action<Model> function)
- {
- ModelEvent -= function;
- }
- private void CallEvent()
- {
- ModelEvent?.Invoke(this);
- }
在C#中,event关键字用于声明一个事件,它本质上是一种特殊的多播委托。`ModelEvent`在这里是一个事件,当你对其使用`+=`操作符时,你实际上是将一个回调方法添加到委托的调用列表中。如果你注册了多个回调,那么这些回调方法就会被添加到`ModelEvent`的调用链中。
当调用`ModelEvent?.Invoke(this);`时,以下是在底层发生的事情:
在C#中,事件的使用是一种很好的设计模式,它允许对象通知其他对象发生了某些事情,而不需要知道这些对象是谁或者它们要做什么。这有助于保持代码的解耦和灵活性。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。