学习日记:事件总线

using System;
using System.Collections.Generic;

namespace FrameWork.Event
{
    public  class EventEntity
    {
        /// <summary>
        /// 事件链
        /// </summary>
        private static Dictionary<Enum, List<Action<abstractEventArgs>>> eventEntitys = null;

        /// <summary>
        /// 当前事件系统的实例
        /// </summary>
        private static EventEntity entity = null;

        private EventEntity()
        {
            InitEvent();
        }

        /// <summary>
        /// 初始化事件实体
        /// </summary>
        private void InitEvent()
        {
            eventEntitys = new Dictionary<Enum, List<Action<abstractEventArgs>>>();
        }

        /// <summary>
        ///添加事件类型,并返回事件类型
        /// </summary>
        /// <param name="_type">指定枚举项</param>
        /// <returns>事件类型</returns>
        private List<Action<abstractEventArgs>> GetEventList(Enum _type)
        {
            if (!eventEntitys.ContainsKey(_type))//如果没有这个事件类型
            {
                eventEntitys.Add(_type, new List<Action<abstractEventArgs>>());
            }
            return eventEntitys[_type];//返回事件类型
        }

        /// <summary>
        /// 添加该事件类型的委托
        /// </summary>
        /// <param name="_type">指定类型</param>
        /// <param name="action">指定事件</param>
        private void AddEvent(Enum _type, Action<abstractEventArgs> action)
        {
            List<Action<abstractEventArgs>> actions = GetEventList(_type);//获取指定事件类型
            if (!actions.Contains(action)) //如果事件类型里面没有这个委托
            {
                actions.Add(action);//则添加
            }
        }

        /// <summary>
        /// 执行事件类型里的所有委托
        /// </summary>
        /// <param name="args">消息</param>
        private void CallEvent(abstractEventArgs args)
        {
            List<Action<abstractEventArgs>> actions = GetEventList(args.m_Type);//通过消息获取事件类型
            for (int i = actions.Count - 1; i >= 0; --i)//倒序遍历执行该事件类型里的所有委托
            {
                if (null != actions[i])
                {
                    actions[i](args);
                }
            }
        }

        /// <summary>
        /// 删除指定的事件类型,下的委托
        /// </summary>
        /// <param name="_type">指定事件类型</param>
        /// <param name="action">事件类型下的指定委托</param>
        private void DelEvent(Enum _type, Action<abstractEventArgs> action)
        {
            List<Action<abstractEventArgs>> actions = GetEventList(_type);
            if (actions.Contains(action))
            {
                actions.Remove(action);
            }
        }

        /// <summary>
        ///遍历所有事件类型,删除所有对应委托
        /// </summary>
        /// <param name="action">指定的委托</param>
        private void DelEvent(Action<abstractEventArgs> action)
        {
            if (eventEntitys.Count > 0)
            {
                foreach (List<Action<abstractEventArgs>> actions in eventEntitys.Values)
                {
                    if (actions.Contains(action))
                    {
                        actions.Remove(action);
                    }
                }
            }
        }

        #region//--------------------------StaticFunction-------------------------------

        /// <summary>
        /// 添加事件监听
        /// </summary>
        /// <param name="_type">事件类型</param>
        /// <param name="action">委托</param>
        public static void AddListener(Enum _type, Action<abstractEventArgs> action)
        {
            if (entity != null)//事件中心实体存在
            {
                entity.AddEvent(_type, action);
            }
            else
            {
                entity = new EventEntity();//初始化事件中心实体
                entity.AddEvent(_type, action);
            }
        }

        /// <summary>
        /// 移除事件监听
        /// </summary>
        /// <param name="_type">事件类型</param>
        /// <param name="action">事件</param>
        public static void DelListener(Enum _type, Action<abstractEventArgs> action)
        {
            if (null != entity)
            {
                entity.DelEvent(_type, action);//移除对应事件类型的对应委托
            }
        }

        /// <summary>
        /// 移除事件监听
        /// </summary>
        /// <param name="_type">事件类型</param>
        /// <param name="action">事件</param>
        public static void DelListener(Action<abstractEventArgs> action)
        {
            if (null != entity)
            {
                entity.DelEvent(action);//移除所有事件类型下的对应委托
            }
        }

        /// <summary>
        /// 执行事件类型里的所有委托
        /// </summary>
        /// <param name="args">事件消息</param>
        public static void EventDispatch(abstractEventArgs args)
        {
            if (null != entity)
            {
                entity.CallEvent(args);
            }
        }

        /// <summary>
        /// 移除所有事件
        /// </summary>
        public static void RemoveAllListener()
        {
            if (null != entity)
            {
                entity.InitEvent();//初始化事件实体清空
            }
        }

        #endregion//--------------------------StaticFunction-------------------------------
    }
}
===================================================================================
using System;
using UnityEngine;

namespace FrameWork.Event
{
    /// <summary>
    /// 事件信息类基类
    /// </summary>
    public abstract class abstractEventArgs
    {
        public readonly Enum m_Type;//事件类型
        public readonly GameObject sender;//事件发送者

        public abstractEventArgs(Enum _t, GameObject _sender)
        {
            m_Type = _t;
            sender = _sender;
        }
    }
}
==================================================================================
using FrameWork.Event;
using System;
using System.Collections;
using UnityEngine;

