【Unity】消息系统

介绍

封装的消息机制

使用

注册消息

Reg(MessageType messageType, MessageDelHandle handle);
messageType : 注册消息类型
handle :消息传入时回调方法(该方法需拥有参数Message

注销消息

UnReg(MessageType messageType, MessageDelHandle handle);
messageType : 注销消息类型
handle :消息传入时回调方法(该方法需拥有参数Message

发送消息

SendMessage(int messageType, object body = null);
messageType : 发送消息类型
body :发送消息携带参数

代码

Message

    /// <summary>
    /// 消息类
    /// </summary>
    public class Message{
        public int Type //发送的消息类型
        {
            get;
            private set;
        }

        public object Body //消息主体
        {
            get;
            private set;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="type">消息类型</param>
        public Message(int type){
            Type = type;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="type">消息类型</param>
        /// <param name="body">消息体</param>
        public Message(int type, object body){
            Type = type;
            Body = body;
        }
    }

MessageType

    /// <summary>
    /// 消息的类型枚举
    /// </summary>
    public enum MessageType {
    }

MessageManager

    /// <summary>
    /// 消息中心
    /// </summary>
    public class MessageManager : Singleton<MessageManager> {

        // 消息委托
        public delegate void MessageDelHandle(Message message);

        // 消息字典
        private readonly Dictionary<int, MessageDelHandle> messageMap = new Dictionary<int, MessageDelHandle>();

        /// <summary>
        /// 构造函数
        /// </summary>
        public MessageManager() {
        }

        /// <summary>
        /// 注册监听
        /// </summary>
        public void Reg(int messageType, MessageDelHandle handle) {
            if (handle == null) {
                return;
            }

            // 把事件添加到对应的委托中
            messageMap.TryGetValue(messageType, out MessageDelHandle myHandle);
            messageMap[messageType] = (MessageDelHandle)Delegate.Combine(myHandle, handle);
        }

        /// <summary>
        /// 移除监听
        /// </summary>
        public void UnReg(int messageType, MessageDelHandle handle) {
            if (handle == null) {
                return;
            }
            messageMap[messageType] = (MessageDelHandle)Delegate.Remove(messageMap[messageType], handle);
        }

        /// <summary>
        /// 清空消息
        /// </summary>
        public void Clear() {
            messageMap.Clear();
        }

        public void SendMessage(int messageType, object body = null) {
            if (messageMap.TryGetValue(messageType, out var handle)) {
                Message evt = new Message(messageType, body);
                try {
                    if (handle != null) {
                        handle(evt);
                    }
                }
                catch (Exception e) {
                    Debug.LogError(string.Format("发送消息:{0}, {1}, {2}, {3}", evt.Type.ToString(), e.Message,
                        e.StackTrace, e));
                    throw;
                }
            }
        }

        #region MessageType

        public void Reg(MessageType messageType, MessageDelHandle handle) {
            //Debug.Log($"注册:{messageType}消息");
            Reg((int)messageType, handle);
        }

        public void UnReg(MessageType messageType, MessageDelHandle handle) {
            //Debug.Log($"注销:{messageType}消息");
            UnReg((int)messageType, handle);
        }

        public void SendMessage(MessageType messageType, object body = null) {
            SendMessage((int)messageType, body);
        }

        #endregion
    }

需实现单例 继承Singleton类

posted @ 2024-02-17 11:15  星空探险家  阅读(67)  评论(0编辑  收藏  举报