这是一个工具类可有可无,根据自己实际情况取舍
using UnityEngine;
using System.Collections;
/// <summary>
/// 获取场景中的对象[设置场景中对象]
/// </summary>
public class GameUtility
{
/// <summary>
/// 获取子节点
/// </summary>
public static Transform GetChild(GameObject root, string path)
{
Transform tra = root.transform.Find(path);
if (tra == null) Debug.Log(path + "not find");
return tra;
}
/// <summary>
/// 获取子节点组件
/// </summary>
public static T GetChildComponent<T>(GameObject root, string path) where T : Component
{
Transform tra = root.transform.Find(path);
if (tra == null) Debug.Log(path + "not find");
T t = tra.GetComponent<T>();
return t;
}
}
NotificationConstant类是对常量的规范定义类
using UnityEngine;
using System.Collections;
/// <summary>
/// Notification常用字符串常量去表示
/// </summary>
public class NotificationConstant
{
public const string ButtonClick = "ButtonClick";
public const string LevelChange = "LevelChange";
public const string RnChange = "RnChange";
}
数据定义类,依据个人需求定义
using UnityEngine;
using System.Collections;
/// <summary>
/// 数据定义
/// </summary>
public class CharacterInfo
{
public int Level { get; set; }
public int Rn { get; set; }
public CharacterInfo()
{
}
public CharacterInfo(int level,int rn)
{
Level = level;
Rn = rn;
}
}
PureMVC主体框架的搭建即四个继承类,TestCommand继承Simple Command[Controller],TestProxy继承Proxy[Model],TestMediator继承Mediator[View],TestFacade继承Facade,这个是中介代理,负责注册事件
using UnityEngine;
using System.Collections;
using PureMVC.Patterns;
public class TestCommand : SimpleCommand
{
public new const string NAME = "TestCommand";
//只对ButtonClick通知拦截,由TestFacade注册的
public override void Execute(PureMVC.Interfaces.INotification notification)
{
TestProxy proxy = (TestProxy)Facade.RetrieveProxy(TestProxy.NAME);
// Debug.Log(proxy + "--" + TestProxy.NAME);
proxy.ChangeNum(1);
}
}
****************************************************************************************************************
using UnityEngine;
using System.Collections;
using PureMVC.Patterns;
public class TestProxy : Proxy
{
public new const string NAME = "TestProxy";
public CharacterInfo Data { get; set; }
public TestProxy()
:base(NAME )
{
Data = new CharacterInfo();
}
//执行ChangeLevel操作,并发送LevelChange通知
//由TestMediator对每个通知进行监测,若在感兴趣的通知里
//则拦截
public void ChangeNum(int change)
{
Data.Level += change;
SendNotification(NotificationConstant.LevelChange, Data);
int ro = Random.Range(1, 100);
Data.Rn = ro;
SendNotification(NotificationConstant.RnChange, Data);
}
}
******************************************************************************************************************
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using PureMVC.Patterns;
using UnityEngine.UI;
public class TestMediator : Mediator
{
public new const string NAME = "TestMediator";
//View
private Text levelText;
private Button levelUpButton;
private Text rnText;
public TestMediator(GameObject root)
: base(NAME)
{
levelText = GameUtility.GetChildComponent<Text>(root, "Text/LevelText");
levelUpButton = GameUtility.GetChildComponent<Button>(root, "LevelUpButton");
rnText = GameUtility.GetChildComponent<Text>(root, "Rondom/Text");
//给Button添加Onclick操作处理
levelUpButton.onClick.AddListener(OnClickLevelUpButton);
}
private void OnClickLevelUpButton()
{
//发送ButtonClick通知,由TestCommand拦截并执行Excute方法
SendNotification(NotificationConstant.ButtonClick);
}
//添加感兴趣的通知,只要有通知发出就会拿遍历list去匹配,
//匹配到则调用下面的HandleNotification函数
//进行处理通知
public override IList<string> ListNotificationInterests()
{
IList<string> list = new List<string>();
list.Add(NotificationConstant.LevelChange);
list.Add(NotificationConstant.RnChange);
return list;
}
//处理通知
public override void HandleNotification(PureMVC.Interfaces.INotification notification)
{
// Debug.Log(notification.Name);
CharacterInfo ci = notification.Body as CharacterInfo;
//对相应的通知做相应的处理
switch (notification.Name)
{
//对LevelChange通知处理过程
case NotificationConstant.LevelChange:
// CharacterInfo ci = notification.Body as CharacterInfo;
levelText.text = ci.Level.ToString();
break;
case NotificationConstant.RnChange:
// CharacterInfo c = notification.Body as CharacterInfo;
rnText.text = ci.Rn.ToString();
break;
default:
break;
}
}
}
******************************************************************************************************************
using UnityEngine;
using System.Collections;
using PureMVC.Patterns;
/// <summary>
/// Facade为主管
/// </summary>
public class TestFacade : Facade
{
public TestFacade(GameObject canvas)
{
// Debug.Log(canvas);
//给TestCommand注册拦截ButtonClick通知
RegisterCommand(NotificationConstant.ButtonClick, typeof(TestCommand));//Controller
// RegisterCommand(NotificationConstant.ButtonClick, typeof(TestCommand));//Controller
//注册中介和代理
RegisterMediator(new TestMediator(canvas));//View
RegisterProxy(new TestProxy());//Model
}
}
框架的启动 Test类,唯一一个挂在场景中的类
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
private GameObject canvas;
void Awake()
{
canvas = GameObject.Find("Canvas");
}
void Start()
{
new TestFacade(canvas );
}
}
注意:场景中的组件就不一一细写了根据TestMediator在场景中设置如图
再来一个PureMVC结构图
相信大家都理解了,不善言辞的我会慢慢练习的。。。。。。。
附Demo演示视频链接:https://live.csdn.net/v/140716
Demo_017源码下载链接,需要的自取:
链接:https://pan.baidu.com/s/1CP4FLttldNHW-gW2zUrlDA
提取码:ncv6