namespace EventSample
{
    /// <summary>
    /// 游戏中途事件
    /// </summary>
    public enum GameEvent
    {
        GamePauseEvent,//游戏暂停事件
        GameResumeEvent,//游戏恢复事件
        GameSenceLaodEvent,//场景加载事件
        GameSenceUnloadEvent,//场景卸载事件
    }

    //封装消息类
    public class GameEventArg : abstractEventArgs
    {
        //。。。。。。。。。
        public GameEventArg(GameEvent _t, GameObject _sender):base( _t, _sender)//可以定义需要传入
        {
            //。。。。。。
        }
    }

    public class EventTest_01 : MonoBehaviour
    {
        private void OnEnable()
        {
            // 向事件系统注册监听器
            EventEntity.AddListener(GameEvent.GamePauseEvent, OnGamePause);
            EventEntity.AddListener(GameEvent.GameResumeEvent, OnGameResume);
        }

        private void OnDisable()
        {
            // 从事件系统注销监听器
            EventEntity.DelListener(GameEvent.GamePauseEvent, OnGamePause);
            EventEntity.DelListener(GameEvent.GameResumeEvent, OnGameResume);
        }

        private void OnGameResume(abstractEventArgs args)
        {
            Debug.Log("游戏恢复");
        }

        private void OnGamePause(abstractEventArgs args)
        {
            Debug.Log("游戏暂停");
        }

        void Start()
        {
            // 发送消息
            StartCoroutine(SendMessage());
        }

        // 发送消息
        private IEnumerator  SendMessage()
        {
            yield return new WaitForSeconds(2);
            // 发送游戏暂停事件
            EventEntity.EventDispatch(new GameEventArg(GameEvent.GamePauseEvent, this.gameObject));
            yield return new WaitForSeconds(2);
            // 发送游戏恢复事件
            EventEntity.EventDispatch(new GameEventArg(GameEvent.GameResumeEvent, this.gameObject));
        }
    }



    /*    /// <summary>
        /// 鼠标消息类 test
        /// </summary>
        public class StylusEventArgs : abstractEventArgs
        {
            /// <summary>
            /// 光标下的游戏对象
            /// </summary>
            public readonly GameObject selected;

            /// <summary>
            /// 光标与游戏对象的触碰点
            /// </summary>
            public readonly Vector3 position = default(Vector3);

            /// <summary>
            /// 鼠标或触笔的按键ID
            /// </summary>
            public readonly int buttonID;

            /// <summary>
            /// 鼠标或者触笔事件
            /// </summary>
            /// <param name="_t">事件类型</param>
            /// <param name="_sender">事件发送者</param>
            /// <param name="_selected">选择对象</param>
            /// <param name="_position">鼠标位置</param>
            /// <param name="_buttonID">按钮ID</param>
            public StylusEventArgs(StylusEvent _t, GameObject _sender, GameObject _selected, int _buttonID = -1, Vector3 _position = default(Vector3)) : base(_t, _sender)
            {
                selected = _selected;
                buttonID = _buttonID;
                position = _position;
            }
        }

        // 事件系统的使用方法示例
        public class EventTest_01 : MonoBehaviour
        {
            private void OnEnable()
            {
                // 向事件系统注册监听器
                EventEntity.AddListener(StylusEvent.Press, OnStylusPress);
                EventEntity.AddListener(StylusEvent.Enter, OnStylusEnter);

            }

            private void OnDisable()
            {
                // 从事件系统注销监听器
                EventEntity.DelListener(StylusEvent.Press, OnStylusPress);
                EventEntity.DelListener(StylusEvent.Enter, OnStylusEnter);
            }

            private void OnDestroy()
            {
                // 移除所有事件监听器
                EventEntity.RemoveAllListener();
            }

            private void OnStylusEnter(abstractEventArgs args)
            {
                StylusEventArgs stylusArgs = args as StylusEventArgs;
                if (stylusArgs != null)
                {
                    Debug.Log("触笔抬起事件");
                    stylusArgs.sender.GetComponent<Renderer>().material.color = Color.green;
                    stylusArgs.selected.GetComponent<Renderer>().material.color = Color.green;
                }
            }

            private void OnStylusPress(abstractEventArgs args)
            {
                // 响应触笔按下事件
                StylusEventArgs stylusArgs = args as StylusEventArgs;
                if (stylusArgs != null)
                {
                    Debug.Log("触笔按下事件");
                    stylusArgs.sender.GetComponent<Renderer>().material.color = Color.red;//发送者.的对象
                    stylusArgs.selected.GetComponent<Renderer>().material.color = Color.red;//接收者.对象
                }
            }


            public GameObject CubeSender;
            public GameObject Cubeselected;

            private void Update()
            {
                if (Input.GetMouseButtonDown(0))
                {
                    EventEntity.EventDispatch(new StylusEventArgs(StylusEvent.Press, CubeSender, Cubeselected));
                }
                if (Input.GetMouseButtonUp(0))
                {
                    EventEntity.EventDispatch(new StylusEventArgs(StylusEvent.Enter, CubeSender, Cubeselected));
                }
            }
        }*/
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值