事件订阅发送

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

    public class EventArg 
    {

        protected int m_EventId = 0;
        protected object m_EventParam = null;

        public EventArg(int eventId)
        {
            m_EventId = eventId;

        }

        public EventArg(int eventId, object eventParam)
        {
            m_EventId = eventId;
            m_EventParam = eventParam;
        }

        public int EventID
        {
            get { return m_EventId; }
        }
        public object EventParam
        {
            get { return m_EventParam; }
            set { m_EventParam = value; }
        }
    }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

    public class EventDispatcher
    {
        
        public delegate void EventHandler(EventArg evt);

        private Dictionary<int, EventHandler> mEventHandlerPool = new Dictionary<int, EventHandler>();
        
        public void AddEventListener(int eventId, EventHandler handler)
        {
            EventHandler evtHandler = null;
            if (mEventHandlerPool.ContainsKey(eventId))
            {
                evtHandler = mEventHandlerPool[eventId];
                evtHandler += handler;
                mEventHandlerPool[eventId] = evtHandler;
            }
            else mEventHandlerPool.Add(eventId, handler);

            evtHandler = null;
        }
        
        public void RemoveEventListener(int eventId, EventHandler handler)
        {
            EventHandler evtHandler = null;
            if (mEventHandlerPool.ContainsKey(eventId))
            {
                evtHandler = mEventHandlerPool[eventId];
                evtHandler -= handler;
                mEventHandlerPool[eventId] = evtHandler;
                if (evtHandler == null) mEventHandlerPool.Remove(eventId);
            }

            evtHandler = null;
        }
        
        public void RemoveEventListener(int eventId)
        {
            EventHandler evtHandler = null;
            if (mEventHandlerPool.ContainsKey(eventId))
            {
                evtHandler = mEventHandlerPool[eventId];
                mEventHandlerPool.Remove(eventId);
            }
            evtHandler = null;
        }
       
        public void DispatchEvent(EventArg evt)
        {
            if (evt != null && mEventHandlerPool.ContainsKey(evt.EventID))
            {
                EventHandler evtHandler = mEventHandlerPool[evt.EventID];
                if (evtHandler != null)
                {
                    evtHandler(evt);
                }
            }
        }

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

public class EventComponent : Singleton<EventComponent>
    {
        private const string EVENTARGTYPE = "SYSTEM";
        private Dictionary<string, EventDispatcher> eventDispatcgers = new Dictionary<string, EventDispatcher>();

        private Queue<EventSender> eventQueue = new Queue<EventSender>();

        public class EventSender
        {
           /// <summary>
           /// 事件发送器
           /// </summary>
            public EventDispatcher dispatcher { get; private set; }
            /// <summary>
            /// 事件内容
            /// </summary>
            public EventArg eventarg { get; private set; }
            /// <summary>
            /// 事件
            /// </summary>
            /// <param name="eventDispatcher"></param>
            /// <param name="arg"></param>
            public EventSender(EventDispatcher eventDispatcher, EventArg arg)
            {
                dispatcher = eventDispatcher;
                eventarg = arg;

            }


        }
        
        /// <summary>
        /// 添加事件监听
        /// </summary>
        /// <param name="EventType">自定义类型</param>
        /// <param name="eventId">事件ID</param>
        /// <param name="handler">委托</param>
        public void AddEventListener(string eventType, int eventId, EventDispatcher.EventHandler handler)
        {
            EventDispatcher dispatcher = null;
            if (eventDispatcgers.ContainsKey(eventType))
            {
                dispatcher = eventDispatcgers[eventType];
            }
            else
            {
                dispatcher = new EventDispatcher();
                eventDispatcgers.Add(eventType, dispatcher);
            }

            dispatcher.AddEventListener(eventId, handler);
        }
        /// <summary>
        /// 移除事件监听
        /// </summary>
        /// <param name="EventType">自定义事件类型</param>
        /// <param name="eventId">事件ID</param>
        /// <param name="handler">委托</param>
        public void RemoveEventListener(string eventType, int eventId, EventDispatcher.EventHandler handler)
        {
            if (eventDispatcgers.ContainsKey(eventType))
            {
                EventDispatcher dispatcher = eventDispatcgers[eventType];
                dispatcher.RemoveEventListener(eventId, handler);
            }
            else
            {
                Debug.LogError(" 不存在的事件类型 >>  "+ eventType);
            }
        }
        /// <summary>
        /// 移除事件所有监听
        /// </summary>
        /// <param name="EventType">自定义事件类型</param>
        /// <param name="eventId">事件ID</param>
        public void RemoveEventListener(string eventType, int eventId)
        {
            if (eventDispatcgers.ContainsKey(eventType))
            {
                EventDispatcher dispatcher = eventDispatcgers[eventType];
                dispatcher.RemoveEventListener(eventId);
            }
            else
            {
                Debug.LogError(" 不存在的事件类型 >>  " + eventType);
            }
        }
        /// <summary>
        /// 立即抛出事件
        /// </summary>
        /// <param name="eventType">自定义事件类型</param>
        /// <param name="evt">消息</param>
        public void DispatchEventImmediately(string eventType, EventArg evt)
        {
            if (eventDispatcgers.ContainsKey(eventType))
            {
                EventDispatcher dispatcher = eventDispatcgers[eventType];
                dispatcher.DispatchEvent(evt);
            }
            else
            {
                Debug.Log(" 不存在的事件类型 >>  " + eventType);
            }
        }
        /// <summary>
        /// 下一帧抛出事件,线程安全
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="evt"></param>
        public void DispatchEvent(string eventType, EventArg evt)
        {
            if (eventDispatcgers.ContainsKey(eventType))
            {
                EventDispatcher dispatcher = eventDispatcgers[eventType];
                eventQueue.Enqueue(new EventSender(dispatcher,evt));
            }
            else
            {
                Debug.Log(" 不存在的事件类型 >>  " + eventType);
            }
        }

        void Update()
        {
            while (eventQueue.Count > 0)
            {
                EventSender eventSender = eventQueue.Dequeue();
                eventSender.dispatcher.DispatchEvent(eventSender.eventarg);
            }

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

    public abstract class Singleton<T>: MonoBehaviour where T : MonoBehaviour
    {
        
        private static T _instance;
        private static object _lock = new object();
        public static T Instance
        {
            get
            {
                Type _type = typeof(T);
                if (_destroyed)
                {
                    Debug.LogWarningFormat("[Singleton]【{0}】已被标记为销毁,返 Null!", _type.Name);
                    return (T)((object)null);
                }
                lock (_lock)
                {
                    if (_instance == null)
                    {
                        _instance = (T)FindObjectOfType(_type);
                        if (FindObjectsOfType(_type).Length > 1)
                        {
                            Debug.LogErrorFormat("[Singleton]类型【{0}】存在多个实例.", _type.Name);
                            return _instance;
                        }
                        if (_instance == null)
                        {
                            GameObject singleton = new GameObject();
                            _instance = singleton.AddComponent<T>();
                            singleton.name = "(singleton)_" + typeof(T).ToString();

                            DontDestroyOnLoad(singleton);
                            
                           
                        }
                    }
                    return _instance;
                }
            }
        }

        protected virtual void Awake()
        {
            if (_instance != null && _instance.gameObject != gameObject)
            {
                
                if (Application.isPlaying)
                {
                    GameObject.Destroy(gameObject);
                }
                else
                {
                    GameObject.DestroyImmediate(gameObject);
                }
            }
            else
            {
                _instance = GetComponent<T>();
                if (!transform.parent)
                {
                    DontDestroyOnLoad(gameObject);
                }
                OnInit();
            }
        }

        public static void DestroyInstance()
        {
            if (_instance != null)
            {
                GameObject.Destroy(_instance.gameObject);
            }
            _destroyed = true;
            _instance = (T)((object)null);
        }

        
        public static void ClearDestroy()
        {
            DestroyInstance();
            _destroyed = false;
        }

        private static bool _destroyed = false;
        
        public void OnDestroy()
        {
            if (_instance != null && _instance.gameObject == base.gameObject)
            {
                _instance = (T)((object)null);
                _destroyed = true;
            }
        }
        
        public virtual void OnInit()
        {
          
        }

    }
    /// <summary>
    /// 系统消息,消息类型,可拓展
    /// </summary>
    public enum FileOperationType
    {
        NULL = 0,
        /// <summary>
        /// 组件加载完成
        /// </summary>
        异步上传成功= 1,
    }

调用实例
开始运行后,先监听事件
触发事件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值