UNITY之PureMvc

来源:互联网 发布:淘宝卖假货美妆店铺 编辑:程序博客网 时间:2024/06/05 16:32
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
    void Start(){
        new TestFacade (gameObject);
    }

}

using UnityEngine;

using System.Collections;
using PureMVC.Patterns;
public class TestFacade : Facade {
    public TestFacade(GameObject canvas){
        //给TestCommand注册拦截LevelUp通知
        RegisterCommand (NotificationConstant.LevelUp,typeof(TestCommand));
        //注册中介和代理
        RegisterMediator (new TestMediator(canvas));
        RegisterProxy (new TestProxy ());
    }
}

using UnityEngine;
using System.Collections;
using PureMVC.Patterns;
public class TestCommand : SimpleCommand {
    public new const string NAME="TestCommand";
    //只对LevelUp通知拦截,由TestFacade注册的
    public override void Execute (PureMVC.Interfaces.INotification notification)
    {
        TestProxy proxy = (TestProxy)Facade.RetrieveProxy (TestProxy.NAME);
        proxy.ChangeLevel (1);
    }
}

using UnityEngine;
using System.Collections;
using PureMVC.Patterns;
using UnityEngine.UI;
using System.Collections.Generic;
public class TestMediator : Mediator {
    public new const string NAME="TestMediator";
    //View
    private Text levelText;
    private Button levelButton;
    public TestMediator(GameObject root):base(NAME){
        levelText = GameUtility.GetChildComponent<Text> (root, "Text/LevelText");
        levelButton = GameUtility.GetChildComponent<Button> (root,"LevelUpButton");
        //给Button添加Onclick操作处理
        levelButton.onClick.AddListener (OnClickLevelUpButton);
    }
    public void OnClickLevelUpButton(){
        //发送LevelUp通知,由TestCommand拦截并执行Excute方法
        SendNotification (NotificationConstant.LevelUp);
    }
    //添加感兴趣的通知,只要有通知发出就会拿遍历list去匹配,
    //匹配到则调用下面的HandleNotification函数
    //进行处理通知
    public override System.Collections.Generic.IList<string> ListNotificationInterests ()
    {
        IList<string> list = new List<string> ();
        list.Add (NotificationConstant.LevelChange);
        return list;
    }
    //处理通知
    public override void HandleNotification (PureMVC.Interfaces.INotification notification)
    {
        Debug.Log (notification.Name+"  "+NotificationConstant.LevelChange);
        //对相应的通知做相应的处理
        switch (notification.Name) {
        //对LevelChange通知处理过程
        case NotificationConstant.LevelChange:
            CharacterInfo ci = notification.Body as CharacterInfo;
            Debug.Log (ci.Level);
            levelText.text = ci.Level.ToString ();
            break;
        default:
            break;
        }
    }
}

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 ChangeLevel(int change){
        Data.Level += change;
        SendNotification (NotificationConstant.LevelChange, Data);
    }
}


0 0
原创粉丝点击