Unity PureMVC 之 PureMVC 框架的下载,以及一个简单应用学会使用 PureMVC 框架
目录
Unity PureMVC 之 PureMVC 框架的下载,以及一个简单应用学会使用 PureMVC 框架
一、简单介绍
Unity 学习中一些知识点整理总结,方便日后查看。
本节介绍 Pure MVC 框架在哪里下载,怎么使用的简单说明,算是 Pure MVC 的入门课程吧,有不对的地方欢迎指正。
Pure MVC是在基于模型、视图和控制器MVC模式建立的一个轻量级的应用框架,这种开源框架是免费的,它最初是执行的ActionScript 3语言使用的Adobe Flex、Flash和AIR,现在已经移植到几乎所有主要的发展平台,目前支持两个版本框架:标准和多核,总之,标准版提供了一种简单的编码分离的方法,按照MVC设计概念。
二、原理图
Proxy:数据代理,接受 Command 对数据模型进行增删改查等的操作,并发送通知给 Mediator
Mediator:媒介层,进行视图UI的相关管理,数据显示,按钮事件操作等,接收Proxy通知,也发送通知给 Command
Command:命令层,接收 Mediator 的消息通知,对Proxy 进行数据操作
三、简单Demo 效果预览
四、注意事项
1、Pure MVC 有两个版本 :
PureMVC / puremvc-csharp-standard-framework 案例使用该版本,单线程的正常版本
PureMVC / puremvc-csharp-multicore-framework 多线程版本,适合服务器开发的版本
2、PureMVC 的 Command(命令层) 的生命周期是每执行一次对应 Command 就会长生新的实例对象,尽量避免把不需要每次生成新的实例对象的代码添加到 Command (命令层)里面
五、PureMVC 框架的下载
1、百度上搜索 puremvc
2、点击进入官网
Pure MVC 官网网址:http://puremvc.org/
3、Unity 使用 C# 语言,点击 C# 进入即可
4、PureMVC C# Github 网页
PureMVC C# Github 网址:https://github.com/PureMVC/puremvc-csharp-standard-framework
5、点击 Code 即可下载源码框架
我们这里下载 puremvc-csharp-standard-framework 版本:https://github.com/PureMVC/puremvc-csharp-standard-framework
六、实现步骤
1、打开 Unity 新建一个空工程
2、把下载的 Pure MVC 解压,并对应导入 Unity 中
3、场景 UI 布局如下
4、新建几个脚本,实现对应的MVC,其中
1)MyData 和 DataProxy 属于 M(Proxy)模型 层,负责数据模型和数据操作,并发送消息通知给 V(Mediator) 视图层
2)DataMediator 属于 V (Mediator) 视图层,负责 UI 的显示和按钮事件等,并接收 M(Proxy)模型 层消息,并发送消息给 C(Command)控制层
3)DataCommand 属于 C(Command)控制层,负责接收 V(Mediator) 视图层 消息,并实际执行命令操作 M(Proxy)模型 层 数据
4)ApplicationFacade 属于 门面,负责注册 MVC 层
5)StartGame 游戏入口,启动 PureMVC 框架
5、把脚本 StartGame 挂载到 Canvas 上
6、运行场景,效果如上
七、关键代码
1、StartGame
/****************************************************
文件:StartGame.cs
作者:Administrator
邮箱:https://blog.csdn.net/u014361280
日期:2020/08/15 22:52:19
功能:游戏入口类
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace PureMVC_XAN.Demo
{
public class StartGame : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
// 这个脚本挂载到 Canvas 上,启动 PureMVC 框架
new ApplicationFacade(this.gameObject);
}
}
}
2、ApplicationFacade
/****************************************************
文件:ApplicationFacade.cs
作者:Administrator
邮箱:https://blog.csdn.net/u014361280
日期:2020/08/15 22:53:38
功能:项目控制类
1、三层的MVC关联绑定
*****************************************************/
using PureMVC.Patterns.Facade;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace PureMVC_XAN.Demo
{
public class ApplicationFacade : Facade
{
public ApplicationFacade(GameObject goRootNode)
{
/* MVC 三层的关联绑定 */
// 控制层注册(命令消息 与控制层类的对应关系,建立绑定)
RegisterCommand("Reg_StartDataCommand", ()=> { return new DataCommand(); });
// 视图层注册
RegisterMediator(new DataMediator(goRootNode));
// 模型层注册
RegisterProxy(new DataProxy());
}
}
}
3、DataCommand
/****************************************************
文件:DataCommand.cs
作者:Administrator
邮箱:https://blog.csdn.net/u014361280
日期:2020/08/15 22:53:22
功能:数据控制类,控制层
1、接收玩家的输入,进行处理
*****************************************************/
using PureMVC.Interfaces;
using PureMVC.Patterns.Command;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace PureMVC_XAN.Demo
{
public class DataCommand : SimpleCommand
{
/// <summary>
/// 执行方法
/// </summary>
/// <param name="notification"></param>
public override void Execute(INotification notification)
{
// 调用数据层的“增加等级”的方法
DataProxy dataProxy = Facade.RetrieveProxy(DataProxy.NAME) as DataProxy;
dataProxy.AddLevel(5);
}
}
}
4、DataMediator
/****************************************************
文件:DataMediator.cs
作者:Administrator
邮箱:https://blog.csdn.net/u014361280
日期:2020/08/15 22:53:12
功能:数据显示类 视图层
1、视图层,UI 的数据显示
*****************************************************/
using PureMVC.Interfaces;
using PureMVC.Patterns.Mediator;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace PureMVC_XAN.Demo
{
public class DataMediator : Mediator
{
// 定义名称
public new const string NAME = "DataMediator";
// 定义显示的控件
private Text Level_Text;
private Button AddLevel_Button;
public DataMediator(GameObject goRootNode) :base(NAME)
{
// 查找赋值显示控件
Level_Text = goRootNode.transform.Find("Level_Text").GetComponent<Text>();
AddLevel_Button = goRootNode.transform.Find("AddLevel_Button").GetComponent<Button>();
// 按钮注册事件
AddLevel_Button.onClick.AddListener(OnClickAddLevelListener);
}
/// <summary>
/// 按钮点击事件监听
/// </summary>
private void OnClickAddLevelListener() {
// 定义消息,发送控制层
SendNotification("Reg_StartDataCommand");
}
public override string[] ListNotificationInterests()
{
List<string> listResult = new List<string>();
// 可以接收的消息集合
listResult.Add("Msg_AddLevel");
return listResult.ToArray();
}
public override void HandleNotification(INotification notification)
{
switch (notification.Name)
{
case "Msg_AddLevel":
// 把模型层发来的数据,显示到控件上
Level_Text.text = (notification.Body as MyData).Level.ToString();
break;
default:
break;
}
}
}
}
5、DataProxy
/****************************************************
文件:DataProxy.cs
作者:Administrator
邮箱:https://blog.csdn.net/u014361280
日期:2020/08/15 22:52:58
功能:数据代理类 模型层
1、模型成,数据的管理操作
*****************************************************/
using PureMVC.Patterns.Proxy;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace PureMVC_XAN.Demo
{
public class DataProxy : Proxy
{
// new 覆盖子类同名字段
public new const string NAME = "DataProxy";
// 数据实例
private MyData _MyData = null;
/// <summary>
/// 构造函数
/// </summary>
public DataProxy() : base(NAME)
{
_MyData = new MyData();
}
/// <summary>
/// 增加玩家的等级
/// </summary>
/// <param name="addNum"></param>
public void AddLevel(int addNum) {
// 把参数累加到 实体类 中
_MyData.Level += addNum;
// 把变化的数据,发送给 视图层
SendNotification("Msg_AddLevel", _MyData);
}
}
}
6、MyData
/****************************************************
文件:MyData.cs
作者:Administrator
邮箱:https://blog.csdn.net/u014361280
日期:2020/08/15 22:52:50
功能:数据实例类 模型层
1、真正的数据
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace PureMVC_XAN.Demo
{
public class MyData
{
// 等级
private int _Level =0;
// 等级属性
public int Level { get => _Level; set => _Level = value; }
}
}