您的位置:首页 > 移动开发 > Unity3D

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);
}
}

上面的这段代码是中间结构,它把底层封装了起来,并且给了析构函数,这个析构函数可以使我们建立多个中间层,而在销毁的时候定向删除。
<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);
}最后是触发,由于用了中间层,直接使用函数就可以触发了(带检测)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity c#