事件派发管理系统总结

方法一:废话不多说先上代码,后期优化代码

事件管理端

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

            }
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值