using System;
using System.Collections.Generic;
public class EventArgs
{
}
public class EventManager
{
static EventManager _instance;
public static EventManager Instance
{
get
{
if (_instance == null)
{
_instance = new EventManager();
}
return _instance;
}
}
public delegate void EventDelegate<T>(T e) where T : EventArgs;
readonly Dictionary<Type, Delegate> _delegates = new Dictionary<Type, Delegate>();
public void AddListener<T>(EventDelegate<T> listener) where T : EventArgs
{
Delegate d;
if (_delegates.TryGetValue(typeof(T), out d))
{
_delegates[typeof(T)] = Delegate.Combine(d, listener);
}
else
{
_delegates[typeof(T)] = listener;
}
}
public void RemoveListener<T>(EventDelegate<T> listener) where T : EventArgs
{
Delegate d;
if (_delegates.TryGetValue(typeof(T), out d))
{
Delegate currentDel = Delegate.Remove(d, listener);
if (currentDel == null)
{
_delegates.Remove(typeof(T));
}
else
{
_delegates[typeof(T)] = currentDel;
}
}
}
public void Raise<T>(T e) where T : EventArgs
{
if (e == null)
{
throw new ArgumentNullException("e");
}
Delegate d;
if (_delegates.TryGetValue(typeof(T), out d))
{
EventDelegate<T> callback = d as EventDelegate<T>;
if (callback != null)
{
callback(e);
}
}
}
}
public class UIEventArgs : EventArgs
{
private bool _isOpen;
public bool IsOpen
{
get
{
return _isOpen;
}
set
{
_isOpen = value;
}
}
}
事件发送端
using UnityEngine;
class Class2 : MonoBehaviour
{
void Send()
{
var arg = new UIEventArgs()
{
IsOpen = true,
};
EventManager.Instance.Raise(arg);
}
void OnGUI()
{
if (GUILayout.Button("Send"))
{
Send();
}
}
}
事件接受端
using UnityEngine;
public class Class1 : MonoBehaviour {
void Start()
{
EventManager.Instance.AddListener<UIEventArgs>(OnReceive);
}
void OnReceive(UIEventArgs e)
{
if (e.IsOpen)
{
print("Name:" + gameObject.name);
}
}
}
方法二:
事件管理端
using System;
using System.Collections.Generic;
namespace MCKinect.Events
{
public enum EventLevel
{
S,
A,
B,
C
}
public struct IdleEventHandler
{
public EventLevel Level { get; private set; }
public Action<int> action { get; private set; }
public IdleEventHandler(EventLevel level, Action<int> action)
{
Level = level;
this.action = action;
}
}
/// <summary>
/// 未抓取物体,也就是处于空间状态事件注册
/// </summary>
public static class KinectEventHandIdle
{
private static List<IdleEventHandler> Values;
public static void AddListener(EventLevel level, Action<int> action)
{
if (Values == null)
Values = new List<IdleEventHandler>();
if (IsHandler(action)) return;
Values.Add(new IdleEventHandler(level, action));
}
/// <summary>
/// 是否已经注册此委托方法
/// </summary>
/// <param name="action"></param>
/// <returns></returns>
public static bool IsHandler(Action<int> action)
{
if (Values == null) return false;
foreach (var item in Values)
{
if (item.action.Equals(action)) return true;
}
return false;
}
public static void RemoveListener(Action<int> action)
{
if (Values == null)
return;
foreach (var item in Values)
{
if (item.action.Equals(action))
{
Values.Remove(item);
break;
}
}
}
public static void SendListener(int handIndex)
{
foreach (var item in Enum.GetValues(typeof(EventLevel)))
{
//try
//{
SendListener((EventLevel)item, handIndex);
//}
//catch (Exception ex)
//{
// throw new Exception("事件异常:" + item.ToString() + "详细信息:" + ex.Message);
// throw;
//}
}
}
public static void SendListener(EventLevel level, int handIndex)
{
if (Values == null) return;
foreach (var item in Values)
{
//if (item.Level.Equals(level))
//{
// if (item.action != null)
// item.action(handIndex);
//}
//try
//{
if (item.Level.Equals(level))
{
if (item.action != null)
item.action(handIndex);
else
{
throw new Exception("这个事件为Null");
}
}
//}
//catch (Exception ex)
//{
// throw new Exception("事件异常:" + item.action.ToString() + "详细信息:" + ex.Message);
//}
}
}
}
}
事件派发管理系统方法一:废话不多说先上代码,后期持续更新事件管理端事件发送端事件接受端方法一:废话不多说先上代码,后期持续更新事件管理端using System;using System.Collections.Generic;public class EventArgs{}public class EventManager{ static EventManager...