コード例 #1
0
        /// <summary>
        /// 分发Message的实现
        /// </summary>
        /// <param name="message"></param>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        /// <param name="messageParams"></param>
        private static void ExecuteMessage(string message, object sender, MessageArgs args, params object[] messageParams)
        {
            if (string.IsNullOrEmpty(message))
            {
                Debug.LogError("[MessageCenter] Argument named 'message' is null or empty.");
                return;
            }

            if (sender == null)
            {
                Debug.LogError("[MessageCenter] Argument named 'sender' is null");
                return;
            }

            // 分发给Controller
            Type type;

            if (s_ControllerDict.TryGetValue(message, out type))
            {
                MessageControllerBase controller = Activator.CreateInstance(type) as MessageControllerBase;
                if (controller != null)
                {
                    controller.ExecuteMessage(message, sender, args, messageParams);
                }
            }

            /*
             * ///// 使用已经创建好的ScriptableObject,Dictionary<string, MessageControllerBase> s_ControllerDict;
             * // MessageControllerBase controller;
             * // if (s_ControllerDict.TryGetValue(message, out controller))
             * // {
             * //     controller.ExecuteMessage(message, sender, args, messageParams);
             * // }
             */

            /*
             * ///// 使用动态创建的ScriptableObject
             * // Type assetType;
             * // if (s_ControllerDict.TryGetValue(message, out assetType))
             * // {
             * //     MessageControllerBase controller = ScriptableObject.CreateInstance(assetType) as MessageControllerBase;
             * //     controller.ExecuteMessage(message, sender, args, messageParams);
             * //     ScriptableObject.Destroy(controller);
             * // }
             */

            // 分发给Handler
            if (s_Handlers.Count > 0)
            {
                for (int i = 0; i < s_Handlers.Count; i++)
                {
                    IMessageHandler handler = s_Handlers[i];
                    if (handler == null)
                    {
                        s_Handlers.RemoveAt(i);
                        i--;
                    }
                    else
                    {
                        handler.ExecuteMessage(message, sender, args, messageParams);
                    }
                }
            }

            // 分发给Listener
            List <MessageListener> listeners;

            if (s_ListenerDict.TryGetValue(message, out listeners))
            {
                for (int i = 0; i < listeners.Count; i++)
                {
                    MessageListener listener = listeners[i];
                    if (listener == null)
                    {
                        listeners.RemoveAt(i);
                        i--;
                        if (listeners.Count == 0)
                        {
                            s_ListenerDict.Remove(message);
                            break;
                        }
                    }
                    else
                    {
                        listener(message, sender, args, messageParams);
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// 让MonoBehaviour可以发送消息
        /// </summary>
        /// <param name="mono"></param>
        /// <param name="message"></param>
        /// <param name="args"></param>
        /// <param name="messageParams"></param>
        public static void SendByMessageCenter(this MonoBehaviour mono, string message, MessageArgs args, params object[] messageParams)
        {
            if (mono == null)
            {
                Debug.LogError("[MessageCenter] Type extention. Argument named 'mono' is null.");
                return;
            }

            ExecuteMessage(message, mono, args, messageParams);
        }
コード例 #3
0
 /// <summary>
 /// 分发Message
 /// </summary>
 /// <param name="message"></param>
 /// <param name="sender"></param>
 /// <param name="args"></param>
 /// <param name="messageParams"></param>
 public static void Send(string message, object sender, MessageArgs args, params object[] messageParams)
 {
     ExecuteMessage(message, sender, args, messageParams);
 }