1.事件的基本行为:
先提供一个接口,在后期会和编辑器交互。
public interface IDispatcher {
void addEventListener(string eventName, Func<Void> handler);
void addEventListener(int eventID, Func<Void> handler);
void addEventListener(string eventName, Func<CEvent, Void> handler);
void addEventListener(int eventID, Func<CEvent, Void> handler);
void removeEventListener(string eventName, Func<Void> handler);
void removeEventListener(int eventID, Func<Void> handler);
void removeEventListener(string eventName, Func<CEvent, Void> handler);
void removeEventListener(int eventID, Func<CEvent, Void> handler);
void dispatchEvent(int eventID);
void dispatchEvent(int eventID, object data);
void dispatchEvent(string eventName);
void dispatchEvent(string eventName, object data);
void dispatch(int eventID);
void dispatch(int eventID, object data);
void dispatch(string eventName);
void dispatch(string eventName, object data);
void clearEvents();
}
2.提供相关继承与该类的接口:
排版接口
interface IEventGraphNode {
EventGraphAlignment alignment {get;}
}
排版枚举
public enum EventGraphAlignment {
None,
Left,
Middle,
Right
}
using UnityEngine;
using System;
using System.Collections.Generic;
public class EventDispatcher : MonoBehaviour, IDispatcher, IEventGraphNode {
//无参数数据的函数
private Dictionary<int, List<Func<Void>>> listners = new Dictionary<int, List<Func<Void>>>();
//有参数数据的函数
private Dictionary<int, List<Func<CEvent, Void>>> dataListners = new Dictionary<int, List<Func<CEvent,Void>>>();
private EventGraphAlignment _aligment = EventGraphAlignment.None;
//--------------------------------------
// ADD LISTENER'S
//--------------------------------------
public void addEventListener(string eventName, Func<Void> handler) {
addEventListener(eventName.GetHashCode(), handler, eventName);
}
public void addEventListener(int eventID, Func<Void> handler) {
addEventListener(eventID, handler, eventID.ToString());
}
private void addEventListener(int eventID, Func<Void> handler, string eventGraphName) {
#if UNITY_EDITOR
EventGraphManager.addDispatcher(this, eventGraphName, handler.Method, handler.Target);
#endif
if(listners.ContainsKey(eventID)) {
listners[eventID].Add(handler);
} else {
List<Func<Void>> handlers = new List<Func<Void>>();
handlers.Add(handler);
listners.Add(eventID, handlers);
}
}
public void addEventListener(string eventName, Func<CEvent, Void> handler) {
addEventListener(eventName.GetHashCode(), handler, eventName);
}
public void addEventListener(int eventID, Func<CEvent, Void> handler) {
addEventListener(eventID, handler, eventID.ToString());
}
private void addEventListener(int eventID, Func<CEvent, Void> handler, string eventGraphName) {
#if UNITY_EDITOR
EventGraphManager.addDispatcher(this, eventGraphName, handler.Method, handler.Target);
#endif
if(dataListners.ContainsKey(eventID)) {
dataListners[eventID].Add(handler);
} else {
List<Func<CEvent, Void>> handlers = new List<Func<CEvent, Void>>();
handlers.Add(handler);
dataListners.Add(eventID, handlers);
}
}
//--------------------------------------
// REMOVE LISTENER'S
//--------------------------------------
public void removeEventListener(string eventName, Func<Void> handler) {
removeEventListener(eventName.GetHashCode(), handler, eventName);
}
public void removeEventListener(int eventID, Func<Void> handler) {
removeEventListener(eventID, handler, eventID.ToString());
}
public void removeEventListener(int eventID, Func<Void> handler, string eventGraphName) {
#if UNITY_EDITOR
EventGraphManager.removeListener(this, eventGraphName, handler.Method, handler.Target);
#endif
if(listners.ContainsKey(eventID)) {
List<Func<Void>> handlers = listners[eventID];
handlers.Remove(handler);
if(handlers.Count == 0) {
listners.Remove(eventID);
}
}
}
public void removeEventListener(string eventName, Func<CEvent, Void> handler) {
removeEventListener(eventName.GetHashCode(), handler, eventName);
}
public void removeEventListener(int eventID, Func<CEvent, Void> handler) {
removeEventListener(eventID, handler, eventID.ToString());
}
public void removeEventListener(int eventID, Func<CEvent, Void> handler, string eventGraphName) {
#if UNITY_EDITOR
EventGraphManager.removeListener(this, eventGraphName, handler.Method, handler.Target);
#endif
if(dataListners.ContainsKey(eventID)) {
List<Func<CEvent, Void>> handlers = dataListners[eventID];
handlers.Remove(handler);
if(handlers.Count == 0) {
dataListners.Remove(eventID);
}
}
}
public void dispatchEvent(string eventName) {
dispatch(eventName.GetHashCode(), null, eventName);
}
public void dispatchEvent(string eventName, object data) {
dispatch(eventName.GetHashCode(), data, eventName);
}
public void dispatchEvent(int eventID) {
dispatch(eventID, null, string.Empty);
}
public void dispatchEvent(int eventID, object data) {
dispatch(eventID, data, string.Empty);
}
//--------------------------------------
// DISPATCH I2
//--------------------------------------
public void dispatch(string eventName) {
dispatch(eventName.GetHashCode(), null, eventName);
}
public void dispatch(string eventName, object data) {
dispatch(eventName.GetHashCode(), data, eventName);
}
public void dispatch(int eventID) {
dispatch(eventID, null, string.Empty);
}
public void dispatch(int eventID, object data) {
dispatch(eventID, data, string.Empty);
}
//--------------------------------------
// PRIVATE DISPATCH I2
//--------------------------------------
private void dispatch(int eventID, object data, string eventName) {
string eventGraphName = null;
#if UNITY_EDITOR
if(eventName.Equals(string.Empty)) {
eventGraphName = eventID.ToString();
} else {
eventGraphName = eventName;
}
EventGraphManager.registerEventDispatch(this, eventGraphName);
#endif
CEvent e = new CEvent(eventID, eventName, data, this);
if(dataListners.ContainsKey(eventID)) {
List<Func<CEvent, Void>> handlers = cloenArray(dataListners[eventID]);
int len = handlers.Count;
for(int i = 0; i < len; i++) {
if(e.canBeDisptached(handlers[i].Target)) {
handlers[i](e);
#if UNITY_EDITOR
EventGraphManager.registerEventDispatch(this, eventGraphName, handlers[i].Method, handlers[i].Target);
#endif
}
}
}
if(listners.ContainsKey(eventID)) {
List<Func<Void>> handlers = cloenArray(listners[eventID]);
int len = handlers.Count;
for(int i = 0; i < len; i++) {
if(e.canBeDisptached(handlers[i].Target)) {
handlers[i]();
}
#if UNITY_EDITOR
EventGraphManager.registerEventDispatch(this, eventGraphName, handlers[i].Method, handlers[i].Target);
#endif
}
}
}
//--------------------------------------
// PUBLIC METHODS
//--------------------------------------
public void clearEvents() {
listners.Clear();
dataListners.Clear();
}
//--------------------------------------
// GET / SET
//--------------------------------------
public EventGraphAlignment alignment {
get {
return _aligment;
}
set {
_aligment = value;
}
}
//--------------------------------------
// PRIVATE METHODS
//--------------------------------------
private List<Func<Void>> cloenArray(List<Func<Void>> list) {
List<Func<Void>> nl = new List<Func<Void>>();
int len = list.Count;
for(int i = 0; i < len; i++) {
nl.Add(list[i]);
}
return nl;
}
private List<Func<CEvent, Void>> cloenArray(List<Func<CEvent, Void>> list) {
List<Func<CEvent, Void>> nl = new List<Func<CEvent, Void>>();
int len = list.Count;
for(int i = 0; i < len; i++) {
nl.Add(list[i]);
}
return nl;
}
//--------------------------------------
// DestroyCEvent
//--------------------------------------
protected virtual void OnDestroy() {
#if UNITY_EDITOR
EventGraphManager.removeAllNodeListners(this);
EventGraphManager.removeNode(this);
#endif
clearEvents();
}
}
后面总结一下。