# 1.前言
Unity自帶消息系統,如SendMessage等,此方法利用的反射,且會反射遊戲物體上的全部組件,對性能不友好。並且因爲參數爲方法名稱,因此若是使用代碼混淆,則會沒法調用 方法,且難以追蹤問題。通常消息發送採用事件或者委託進行。可是對於一些跨線程操做,或者涉及系統底層(通常也再也不主線程)消息時也會更新UI(確切的說是渲染問題)錯誤。因此在此基於腳本update方法,造成一個統一的消息機制。android
# 2.消息系統組成
主要有三部分組成,消息中心、消息處理器和消息通道。
## 2.1 消息中心
此部分主要用來管理消息,包括存儲、註冊等。性能
```csharp
using System;
using System.Collections.Generic;
using UnityEngine;this
namespace MSG
{
public delegate void MessageHandler(Message message);spa
public class MessageCenter
{
#region Instance
private static MessageCenter instance;
private static MessageProcessor messageProcessor;
private static bool instanceCreated = false;線程
public static MessageCenter GetInstance()
{
if (!instanceCreated)
{
instance = new MessageCenter();orm
GameObject processor = new GameObject("MessageProcessor");
messageProcessor = processor.AddComponent<MessageProcessor>();
instanceCreated = true;
}對象
return instance;
}遊戲
private MessageCenter() { }
#endregion事件
private Dictionary<MsgChannel, MessageHandler> messageHandlers = new Dictionary<MsgChannel, MessageHandler>();
private Queue<Message> messageQueue = new Queue<Message>();string
public void BroadcastMessage(Message message)
{
messageQueue.Enqueue(message);
}
public Message PostMessage()
{
Message message = null;
if (messageQueue.Count != 0)
{
message = messageQueue.Dequeue();
}
return message;
}
public void RegisterMessageHandler(MsgChannel type,MessageHandler messageHandler)
{
MessageHandler handler = null;
bool exist = messageHandlers.TryGetValue(type, out handler);
if (!exist)
{
Debug.Log("Add message handler " + messageHandler.Method.Name);
messageHandlers.Add(type, messageHandler);
}
else
{
//若是已經註冊,則不會重複註冊
Delegate[] handlers = handler.GetInvocationList();
if (Array.IndexOf(handlers, messageHandler) == -1)
{
Debug.Log("Plus message handler " + messageHandler.Method.Name);
handler += messageHandler;
messageHandlers[type] = handler;
}
}
}
public void UnregisterMessageHandler(MsgChannel type,MessageHandler messageHandler)
{
MessageHandler handler;
bool exist = messageHandlers.TryGetValue(type, out handler);
if (exist)
{
handler -= messageHandler;
if (handler == null)
{
Debug.Log("Message handler to be unregistered is null now" + messageHandler.Method.Name);
messageHandlers.Remove(type);
}
else
{
Debug.Log("Message handler to be unregistered is unregistered " + messageHandler.Method.Name);
messageHandlers[type] = handler;
}
}
}
public MessageHandler GetMessageHandler(MsgChannel type)
{
MessageHandler handler;
messageHandlers.TryGetValue(type, out handler);
return handler;
}
}
}
```
## 2.2 消息處理器
此部分功能負責發送消息。
```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MSG
{
public class MessageProcessor : MonoBehaviour
{
public bool ProcessorActive = true;
private void ProcessMessages()
{
Message message = MessageCenter.GetInstance().PostMessage();
if (message != null)
{
MessageHandler handler = MessageCenter.GetInstance().GetMessageHandler(message.what);
if (handler != null)
{
handler(message);
}
}
}
private void Update()
{
ProcessMessages();
}
}
}
```
## 2.3 消息通道
消息通道爲枚舉類型,決定了此消息經過什麼通道發送,可自行添加。並經過Message類及其子類傳遞參數
```csharp
using UnityEngine;
namespace MSG
{
public enum MsgChannel
{
NONE = 0,
TEST_MSG1 = 1,
TEST_MSG2 = 2
}
/// <summary>
/// Base Message class imitating android Message class.
/// arg一、arg2 and message are used to store simple values.
/// </summary>
public class Message
{
public MsgChannel what = MsgChannel.NONE;
public int arg1 = 0;
public int arg2 = 0;
public string message;
public Message(MsgChannel what, int arg1 = 0, int arg2 = 0, string message = "")
{
this.what = what;
this.arg1 = arg1;
this.arg2 = arg2;
this.message = message;
}
public Message() { }
}
/// <summary>
/// Extend Message class and carry more infomation
/// </summary>
public class NetworkMessage : Message
{
public Texture2D t2d;
public NetworkMessage(MsgChannel what,Texture2D t2d, int arg1 = 0, int arg2 = 0, string message = "")
:base(what,arg1,arg2,message)
{
this.t2d = t2d;
}
}
}
```
## 2.4 問題標記
MessageCenter的單例採用以下,是出於如下幾點考慮。
```csharp
#region Instance
private static MessageCenter instance;
private static MessageProcessor messageProcessor;
private static bool instanceCreated = false;
public static MessageCenter GetInstance()
{
if (!instanceCreated)
{
instance = new MessageCenter();
GameObject processor = new GameObject("MessageProcessor");
messageProcessor = processor.AddComponent<MessageProcessor>();
GameObject.DontDestroyOnLoad(messageProcessor);
instanceCreated = true;
}
return instance;
}
private MessageCenter() { }
#endregion
```
### 2.4.1 MessageProcessor可能會被銷燬
GameObject.DontDestroyOnLoad(messageProcessor);來實現不被銷燬
### 2.4.2 最初方案引起的問題
最初方案以下:
```csharp
#region Instance
private static MessageCenter instance;
private static MessageProcessor messageProcessor;
private static bool instanceCreated = false;
public static MessageCenter GetInstance()
{
if (!instanceCreated)
{
instance = new MessageCenter();
//GameObject processor = new GameObject("MessageProcessor");
//messageProcessor = processor.AddComponent<MessageProcessor>();
instanceCreated = true;
}
if (messageProcessor == null)
{
GameObject processor = new GameObject("MessageProcessor");
messageProcessor = processor.AddComponent<MessageProcessor>();
}
return instance;
}
private MessageCenter() { }
#endregion
```
最初方案中instance和messageProcessor是單獨處理的,可是在使用過程當中,在OnDisable方法中調用了MessageCenter的單例(如3.調用方法)。則當unity關閉,隨機銷燬對象時,若是MessageCenter先銷燬,在銷燬MessageProcessor時,則會在OnDestroy方法中先調用OnDisable方法。此時會從新生成MessageCenter的單例,此時就會報以下錯誤:
**Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?**
### 2.4.3 矛盾問題
一、要想MessageProcessor一直存在,則須要當其爲空時從新生成,但在關閉應用時報錯。
二、不從新生成MessageProcessor,場景加載時殺掉MessageProcessor,則消息沒法傳遞。
三、將messageProcessor獨立出來,不在MessageCenter中生成。並加DontDestroyOnLoad方法。此時若是場景從新加載則會有兩個MessageProcessor。
**雖然存在一些矛盾,可是能夠經過場景加載時採用Additive方式,或者其餘方法,找到適合本身工程的處理手段。**
# 3.調用方法
```csharp
using MSG;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MessageSystemTest : MonoBehaviour
{
public Button button;
private void Start()
{
button.onClick.AddListener(() =>
{
MessageCenter.GetInstance().BroadcastMessage(new Message(MsgChannel.TEST_MSG1, 111));
MessageCenter.GetInstance().BroadcastMessage(new Message(MsgChannel.TEST_MSG1, 111));
MessageCenter.GetInstance().BroadcastMessage(new NetworkMessage(MsgChannel.TEST_MSG2, null));
});
}
void OnEnable ()
{
MessageCenter.GetInstance().RegisterMessageHandler(MsgChannel.TEST_MSG1, Method);
MessageCenter.GetInstance().RegisterMessageHandler(MsgChannel.TEST_MSG1, Method1);
MessageCenter.GetInstance().RegisterMessageHandler(MsgChannel.TEST_MSG2, Method2);
}
private void OnDisable()
{
MessageCenter.GetInstance().UnregisterMessageHandler(MsgChannel.TEST_MSG1, Method);
MessageCenter.GetInstance().UnregisterMessageHandler(MsgChannel.TEST_MSG1, Method1);
MessageCenter.GetInstance().UnregisterMessageHandler(MsgChannel.TEST_MSG2, Method2);
}
void Method(Message message)
{
Debug.LogFormat("Method : Message -{0}- obtained from channel {1}", message.arg1, message.what);
}
void Method1(Message message)
{
Debug.LogFormat("Method1 : Message -{0}- obtained from channel {1}", message.arg1, message.what);
}
void Method2(Message message)
{
NetworkMessage networkMessage = message as NetworkMessage;
if(networkMessage != null)
{
Debug.LogFormat("NetworkMessage obtained from channel {0}", message.what);
}
}
}
```