【唐老狮】Unity中的MVC思想—2—MVC的基本概念

【唐老狮】Unity中的MVC思想—5—使用MVC制作UI逻辑

MVC基本概念

MVC是一种编程思想

软件和网页都是由数据、UI构成的

屏幕截图 2023-05-08 182757

MVC全名是Model View Controller,是一种软件设计规范

屏幕截图 2023-05-08 182944

MVC在游戏中的应用

MVC主要用于UI系统的开发上

好处:降低耦合、方面修改、逻辑清晰

缺点:脚本变多、体量变大、流程复杂

使用MVC思想制作UI逻辑

Model数据脚本

PlayerModel.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Runtime.ConstrainedExecution;
using UnityEngine;
using UnityEngine.Events;

namespace learn
{
public class PlayerModel
{
//数据内容
private string _playerName;
private int _lev, _money, _gem, _power, _hp, _atk, _def, _crit, _miss, _luck;
public string PlayerName => _playerName;
public int Lev => _lev;
public int Money => _money;
public int Gem => _gem;
public int Power => _power;
public int Hp => _hp;
public int Atk => _atk;
public int Def => _def;
public int Crit => _crit;
public int Miss => _miss;
public int Luck => _luck;

private event UnityAction<PlayerModel> updateEvent;

//通过单例保证数据的唯一性
private static PlayerModel data = null;

public static PlayerModel Data
{
get
{
if (data==null)
{
data = new PlayerModel();
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);
}

public void AddEventListener(UnityAction<PlayerModel> funAction)
{
updateEvent += funAction;
}

public void RemoveEventListener(UnityAction<PlayerModel> function)
{
updateEvent -= function;
}

//通知外铺更新数据的方法
private void UpdateInfo()
{
//找到对应的 使用数据的脚本 去更新数据
if (updateEvent!= null)
{
updateEvent(this);
}
}
}
}

View界面脚本

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
26
using UnityEngine;
using UnityEngine.UI;

namespace learn
{
public class MainView : MonoBehaviour
{
//1.找控件
public Button btnRole;
public Text txtName;
public Text txtLev;
public Text txtMoney;
public Text txtGem;
public Text txtPower;

//2.提供面板更新的方法
public void UpdateInfo(PlayerModel data)
{
txtName.text = data.PlayerName;
txtLev.text = "LN." + data.Lev;
txtMoney.text = data.Money.ToString();
txtGem.text = data.Gem.ToString();
txtPower.text = data.Power.ToString();
}
}
}

RoleView.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
using UnityEngine;
using UnityEngine.UI;

namespace learn
{
public class RoleView : MonoBehaviour
{
//1.找控件
public Button btnClose;
public Button btnLevUp;

public Text txtLev;
public Text txtHp;
public Text txtAtk;
public Text txtDef;
public Text txtCrit;
public Text txtMiss;
public Text txtLuck;

//2.更新面板
public void UpdateInfo(PlayerModel data)
{
txtLev.text = "LV." + data.Lev;
txtHp.text = data.Hp.ToString();
txtAtk.text = data.Atk.ToString();
txtDef.text = data.Def.ToString();
txtCrit.text = data.Crit.ToString();
txtMiss.text = data.Miss.ToString();
txtLuck.text = data.Luck.ToString();
}
}
}

Controller业务逻辑脚本

MainController.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
using System;
using UnityEngine;

namespace learn
{
public class MainController : MonoBehaviour
{
private MainView _mainView;

private static MainController controller = null;

public static MainController Controller => controller;

//1.界面的显影

public static void ShowMe()
{
if (controller==null)
{
GameObject res = Resources.Load<GameObject>("UI/MainPanel");
GameObject obj = Instantiate(res, GameObject.Find("Canvas").transform, false);
controller = obj.GetComponent<MainController>();
}
controller.gameObject.SetActive(true);
}

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

private void Start()
{
_mainView = GetComponent<MainView>();
_mainView.UpdateInfo(PlayerModel.Data);
//2.事件的监听,处理对应的业务逻辑
_mainView.btnRole.onClick.AddListener(ClickRoleBtn);

PlayerModel.Data.AddEventListener(UpdateInfo);
}

private void ClickRoleBtn()
{
RoleController.ShowMe();
}
//3.界面的更新
private void UpdateInfo(PlayerModel data)
{
if (_mainView!=null)
{
_mainView.UpdateInfo(data);
}
}

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

RoleController.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 System;
using UnityEngine;

namespace learn
{
public class RoleController : MonoBehaviour
{
private RoleView roleView;

private static RoleController controller = null;

public static RoleController Controller => controller;

public static void ShowMe()
{
if (controller==null)
{
GameObject res = Resources.Load<GameObject>("UI/RolePanel");
GameObject obj = Instantiate(res, GameObject.Find("Canvas").transform, false);
controller = obj.GetComponent<RoleController>();
}
controller.gameObject.SetActive(true);
}

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

private void Start()
{
roleView = GetComponent<RoleView>();
roleView.UpdateInfo(PlayerModel.Data);

roleView.btnClose.onClick.AddListener(ClickCloseBtn);
roleView.btnLevUp.onClick.AddListener(ClickLevUpBtn);
PlayerModel.Data.AddEventListener(UpdateInfo);
}

private void UpdateInfo(PlayerModel data)
{
if (roleView!=null)
{
roleView.UpdateInfo(data);
}
}

private void ClickLevUpBtn()
{
PlayerModel.Data.LevUp();
}

private void ClickCloseBtn()
{
HideMe();
}
private void OnDestroy()
{
PlayerModel.Data.RemoveEventListener(UpdateInfo);
}
}
}