事件系统模板

通用的事件系统,基本满足了大部分需求,也方便拓展。

using System;
using System.Collections.Generic;
using UnityEngine;

namespace GameClient
{
    public class ClientEventNode
    {
        public EUIEventID                       id;
        public ClientEventSystem.UIEventHandler handle;
    }

    class UIEventSystem : ClientEventSystem
    {
        public static UIEventSystem ms_instance = new UIEventSystem();
        public static UIEventSystem GetInstance()
        {
            return ms_instance;
        }
    }

    class GlobalEventSystem : ClientEventSystem
    {
        public static GlobalEventSystem ms_instance = new GlobalEventSystem();
        public static GlobalEventSystem GetInstance()
        {
            return ms_instance;
        }
    }

    public class UIEvent
    {
        public EUIEventID EventID;
        public object Param1;
        public object Param2;
        public object Param3;
        public object Param4;
        public bool IsUsing = false;

        public void Initialize()
        {
            Param1 = null; 
            Param2 = null; 
            Param3 = null;
            Param4 = null;
        }
    }


    public class ClientEventSystem
    {
        public delegate void UIEventHandler(UIEvent uiEvent);

        protected Dictionary<EUIEventID, List<UIEventHandler>> m_eventProcessors = new Dictionary<EUIEventID, List<UIEventHandler>>();
        protected List<UIEvent> m_eventBuffers = new List<UIEvent>();

        private bool bLock = false;

        private void Clear()
        {
            m_eventProcessors.Clear();
            m_eventBuffers.Clear();
        }

        private UIEvent GetIdleUIEvent()
        {
            UIEvent uiEvent;
            for (int i = 0; i < m_eventBuffers.Count; i++)
            {
                uiEvent = m_eventBuffers[i];
                if (uiEvent.IsUsing == false)
                {
                    uiEvent.Initialize();
                    uiEvent.IsUsing = true;
                    return uiEvent;
                }
            }

            uiEvent = new UIEvent();
            m_eventBuffers.Add(uiEvent);
            uiEvent.Initialize();
            uiEvent.IsUsing = true;
            return uiEvent;
        }

        private void _HandleUIEvent(UIEvent uiEvent)
        {
            if (uiEvent != null)
            {
                try
                {
                    List<UIEventHandler> eventHandlers;

                    if (m_eventProcessors != null)
                    {
                        m_eventProcessors.TryGetValue(uiEvent.EventID, out eventHandlers);

                        if (eventHandlers != null)
                        {
                            List<UIEventHandler> temp = new List<UIEventHandler>(eventHandlers);
                            for (int i = 0; i < temp.Count; ++i)
                            {
                                temp[i](uiEvent);
                            }
                        }
                    }

                    uiEvent.IsUsing = false;
                }
                catch (Exception e)
                {
                    Debug.LogError(e.ToString());
                }
            }
            else
            {
                Debug.LogError("uiEvent is null in [_HandleUIEvent].");
            }
        }

        #region 对外接口
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="id"></param>
        /// <param name="eventHandler"></param>
        public void RegisterEventHandler(EUIEventID id, UIEventHandler eventHandler)
        {
            if (!m_eventProcessors.ContainsKey(id))
            {
                m_eventProcessors.Add(id, new List<UIEventHandler>());
            }

            List<UIEventHandler> eventHandlers = m_eventProcessors[id];
            if (!eventHandlers.Contains(eventHandler))
            {
                eventHandlers.Add(eventHandler);
            }
        }

        /// <summary>
        /// 反注册
        /// </summary>
        /// <param name="id"></param>
        /// <param name="eventHandler"></param>
        public void UnRegisterEventHandler(EUIEventID id, UIEventHandler eventHandler)
        {
            List<UIEventHandler> eventHandlers;
            m_eventProcessors.TryGetValue(id, out eventHandlers);
            if (eventHandlers != null)
            {
                eventHandlers.Remove(eventHandler);
            }
        }

