Unity使用中间层分发消息通知
2015-09-29 15:12
393 查看
本文参考自 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);
}
}
上面的这段代码是中间结构,它把底层封装了起来,并且给了析构函数,这个析构函数可以使我们建立多个中间层,而在销毁的时候定向删除。
上面是绑定消息,可以在任意脚本中进行。
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);
}最后是触发,由于用了中间层,直接使用函数就可以触发了(带检测)。
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);
}最后是触发,由于用了中间层,直接使用函数就可以触发了(带检测)。
相关文章推荐
- c#调用COM组件
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- C#中this的用法集锦
- C#.NET获取拨号连接的宽带连接方法
- C#异步绑定数据实现方法
- C#实现AddRange为数组添加多个元素的方法
- C#中Equality和Identity浅析
- C#生成饼形图及添加文字说明实例代码
- C#判等对象是否相等的方法汇总
- C#简单的向量用法实例教程
- C#实现基于链表的内存记事本实例
- C#托管堆对象实例包含内容分析