UnityMVC模式简述
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 {mvc的controller
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()
{
}
/// <summary>
/// 展⽰佳丽信息
/// </summary>
public void UpdateView(PlayerModel playerModel)
{
< = string.Format(@"
姓名:{0}
年龄:{1}
⾝⾼:{2}
体重:{3}
⾝份证:{4}
",playerModel.name,playerModel.age,playerModel.height,playerModel.weight,playerModel.id); }
}
展⽰⼀波
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论