        /// <summary>
        /// 抛出事件
        /// </summary>
        /// <param name="id"></param>
        /// <param name="param1"></param>
        /// <param name="param2"></param>
        /// <param name="param3"></param>
        /// <param name="param4"></param>
        public static void SendUIEventEx(EUIEventID id, object param1 = null, object param2 = null, object param3 = null, object param4 = null)
        {
#if !LOGIC_SERVER


            var sys = UIEventSystem.GetInstance();

            if (sys == null)
            {
                return;
            }

            UIEvent uiEvent = sys.GetIdleUIEvent();
            uiEvent.EventID = id;
            uiEvent.Param1 = param1;
            uiEvent.Param2 = param2;
            uiEvent.Param3 = param3;
            uiEvent.Param4 = param4;
            sys._HandleUIEvent(uiEvent);

#endif

        }

        /// <summary>
        /// 抛出事件
        /// </summary>
        /// <param name="id"></param>
        /// <param name="param1"></param>
        /// <param name="param2"></param>
        /// <param name="param3"></param>
        /// <param name="param4"></param>
        public void SendUIEvent(EUIEventID id, object param1 = null, object param2 = null, object param3 = null, object param4 = null)
        {
#if !LOGIC_SERVER

            UIEvent uiEvent = GetIdleUIEvent();
            uiEvent.EventID = id;
            uiEvent.Param1 = param1;
            uiEvent.Param2 = param2;
            uiEvent.Param3 = param3;
            uiEvent.Param4 = param4;
            _HandleUIEvent(uiEvent);
#endif
        }

        /// <summary>
        /// 抛出事件
        /// </summary>
        /// <param name="uiEvent"></param>
        public void SendUIEvent(UIEvent uiEvent)
        {
            _HandleUIEvent(uiEvent);
        }
        #endregion

        #region 测试
        public void PopupLeakedEvents()
        {
            Debug.LogWarning("PopupLeakedEvents");
            var enumerator = m_eventProcessors.GetEnumerator();
            while (enumerator.MoveNext())
            {
                if (enumerator.Current.Value.Count > 0)
                {
                    var list = enumerator.Current.Value;
                    for (int i = 0; i < list.Count; ++i)
                    {
                        Debug.LogWarningFormat("这个倒底是怎么发生的呢? 真是奇怪 ? Leaked UIEventID = [" + enumerator.Current.Key.ToString() + "]" +
    "Leaked UIEventValue = [" + list[i].Method.ToString() + "]");
                    }
                }
            }
        }
        #endregion
    }
}
//定义的事件枚举
namespace GameClient
{
    public enum EUIEventID
    {
        Invalid = -1,
        TestEvent = 1,
    }
}

测试功能,随便建两个脚本

using UnityEngine;

namespace GameClient
{
    public class TestDataClass
    {
        public int testData;
    }

    public class TestEventSystem : MonoBehaviour
    {
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.R))
            {
                //注册
                UIEventSystem.GetInstance().RegisterEventHandler(EUIEventID.TestEvent, ShowUpgrade);
            }
            else if (Input.GetKeyDown(KeyCode.U))
            {
                //反注测
                UIEventSystem.GetInstance().UnRegisterEventHandler(EUIEventID.TestEvent, ShowUpgrade);
            }
        }

        //使用
        public void ShowUpgrade(UIEvent uiEvent)
        {
            TestDataClass roleid = (TestDataClass)uiEvent.Param1;
            Debug.Log("事件触发,testData:" + roleid.testData);
        }
    }
}
using UnityEngine;

namespace GameClient
{
    public class TestTrigger : MonoBehaviour
    {
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.E))
            {
                UIEventSystem.GetInstance().SendUIEvent(EUIEventID.TestEvent, new TestDataClass() { testData = 12 });
            }
        }
    }
}

运行Unit有,按R键注册事件,按U键反注册事件,按E触发事件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值