1,特别简单的一版
参考自该篇文章:
https://blog.csdn.net/frank901/article/details/72847352 文章很简单,但我个人觉得框架逻辑上有些不合理的地方,文章中使用委托作为事件的触发,个人感觉不使用一样可以达到效果,所以没有使用委托。
大致内容:一共3个类,GameModel,GameView,GameController。
GameModel:提供数据以及对数据的操作。
GameView:ui相关的数据,以及对ui相关数据的操作,为GameController注册事件。
GameController:当ui上的控件触发了注册的事件时,对GameView和GameModel做出相应的逻辑回应。
思考:
对于model,创建一个主ModelManager管理多个不同的Model,每个Model提供操作自己数据的函数,统一在ModelManager里操作。
对于view,创建一个主ViewManager管理多个不同的View,每个View提供操作自己的控件做出相应的逻辑操作的函数,事件都在ViewManager里注册。
对于controller,一样创建主的ControllerManager来管理多个不同的controller。
感觉这样做是要凉的节奏,逻辑上估计会很复杂,还是多看几篇文章先。。。
代码如下:
using UnityEngine;
public class GameModel : MonoBehaviour {
private int id;
public int Id
{
get{ return id;}
set { id = value;}
}
//行为逻辑数据操作等
public void ChangeModelValue(int modelValue)
{
Id = modelValue;
}
}
using UnityEngine;
using UnityEngine.UI;
public class GameView : MonoBehaviour {
private Text text;
private Button button;
private void Awake()
{
text = GameObject.Find("TextField").GetComponent
button = GameObject.Find("Button").GetComponent
}
using UnityEngine;
public class GameController : MonoBehaviour {
private static GameController instance;
public static GameController Get()
{
return instance;
}
GameModel model;
GameView view;
// Use this for initialization
void Awake () {
instance = this;
model = GetComponent
view = GameObject.Find("Canvas").GetComponent
}
private void Start()
{
//初始化数据
model.ChangeModelValue(1);
}
//触发逻辑事件时,model和view做出的操作
public void OnButtonClick()
{
model.ChangeModelValue(10);
view.UpDateTextInfo(model.Id.ToString());
}
}
2,好一点的版本