Unity使用中间层分发消息通知

本文参考自 http://www.cnblogs.com/neverdie/p/3790879.html

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

// NotificationCenter的拓展类,在这里弄出多个INotificationCenter的子类,
// 分别处理不同的消息转发,便于消息分组
public class NotificationCenter : INotificationCenter
{
    private static INotificationCenter singleton;

    private event EventHandler GameOver;
    private event EventHandler ScoreAdd;

    private NotificationCenter()
        : base()
    {
        // 在这里添加需要分发的各种消息
        eventTable["GameOver"] = GameOver;
        eventTable["ScoreAdd"] = ScoreAdd;
    }

    public static INotificationCenter GetInstance()
    {
        if (singleton == null)
            singleton = new NotificationCenter();
        return singleton;
    }
}

// NotificationCenter的抽象基类
public abstract class INotificationCenter
{

    protected Dictionary<string, EventHandler> eventTable;

    protected INotificationCenter()
    {
        eventTable = new Dictionary<string, EventHandler>();
    }

    // PostNotification -- 将名字为name,发送者为sender,参数为e的消息发送出去
    public void PostNotification(string name)
    {
        this.PostNotification(name, null, EventArgs.Empty);
    }
    public void PostNotification(string name, object sender)
    {
        this.PostNotification(name, sender, EventArgs.Empty);
    }
    public void PostNotification(string name, object sender, EventArgs e)
    {
        if (eventTable[name] != null)
        {
            eventTable[name](sender, e);
        }
    }

    // 添加或者移除了一个回调函数。
    public void AddEventHandler(string name, EventHandler handler)
    {
        eventTable[name] += handler;
    }
    public void RemoveEventHandler(string name, EventHandler handler)
    {
        eventTable[name] -= handler;
    }

}

上面的代码是将底层的功能以单例模式实现,这样我们就有了用来存储所有事件的字典,然后通过这个字典我们可以添加我们需要操作的所有事件句柄(增删改)。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class Observer : MonoBehaviour
{
    private INotificationCenter center;
    private Dictionary<string, EventHandler> handlers;

    void Awake()
    {
        handlers = new Dictionary<string, EventHandler>();
        center = NotificationCenter.GetInstance();
    }

    void OnDestroy()
    {
        foreach (KeyValuePair<string, EventHandler> kvp in handlers)
        {
            center.RemoveEventHandler(kvp.Key, kvp.Value);
        }
    }

    public void AddEventHandler(string name, EventHandler handler)
    {
        center.AddEventHandler(name, handler);
        handlers.Add(name, handler);
    }
    public void DeleteEventHandler(string name, EventHandler handler)
    {
        center.RemoveEventHandler(name, handler);
        handlers.Remove(name);
    }
    public void DoEvent(string name,GameObject sender)
    {
        center.PostNotification(name,sender,EventArgs.Empty);
    }
}

上面的这段代码是中间结构,它把底层封装了起来,并且给了析构函数,这个析构函数可以使我们建立多个中间层,而在销毁的时候定向删除。
<pre name="code" class="csharp">using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;
public class PanelCtrl : MonoBehaviour {
    InputField name;
    InputField score;
	// Use this for initialization
	void Start () {
        //-----绑定消息
        GameObject.Find("Main Camera").GetComponent<Observer>().AddEventHandler("GameOver",OnGameOver);
        GameObject.Find("Main Camera").GetComponent<Observer>().AddEventHandler("ScoreAdd",OnOtherTirgger);
	}
    void OnGameOver(object sender, EventArgs s)
    {
        GameObject temp = (GameObject)sender;
        print(temp.name+"is Over");

    }
    void OnOtherTirgger(object sender, EventArgs s)
    {
        GameObject temp = (GameObject)sender;
        print(temp.name + "is tirgger");
    }

}


 

上面是绑定消息,可以在任意脚本中进行。

        if (Input.GetKeyDown(KeyCode.Q))
        {
            GameObject.Find("Main Camera").GetComponent<Observer>().DoEvent("GameOver", this.gameObject);
        }
        if (Input.GetKeyDown(KeyCode.W))
        {
            GameObject.Find("Main Camera").GetComponent<Observer>().DoEvent("ScoreAdd", this.gameObject);
        }
最后是触发,由于用了中间层,直接使用函数就可以触发了(带检测)。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值