PureMVC开源框架在Unity中的使用逻辑

PureMVC框架官网(http://puremvc.org/)
开源代码
单核版本下载地址:
源:https://en.wikipedia.org/wiki/Singleton_pattern
自传蓝奏云:https://wwz.lanzoum.com/iv3KY07bzvje
PureMVC框架单核版本使用逻辑
首先编写PureNotification类:
这是通知名称类,使用常量string保存通知名称

Model层:
1. 编写数据结构类,纯class->PlayerDataObj
2. 编写继承Proxy的代理类,其中Proxy需要编写一个必选参数“名称",一个可选参数”数据变量“的构造方法

例:public PlayerProxy(string name,object data=null);
Proxy代理使用Data属性存储数据,使用Facade.RegisterProxy注册代理,可使用Facade.Retri…通过名字获取数据。

Proxy固定写法:
public class PlayerProxy : Proxy
{
   public new const string NAME = "PlayerProxy";
   public PlayerProxy() : base(NAME)
   {
   XXX data=new XXX();
   ....
   Data=data;
   }
...其他数据业务逻辑
    例如 PlayerLevelUp()数据升级逻辑
}

View层:
1. 为每个面板都编写View类,继承MonoBehaviour,用于声明UI组件以及编写更新UI信息方法UpdateInfo(XXX 数据信息类)
2. 为每个View类编写Mediator类,需要继承Mediator

Mediator固定写法:

public class MainView_Mediator : Mediator
{
    public static new string NAME = "Mediator名称,例如MainView_Mediator";
    public MainView_Mediator() : base(NAME)//构造函数,不需要写业务逻辑,调用父类的单参数构造函数即可
    {


    }
    public override string[] ListNotificationInterests()//重写该方法,用于告诉脚本该类需要监听哪些通知,通知名存在PureNotification中
    {
        return new string[] {
            PureNotification.UPDATE_INFO
        };
    }
    public override void HandleNotification(INotification notification)//重写触发方法
    {
        switch (notification.Name)//通过通知的名称判断需要做什么逻辑,前提是在ListNotificationInterests中添加好了通知名称
        {
            case PureNotification.UPDATE_INFO:
                if(ViewComponent!=null)
                ((XXXView)ViewComponent).UpdateView((XXX)notification.Body);
                break;
        }
    }
    public void SetView(XXXView view类)
    {
        ViewComponent=view类;//为Mediator类设置View脚本,这个是必要步骤
    }
}

Controller层:
这里编写的是业务逻辑,也就是发送通知时触发的方法,所有类都继承SimpleCommand类,并重写Execute方法

public class StartUp_Command : SimpleCommand
{
    public override void Execute(INotification notification)
    {
        Debug.Log(notification.Name + "Excute");
    }
}

Facade管理类脚本:
最重要的就是GameFacade脚本(名字根据自己的项目设定,最好加一个Facade后缀,以表示这是Facade脚本),该脚本继承Facade类,并且需要编写为单例脚本,并重写InitializeFacade方法,里面编写使用RegisterCommand注册Command命令的逻辑

示例脚本:
public class GameFacade : Facade
{
    public static GameFacade Instance
    {
        get
        {
            if(instance==null)
            {
                instance = new GameFacade();
            }
            return instance as GameFacade;
        }
    }
    protected override void InitializeFacade()
    {
        base.InitializeFacade();
        RegisterCommand(PureNotification.START_UP,()=> {
            return new StartUp_Command();
        });
        RegisterCommand(PureNotification.SHOW_PANEL, () => {
            return new ShowPanel_Command();
        });
        RegisterCommand(PureNotification.HIDE_PANEL, () =>
         {
             return new HidePanel_Command();
         });
        RegisterCommand(PureNotification.LEV_UP, () =>
        {
            return new LevUp_Command();
        });
    }

注册完毕后使用SendNotification(PureNotification.通知名);进行发起通知

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值