【Unity】MVC的简单分享以及一个在UI中使用的例子

MVC的解释为(MVC的字母顺序也代表了框架的构建顺序):

Model(模型层/数据层):负责数据的处理、例如增删查改等等。

View(视图层):负责获取控件,向用户显示数据。

Controller(控制层/业务逻辑层):处理业务逻辑,包括事件监听、触发数据更新、触发界面更新。

MVC各个模块之间的协作图:

使用MVC的

好处:

  1. 各个模块之间各司其职、能够降低耦合度;
  2. 有利于项目的分工进行;
  3. 有利于组件的复用。

坏处:

  1. 文件和类增加会导致系统的复杂度变高;
  2. 有些小的项目只用一个脚本就能包括的逻辑,使用MVC框架后会增加结构的复杂性;
  3. 随着系统中类的增加,会导致性能的消耗随之增加。

适用场景:

较大型的商业级别项目

MVC在游戏当中的使用:作为UI框架使用较多,M为数据层面,V为UI层面,C是连接和管理两个模块的业务层。

下面是MVC框架例子:

如何使用?

脚本说明:

创建三个脚本分别为(脚本具体内容处于博客最下方):

PlayerModel.cs、RoleView.cs以及RoleController.cs。

PlayerModel.cs:定义数据模型、包括了更新数据和保存数据的方法、通过事件绑定通知Controller层需要让View层更新数据;

RoleView.cs:获取UI控件,并提供一个更新UI数据的方法;

RoleController.cs:上为视图层中的组件绑定事件业务、让视图层更新UI显示的数据,下能提醒数据层进行数据处理。

步骤:

  1. 创建一个UIPanel,取名为RolePanel,为其挂载RoleView和RoleController脚本,同时在该Panel中添加一个按钮和一个文本。
  1. 运行游戏,点击按钮后,生命值会减少。

PlayerModel.cs:

using UnityEngine;

using UnityEngine.Events;

public class PlayerModel

{

//自身的单例,让数据在整个游戏中只存在一份

private static PlayerModel instance = null;

public static PlayerModel Instance

{

get

{

if(instance == null)

instance = new PlayerModel();

return instance;

}

}

//数据

private int health;//生命值

public int Health

{

get

{

return health;

}

}

//反馈给Controller的更新事件

public UnityAction<PlayerModel> updateModelEvent = null;

//1.初始化数据

public void InitData()

{

health = PlayerPrefs.GetInt("Health", 100);//初始化生命值,若在PlayerPrefs中没有这个属性则创建它,并将其的数值设置为100

}

//2.更新数据,假定更新一次减少10的生命值

public void UpdateData()

{

health -= 10;

//保存数据

SaveData();

}

//3.保存数据

public void SaveData()

{

PlayerPrefs.SetInt("Health",health);

//触发更新事件

UpdateDataInvoke();

}

//4.更新后触发的事件

/// <summary>

/// 添加事件的方法

/// </summary>

/// <param name="action"></param>

public void AddUpdateEvent(UnityAction<PlayerModel> action)

{

if (action != null)

updateModelEvent += action;

}

/// <summary>

/// 移除事件的方法

/// </summary>

/// <param name="action"></param>

public void RemoveUpdateEvent(UnityAction<PlayerModel> action)

{

if(action != null)

updateModelEvent -= action;

}

/// <summary>

/// 更新时的操作

/// </summary>

private void UpdateDataInvoke()

{

if (updateModelEvent != null)

{

updateModelEvent?.Invoke(this);

}

}

}

RoleView.cs:

using UnityEngine;

using UnityEngine.UI;

public class RoleView : MonoBehaviour

{

//1.获取控件

public Text health_Text;

public Button hurtButton;

//2.更新数据的方法

public void UpdateUIInfo(PlayerModel playerModel)

{

health_Text.text = "当前生命值:" + playerModel.Health.ToString();

}

}

RoleController.cs:

using UnityEngine;

public class RoleController : MonoBehaviour

{

//自己的单例(作为系统访问MVC的入口)

private static RoleController instance;

public static RoleController Instance

{

get

{

return instance;

}

}

private RoleView roleView;//对应的视图层

private PlayerModel playerModel => PlayerModel.Instance;

// Start is called before the first frame update

void Start()

{

if(roleView == null)

{

//1.获取相关引用

//获取Panel

//获取组件

roleView = this.GetComponent<RoleView>();

instance = this.GetComponent<RoleController>();

//2.初始化

PlayerModel.Instance.InitData();

roleView.UpdateUIInfo(playerModel);

//3.绑定相应逻辑

//View层绑定业务

roleView.hurtButton.onClick.AddListener(HurtBtnMethod);

//Model层绑定更新事件

PlayerModel.Instance.AddUpdateEvent(UpdateEvent);

}

}

/// <summary>

/// 受伤按钮绑定的方法

/// </summary>

private void HurtBtnMethod()

{

//受伤

PlayerModel.Instance.UpdateData();

}

private void OnDestroy()

{

//1.移除业务

//2.销毁对象

//3.置空

if (roleView != null)

{

roleView.hurtButton.onClick.RemoveListener(HurtBtnMethod);

Destroy(roleView);

roleView = null;

}

PlayerModel.Instance.RemoveUpdateEvent(UpdateEvent);

}

/// <summary>

/// 更新事件

/// </summary>

private void UpdateEvent(PlayerModel playerModel_)

{

roleView.UpdateUIInfo(playerModel_);

}

}

完成!!!

相关推荐
神码编程18 小时前
【Unity】MiniGame编辑器小游戏(十五)中国象棋局域网对战【Chinese Chess】(上)
unity·编辑器·游戏引擎·小游戏
伽蓝_游戏18 小时前
第四章:AssetBundle 核心机制与文件结构
unity·c#·游戏引擎·游戏程序
2501_9307077818 小时前
使用C#代码拆分 PowerPoint 演示文稿
开发语言·c#·powerpoint
郝学胜-神的一滴19 小时前
中级OpenGL教程 006:高光反射原理与 Shader 实现
c++·unity·godot·图形渲染·three.js·opengl·unreal
SenChien19 小时前
C#学习笔记-入门篇
笔记·学习·c#·rider
诙_19 小时前
由C++速通C#
开发语言·c#
Xin_ye1008619 小时前
C# 零基础到精通教程 - 第九章:面向对象编程(高级)——接口、委托与事件
开发语言·c#
步步为营DotNet19 小时前
深入.NET 11:C# 14 在边缘计算数据处理的优化与实践
c#·.net·边缘计算
weixin_4280053019 小时前
C#调用 AI学习从0开始-第1阶段(基础与工具)-第6天流式输出
开发语言·学习·c#·流式输出stream
xiaoshuaishuai819 小时前
C# Anthropic连接超时原因及方案
开发语言·网络·tcp/ip·c#