Unity MVC模式简述
什么是MVC
MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式。这种模式用于应用程序的分层开发。
-
Model(模型) - 模型代表一个存取数据的对象或 JAVA POJO。它也可以带有逻辑,在数据变化时更新控制器。
-
View(视图) - 视图代表模型包含的数据的可视化。
-
Controller(控制器) - 控制器作用于模型和视图上。它控制数据流向模型对象,并在数据变化时更新视图。它使视图与模型分离开。
为什么要使用MVC
-
多个视图能共享一个模型。同一个模型可以被不同的视图重用,大大提高了代码的可重用性。
-
由于MVC的三个模块相互独立,改变其中一个不会影响其他两个,所以依据这种设计思想能构造良好的松耦合的构件。
-
控制器提高了应用程序的灵活性和可配置性。控制器可以用来联接不同的模型和视图去完成用户的需求,这样控制器可以为构造应用程序提供强有力的手段
Demo
使用MVC架构模式来做一个小例子,话说皇帝陛下最近有点小忧愁,后宫佳丽三千,不易于管理啊,每次翻牌子都非常麻烦,于是就想着开发一款翻牌子软件,来简化这种翻牌子的操作,于是忠心的大臣就找人开发了这个翻牌子软件。。。。嘿嘿嘿
佳丽的数据模型
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//人物模型
public struct PlayerModel {
public string name;//名字
public int age;//年龄
public int id;//身份证
public float height;//身高
public float weight;//体重
}
public class GameModel : MonoBehaviour
{
public delegate void IUpdateView(PlayerModel playerModel);
public event IUpdateView OnUpdateView;
private PlayerModel _playerModel;
public PlayerModel playerModel {
get { return _playerModel; }
set {
_playerModel = value;
OnUpdateView?.Invoke(_playerModel);
}
}
}
驱动数据
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
public static GameController instance;
public GameModel gameModel;
public GameView gameView;
/// <summary>
/// 三千佳丽
/// </summary>
private PlayerModel[] players = new PlayerModel[3];
private void Awake()
{
instance = this;
}
void Start()
{
SetPlayers();
gameModel.OnUpdateView += gameView.UpdateView;
gameModel.playerModel = GetPlayer();
}
/// <summary>
/// 随机翻牌子
/// </summary>
/// <returns></returns>
PlayerModel GetPlayer() {
int id = Random.Range(0,players.Length);
return players[id];
}
/// <summary>
/// 把所有佳丽记录在案,方便皇上翻牌
/// </summary>
void SetPlayers() {
PlayerModel playerModel = new PlayerModel();
PlayerModel playerModel2 = new PlayerModel();
PlayerModel playerModel3 = new PlayerModel();
playerModel.name = "赛西施";
playerModel.age = 18;
playerModel.height = 165;
playerModel.weight = 50;
playerModel.id = 88888888;
playerModel2.name = "胜貂蝉";
playerModel2.age = 19;
playerModel2.height = 164;
playerModel2.weight = 49;
playerModel2.id = 99999999;
playerModel3.name = "杨玉环";
playerModel3.age = 24;
playerModel3.height = 163;
playerModel3.weight = 49;
playerModel3.id = 66666666;
players[0] = playerModel;
players[1] = playerModel2;
players[2] = playerModel3;
}
/// <summary>
/// 翻牌按钮,随机选择一个佳丽
/// </summary>
public void OnClickChangeBtn() {
PlayerModel playerModel = GetPlayer();
if (playerModel.Equals(gameModel.playerModel))
{
//刚宠幸过,重新翻
OnClickChangeBtn();
}
else {
//幸运儿诞生
gameModel.playerModel = playerModel;
}
}
}
来吧,展示:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameView : MonoBehaviour
{
public Text showText;
public Button changeBtn;
private void Start()
{
changeBtn.onClick.AddListener(GameController.instance.OnClickChangeBtn);
}
/// <summary>
/// 展示佳丽信息
/// </summary>
public void UpdateView(PlayerModel playerModel)
{
showText.text = string.Format(@"
姓名:{0}
年龄:{1}
身高:{2}
体重:{3}
身份证:{4}
",playerModel.name,playerModel.age,playerModel.height,playerModel.weight,playerModel.id);
}
}
展示一波