UMVCL7——MVP基本实例

MVP基本实例

还是和上一章相同的UI需求

假设我们有一个主面板,它的各个UI控件需要的效果如下

image

有一个选角面板,它的各个UI控件需要的效果如下

image

该UI的MVC的代码逻辑如下:

Model层

直接使用MVC那一章的Model代码即可

View层

相比MVC的那一章,这里的View层代码不一定需要再提供更新控件显示的方法了,只需要管理控件即可
更新控件的实现交给Presenter(主持人)层脚本实现

MVP_MainView.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using UnityEngine;
using UnityEngine.UI;

public class MVP_MainView : MonoBehaviour
{
public Text txtName;
public Text txtLev;
public Text txtMoney;
public Text txtGem;
public Text txtPower;

public Button btnRole;

//更新方法是可选的
//因为接下来 你可以到P层脚本里面通过访问控件去修改

//public void UpdateInfo(string name, int lev, int money, int gem, int power)
//{
// txtName.text = name;
// txtLev.text = "LV." + lev.ToString();
// txtMoney.text = money.ToString();
// txtGem.text = gem.ToString();
// txtPower.text = power.ToString();
//}
}

MVP_RoleView.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using UnityEngine;
using UnityEngine.UI;

public class MVP_RoleView : MonoBehaviour
{
public Text txtLev;
public Text txtHp;
public Text txtAtk;
public Text txtDef;
public Text txtCrit;
public Text txtMiss;
public Text txtLuck;

public Button btnClose;
public Button btnLevUp;

//更新方法是可选的
//因为接下来 你可以到P层脚本里面通过访问控件去修改
}

Presenter层

相比于MVC的Controller层,Presenter层脚本自己实现了对View层的UI控件的更新,
这也意味着Model层的数据不需要再传入View层让View层更新
因此,Presenter层切断了View层和Model层的关系,而是由Presenter层读取Model层数据再为View层更新控件

MainPresenter.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using UnityEngine;

public class MainPresenter : MonoBehaviour
{
private static MainPresenter presenter = null;
public static MainPresenter Instance => presenter;

//在Presenter中得到界面
private MVP_MainView mainView;

private void Start()
{
mainView = GetComponent<MVP_MainView>();
//不需要执行View的UpdateInfo,直接执行自己的方法
UpdateInfo(PlayerModel.Instance);
PlayerModel.Instance.AddEventListener(UpdateInfo);
//界面事件的监听,来处理对应的业务逻辑
mainView.btnRole.onClick.AddListener(() =>
{
//显示角色面板
RolePresenter.ShowMe();
});
}

private void OnDestroy()
{
PlayerModel.Instance.RemoveEventListener(UpdateInfo);
}

private void UpdateInfo(PlayerModel player)
{
//以前这里是把数据M传到V中去更新,现在全部由P来做,这就断绝了M和V的来往
if (mainView != null)
{
mainView.txtName.text = player.Name;
mainView.txtLev.text = "LV." + player.Level;
mainView.txtMoney.text = player.Money.ToString();
mainView.txtGem.text = player.Gem.ToString();
mainView.txtPower.text = player.Power.ToString();
}
}

public static void ShowMe()
{
if (presenter == null)
{
//实例化面板对象
GameObject res = Resources.Load<GameObject>("UI/MainPanel");
GameObject obj = Instantiate(res);
obj.transform.SetParent(GameObject.Find("Canvas").transform, false);
presenter = obj.GetComponent<MainPresenter>();
}
presenter.gameObject.SetActive(true);
}

public static void HideMe()
{
if (presenter != null)
presenter.gameObject.SetActive(false);
}
}

RolePresenter.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using UnityEngine;

public class RolePresenter : MonoBehaviour
{
private MVP_RoleView roleView;

private static RolePresenter presenter = null;

public static RolePresenter Presenter => presenter;

void Start()
{
roleView = GetComponent<MVP_RoleView>();
//不需要执行View的UpdateInfo,直接执行自己的方法
UpdateInfo(PlayerModel.Instance);
PlayerModel.Instance.AddEventListener(UpdateInfo);
roleView.btnClose.onClick.AddListener(() =>
{
HideMe();
});
roleView.btnLevUp.onClick.AddListener(() =>
{
PlayerModel.Instance.LevelUp();
});
}

private void OnDestroy()
{
PlayerModel.Instance.RemoveEventListener(UpdateInfo);
}

private void UpdateInfo(PlayerModel player)
{
if (roleView != null)
{
//以前这里是把数据M传到V中去更新,现在全部由P来做,这就断绝了M和V的来往
roleView.txtLev.text = "LV." + player.Level;
roleView.txtHp.text = player.Hp.ToString();
roleView.txtAtk.text = player.Atk.ToString();
roleView.txtDef.text = player.Def.ToString();
roleView.txtCrit.text = player.Crit.ToString();
roleView.txtMiss.text = player.Miss.ToString();
roleView.txtLuck.text = player.Luck.ToString();
}
}

public static void ShowMe()
{
if (presenter == null)
{
//实例化面板对象
GameObject res = Resources.Load<GameObject>("UI/RolePanel");
GameObject obj = Instantiate(res);
obj.transform.SetParent(GameObject.Find("Canvas").transform, false);
presenter = obj.GetComponent<RolePresenter>();
}
presenter.gameObject.SetActive(true);
}

public static void HideMe()
{
if (presenter != null)
presenter.gameObject.SetActive(false);
}
}