using UnityEngine;
using System.Collections;
///
/// 消息监听接口
///
public interface IEventListener
{
///
/// 处理消息
///
/// 消息Id
/// 参数1
/// 参数2
/// 是否终止消息派发
bool HandleEvent(int id, object param1, object param2);
///
/// 消息优先级
///
///
int EventPriority();
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EventNode : MonoBehaviour
{
///
/// 节点优先级
///
public int EventNodePriority { set; get; }
///
/// 所有消息集合
///
private Dictionary> mListeners = new Dictionary>();
///
/// 消息节点
///
private List mNodeList = new List();
///
/// 挂载一个消息节点到当前节点上
///
/// 消息节点
/// 如果当前节点里面已经包含要添加的这个节点那么返回false
public bool AttachEventNode(EventNode node)
{
if (node == null)
{
return false;
}
if (mNodeList.Contains(node))
{
return false;
}
int pos = 0;
for (int i = 0; i < mNodeList.Count;i++ )
{
if (node.EventNodePriority > mNodeList[i].EventNodePriority)
{
break;
}
pos++;
}
mNodeList.Insert(pos,node);
return true;
}
///
/// 卸载一个消息节点
///
/// 消息节点
/// 如果节点不存在那么返回false
public bool DetachEventNode(EventNode node)
{
if (!mNodeList.Contains(node))
{
return false;
}
mNodeList.Remove(node);
return true;
}
///
/// 挂载一个消息监听器到当前的消息节点
///
/// 消息ID
/// 消息监听器
/// 当前消息节点已经挂载了这个消息监听器那么返回false
public bool AttachEventListener(int key,IEventListener listener)
{
if (listener == null)
{
return false;
}
if (!mListeners.ContainsKey(key))
{
mListeners.Add(key,new List() { listener });
return true;
}
if (mListeners[key].Contains(listener))
{
return false;
}
int pos = 0;
for (int i = 0;i< mListeners[key].Count;i++ )
{
if (listener.EventPriority() > mListeners[key][i].EventPriority())
{
break;
}
pos++;
}
mListeners[key].Insert(pos,listener);
return true;
}
///
/// 卸载一个消息节点
///
/// 如果当前消息节点不存在那么返回false
public bool DetachEventListener(int key,IEventListener listener)
{
if (mListeners.ContainsKey(key) && mListeners[key].Contains(listener))
{
mListeners[key].Remove(listener);
return true;
}
return false;
}
public void SendEvent(int key,object param1 = null,object param2 = null)
{
DispatchEvent(key, param1, param2);
}
///
/// 派发消息到子消息节点以及自己节点下的监听器上
///
/// 消息ID
///
///
/// 如果中断消息返回true
private bool DispatchEvent(int key,object param1,object param2)
{
for (int i = 0; i < mNodeList.Count;i++ )
{
if (mNodeList[i].DispatchEvent(key, param1, param2)) return true;
}
return TriggerEvent(key, param1, param2);
}
///
/// 消息触发
///
/// 消息id
///
///
/// 是否中断
private bool TriggerEvent(int key,object param1,object param2)
{
if (!this.gameObject.activeSelf || !this.gameObject.activeInHierarchy || !this.enabled)
{
return false;
}
if (!mListeners.ContainsKey(key))
{
return false;
}
List listeners = mListeners[key];
for (int i = 0; i < listeners.Count; i++)
{
if (listeners[i].HandleEvent(key, param1, param2)) return true;
}
return false;
}
void OnApplicationQuit()
{
mListeners.Clear();
mNodeList.Clear();
}
}
测试:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class EventDef
{
public const int EventTest1 = 1;
public const int EventTest2 = EventTest1 + 1;
public const int ResLoadFinish = 1000;
}
using UnityEngine;
using System.Collections;
public class EventListener : MonoBehaviour,IEventListener
{
// Awake is called when the script instance is being loaded.
void Awake()
{
if (EventNodeCore.Instance)
{
EventNodeCore.Instance.AttachEventListener(EventDef.EventTest1, this);
}
}
void OnDestroy()
{
if (EventNodeCore.Instance)
{
EventNodeCore.Instance.DetachEventListener(EventDef.EventTest1, this);
}
}
public bool HandleEvent(int id, object param1, object param2)
{
Debug.Log("EventListener.HandleEvent =>" + " id=" + id + "param1=" + param1);
switch(id)
{
case EventDef.EventTest1:
Debug.Log(this.name + "=>" + "HandleEvent EventTest1");
return false;
}
return false;
}
public int EventPriority()
{
return 0;
}
}
using UnityEngine;
using System.Collections;
public class EventListener1 : MonoBehaviour , IEventListener
{
// Awake is called when the script instance is being loaded.
void Awake()
{
if (EventNode1.Instance)
{
EventNode1.Instance.AttachEventListener(EventDef.EventTest1, this);
EventNode1.Instance.AttachEventListener(EventDef.EventTest2, this);
}
}
void OnDestroy()
{
if (EventNode1.Instance)
{
EventNode1.Instance.DetachEventListener(EventDef.EventTest1, this);
EventNode1.Instance.DetachEventListener(EventDef.EventTest2, this);
}
}
// Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.
void Start()
{
}
// Update is called every frame, if the MonoBehaviour is enabled.
void Update()
{
}
public bool HandleEvent(int id, object param1, object param2)
{
Debug.Log("EventListener1." + "HandleEvent => id =" + id + " param1=" + param1);
//return true 消息中断
return false;
}
public int EventPriority()
{
return 1;
}
}
using UnityEngine;
using System.Collections;
public class EventListener2 : MonoBehaviour,IEventListener
{
// Awake is called when the script instance is being loaded.
void Awake()
{
if (EventNode2.Instance)
{
EventNode2.Instance.AttachEventListener(EventDef.EventTest1, this);
}
}
void OnDestroy()
{
if (EventNode2.Instance)
{
EventNode2.Instance.DetachEventListener(EventDef.EventTest1, this);
}
}
public bool HandleEvent(int id, object param1, object param2)
{
Debug.Log("EventListener2.HandleEvent => id =" + id + " param1=" + param1);
return false;
}
public int EventPriority()
{
return 2;
}
}
using UnityEngine;
using System.Collections;
public class EventNode1 : EventNode
{
private static EventNode1 mInstance;
public static EventNode1 Instance
{
get
{
return mInstance;
}
}
void Awake()
{
mInstance = this;
//base.EventNodePriority = 30;
if (EventNodeCore.Instance)
{
EventNodeCore.Instance.AttachEventNode(this);
}
Debug.Log("-------------1");
}
void OnDestroy()
{
if (EventNodeCore.Instance)
{
EventNodeCore.Instance.DetachEventNode(this);
}
}
}
using UnityEngine;
using System.Collections;
public class EventNode2 : EventNode
{
private static EventNode2 mInstance;
public static EventNode2 Instance
{
get
{
return mInstance;
}
}
// Awake is called when the script instance is being loaded.
void Awake()
{
mInstance = this;
base.EventNodePriority = 20;
if (EventNodeCore.Instance)
{
EventNodeCore.Instance.AttachEventNode(this);
}
}
void OnDestroy()
{
if (EventNodeCore.Instance)
{
EventNodeCore.Instance.DetachEventNode(this);
}
}
}
using UnityEngine;
using System.Collections;
public class EventNodeCore : EventNode
{
private static EventNodeCore mInstance;
public static EventNodeCore Instance
{
get
{
return mInstance;
}
}
void Awake()
{
mInstance = this;
Debug.Log("-------------");
}
void OnDestroy()
{
}
void OnGUI()
{
if (GUI.Button(new Rect (0,0,200,50),"EventNodecore.SenEvent"))
{
EventNodeCore.Instance.SendEvent(EventDef.EventTest1, "测试消息发送");
}
if (GUI.Button(new Rect(0, 60, 200, 50), "EventNode1.SenEvent"))
{
EventNode1.Instance.SendEvent(EventDef.EventTest1, "测试消息发送");
EventNode1.Instance.SendEvent(EventDef.EventTest2, "测试消息发送");
}
if (GUI.Button(new Rect(0, 120, 200, 50), "EventNode2.SenEvent"))
{
EventNode2.Instance.SendEvent(EventDef.EventTest1, "测试消息发送");
}
}
}