首先是一个接口
处理网络事件
namespace GlobalScripts
{
public interface IView
{
void OnGameUIEventMessage(IMessage message);
void OnNetworkEventMessage(IMessage message);
}
}
接口声明两个方法
第一个是处理UI类型的消息,第二个处理网络消息。
然后是基类,继承MonoBehaviour
using UnityEngine;
using System.Collections.Generic;
namespace GlobalScripts
{
class UIEventCell
{
public GameUIEventType messages;
public GameUICallback callback;
}
class NetEventCell
{
public NetworkEventType messages;
public NetworkCallback callback;
}
public class Base : MonoBehaviour
{
private List<UIEventCell> mUIEventList = new List<UIEventCell>(8);
private List<NetEventCell> mNetEventList = new List<NetEventCell>(8);
//注册UI消息 - 默认处理函数
protected void RegisterGameUIEventMessage(IView view, GameUIEventType message)
{
GlobalEventManager.GetInstance().RegisterEvent((int)message, view.OnGameUIEventMessage);
mUIEventList.Add(new UIEventCell
{
messages = message,
callback = view.OnGameUIEventMessage
});
}
//注册UI消息 - 指定处理函数
protected void RegisterGameUIEventMessage(GameUICallback callback, GameUIEventType message)
{
GlobalEventManager.GetInstance().RegisterEvent((int)message, callback);
mUIEventList.Add(new UIEventCell
{
messages = message,
callback = callback
});
}
//通知UI事件
protected void NotifyGameUIEvent(GameUIEventType varUIEventType, object body = null)
{
GlobalEventManager.GetInstance().NotifyEvent(new Message((int)varUIEventType, body));
}
//注册网络消息 - 默认处理函数
protected void RegisterNetworkEventMessage(IView view, NetworkEventType varType)
{
NetworkeEvetManager.GetInstance().RegisterEvent((int)varType, view.OnNetworkEventMessage);
mNetEventList.Add(new NetEventCell
{
messages = varType,
callback = view.OnNetworkEventMessage
});
}
//注册网络消息 - 指定处理函数
protected void RegisterNetworkEventMessage(NetworkCallback varCallback, NetworkEventType varType)
{
NetworkeEvetManager.GetInstance().RegisterEvent((int)varType, varCallback);
mNetEventList.Add(new NetEventCell
{
messages = varType,
callback = varCallback
});
}
//注销一个GameUIEventType事件
protected void RemoveMessage(GameUIEventType messages, GameUICallback callback)
{
GlobalEventManager.GetInstance().UnRegisterEvent((int)messages, callback);
}
//注销一个NetworkEventType事件
protected void RemoveMessage(NetworkEventType messages, NetworkCallback varCallback)
{
NetworkeEvetManager.GetInstance().UnRegisterEvent((int)messages, varCallback);
}
//移除该模块当中的所有事件
protected void RemoveAllMessage()
{
foreach (var uiEventCell in mUIEventList)
{
GlobalEventManager.GetInstance().UnRegisterEvent((int)uiEventCell.messages, uiEventCell.callback);
}
foreach (var netEventCell in mNetEventList)
{
NetworkeEvetManager.GetInstance().UnRegisterEvent((int)netEventCell.messages, netEventCell.callback);
}
mUIEventList.Clear();
mNetEventList.Clear();
}
}
}
处理消息的注册,通知,和反注册。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace GlobalScripts
{
public delegate void GameUICallback(IMessage varData);
//全局事件管理类
public class GlobalEventManager
{
private static GlobalEventManager Instance;
public static GlobalEventManager GetInstance()
{
return Instance ?? (Instance = new GlobalEventManager());
}
readonly Dictionary<int, GameUICallback> mGlobalEventing = new Dictionary<int, GameUICallback>();
//注册某一个事件
public void RegisterEvent(int varEventType, GameUICallback varCallback)
{
if (mGlobalEventing.ContainsKey(varEventType))
{
mGlobalEventing[varEventType] += varCallback;
}
else
{
mGlobalEventing.Add(varEventType,varCallback);
}
}
//通知某一个事件
public void NotifyEvent(IMessage varData)
{
GameUICallback _CB;
if (!mGlobalEventing.TryGetValue(varData.EventType, out _CB))
{
Debug.LogError("NotifyEvent: Not Find Handle. Type = " + varData.EventType);
return;
}
if(_CB != null)
_CB(varData);
}
//通知某一个事件
internal void NotifyEvent(int varEventType)
{
NotifyEvent(new Message(varEventType));
}
//解除某一个事件
public void UnRegisterEvent(int varEventType, GameUICallback call)
{
if (!mGlobalEventing.ContainsKey(varEventType))
{
Debug.LogError("UnRegisterEvent: Not Find Handle. Type = " + varEventType);
return;
}
mGlobalEventing[varEventType] -= call;
}
}
}
UI及游戏内一些事件的注册分发与反注册
using System.Collections.Generic;
using UnityEngine;
namespace GlobalScripts
{
public delegate void NetworkCallback(IMessage varData);
public class NetworkeEvetManager
{
//网络全局事件管理类
private static NetworkeEvetManager Instance;
public static NetworkeEvetManager GetInstance()
{
return Instance ?? (Instance = new NetworkeEvetManager());
}
readonly Dictionary<int, NetworkCallback> mGlobalEventing = new Dictionary<int, NetworkCallback>();
//注册某一个事件
public void RegisterEvent(int varEventType, NetworkCallback varCallback)
{
if (mGlobalEventing.ContainsKey(varEventType))
{
mGlobalEventing[varEventType] += varCallback;
}
else
{
mGlobalEventing.Add(varEventType, varCallback);
}
}
//通知某一个事件
public void NotifyEvent(IMessage varData)
{
NetworkCallback _CB;
if (!mGlobalEventing.TryGetValue(varData.EventType, out _CB))
{
Debug.LogError("NotifyEvent: Not Find Handle. Type = " + varData.EventType);
return;
}
if (_CB != null)
_CB(varData);
}
//通知某一个事件
public void NotifyEvent(int varNetworkEventType)
{
NotifyEvent(new Message(varNetworkEventType));
}
//解除某一个事件
public void UnRegisterEvent(int varEventType, NetworkCallback call)
{
if (!mGlobalEventing.ContainsKey(varEventType))
{
Debug.LogError("UnRegisterEvent: Not Find Handle. Type = " + varEventType);
return;
}
mGlobalEventing[varEventType] -= call;
}
}
}
处理网络事件