前言
前一次使用的MVC框架是简单版通知类型实现,这次使用较复杂一点的方式来实现
前一次MVC框架链接:https://blog.csdn.net/liaoshengg/article/details/82150287
但与第一种也有区别:
把发布信息与Controller类分离出NotifiCenter,使得Model层 和 View层之间进行交互而不通过Controller控制层,
也就是抽象出一个中介者NotifiCenter (通知发布中心)来代替控制类。
Model
public class MyModel : MonoBehaviour {
private MyData mydata = new MyData ();
//View中发送通知,进行出来
public void UpdateScore(int s){
mydata.Score += s;
NotifiCenter.instance.Send ("UpdateUI", this.mydata);
}
public void UpdateLevel(int l){
mydata.Level += l;
NotifiCenter.instance.Send ("UpdateUI", this.mydata);
}
}
public class MyData{
public int Score = 0;
public int Level = 0;
}
View
public class MyView : MonoBehaviour {
private Text scoreText;
private Text levelText;
private Button upScoreBtn;
private Button upLevelBtn;
void Awake(){
scoreText = transform.Find ("ScoreText").GetComponent<Text> ();
levelText = transform.Find ("LevelText").GetComponent<Text> ();
upScoreBtn = transform.Find ("ScoreButton").GetComponent<Button> ();
upLevelBtn = transform.Find ("LevelButton").GetComponent<Button> ();
upScoreBtn.onClick.AddListener (scoreBtnOnClick);
upLevelBtn.onClick.AddListener (levelBtnOnClick);
}
private void scoreBtnOnClick(){
NotifiCenter.instance.Send ("UpdateScore", 2);
}
private void levelBtnOnClick(){
NotifiCenter.instance.Send ("UpdateLevel", 3);
}
public void UpdateUI(MyData data){
scoreText.text = data.Score.ToString ();
levelText.text = data.Level.ToString ();
}
}
Controller
public class MyController : MonoBehaviour {
private MyModel model;
private MyView view;
void Awake(){
model = GetComponent<MyModel> ();
view = GameObject.Find ("Canvas").GetComponent<MyView> ();
NotifiCenter.instance.Add ("UpdateScore", model.UpdateScore);
NotifiCenter.instance.Add ("UpdateLevel", model.UpdateLevel);
NotifiCenter.instance.Add ("UpdateUI", view.UpdateUI);
}
}
NotifiCenter
public class NotifiCenter : MonoBehaviour {
public static NotifiCenter instance;
public Dictionary<string,Action<MyData>> dicMV;
public Dictionary <string,Action<int>> dicVM;
void Awake(){
instance = this;
dicMV = new Dictionary<string, Action<MyData>> ();
dicVM = new Dictionary<string, Action<int>> ();
}
//重载
public void Add(string key,Action<MyData> value){
if (dicMV.ContainsKey (key)) {
dicMV [key] += value;
} else {
dicMV.Add (key, value);
}
}
public void Add(string key,Action<int> value ){
if (dicVM.ContainsKey (key)) {
dicVM [key] += value;
} else {
dicVM.Add (key, value);
}
}
public void Send(string key,MyData para){
if (dicMV.ContainsKey (key)) {
dicMV [key] (para);
}
}
public void Send(string key,int para){
if (dicVM.ContainsKey (key)) {
dicVM [key] (para);
}
}
}
实现流程
开始初始化:
开始的时候首先控制类先对NotifiCenter进行注册事件方法
NotifiCenter.instance.Add ("UpdateScore", model.UpdateScore);
NotifiCenter.instance.Add ("UpdateLevel", model.UpdateLevel);
NotifiCenter.instance.Add ("UpdateUI", view.UpdateUI);
不过这里因为是使用字典,要记得先初始化字典空间,再给字典添加成员
dicMV = new Dictionary<string, Action<MyData>> ();
dicVM = new Dictionary<string, Action<int>> ();
然后:
开始玩家和UI的交互(点击按钮)
点击按钮的时候,向通知中心发布(Send)一个消息,这个消息让数据层进行数据更新;
接着,数据更新完毕后也会向通知中心发布一个消息(Send)一个消息,这个消息让UI显示(Text)进行数据显示更新。