Created
February 29, 2020 07:51
-
-
Save jackyli-work/ac7b9f6051d173cea75a5df11ab4eb7e to your computer and use it in GitHub Desktop.
MessageSystem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace JFun.Gameplay.MessageSystem | |
{ | |
public static class HashMessageManager | |
{ | |
private static Dictionary<int, Delegate> _eventHashTable = new Dictionary<int, Delegate>(); | |
//Hash handlers that should never be removed, regardless of calling Cleanup | |
private static List<int> _permanentHashList = new List<int>(); | |
public static event Action<int, Delegate> OnListenerAddStartEvent; | |
public static event Action<int, Delegate> OnListenerAddEndEvent; | |
public static event Action<int, Delegate> OnListenerRemoveStartEvent; | |
public static event Action<int, Delegate> OnListenerRemoveEndEvent; | |
public static event Action<int> OnBroadcastStartEvent; | |
public static event Action<int, Delegate> OnBroadcastingEvent; | |
public static event Action<int> OnBroadcastEndEvent; | |
#region Helper Methods | |
//Marks a certain hash as permanent. | |
public static void MarkAsPermanent(int eventHash) | |
{ | |
_permanentHashList.Add(eventHash); | |
} | |
public static void Cleanup() | |
{ | |
List<int> removeList = new List<int>(); | |
foreach (KeyValuePair<int, Delegate> pair in _eventHashTable) | |
{ | |
bool wasFound = false; | |
foreach (int hash in _permanentHashList) | |
{ | |
if (pair.Key == hash) | |
{ | |
wasFound = true; | |
break; | |
} | |
} | |
if (!wasFound) | |
{ | |
removeList.Add(pair.Key); | |
} | |
} | |
foreach (int hash in removeList) | |
{ | |
_eventHashTable.Remove(hash); | |
} | |
} | |
#endregion Helper Methods | |
#region AddListener | |
//No parameters | |
public static void AddListener(int eventHash, Action handler) | |
{ | |
OnListenerAddStart(eventHash, handler); | |
_eventHashTable[eventHash] = (Action)_eventHashTable[eventHash] + handler; | |
OnListenerAddEnd(eventHash, handler); | |
} | |
//Single parameter | |
public static void AddListener<T>(int eventHash, Action<T> handler) | |
{ | |
OnListenerAddStart(eventHash, handler); | |
_eventHashTable[eventHash] = (Action<T>)_eventHashTable[eventHash] + handler; | |
OnListenerAddEnd(eventHash, handler); | |
} | |
//Two parameters | |
public static void AddListener<T, U>(int eventHash, Action<T, U> handler) | |
{ | |
OnListenerAddStart(eventHash, handler); | |
_eventHashTable[eventHash] = (Action<T, U>)_eventHashTable[eventHash] + handler; | |
OnListenerAddEnd(eventHash, handler); | |
} | |
//Three parameters | |
public static void AddListener<T, U, V>(int eventHash, Action<T, U, V> handler) | |
{ | |
OnListenerAddStart(eventHash, handler); | |
_eventHashTable[eventHash] = (Action<T, U, V>)_eventHashTable[eventHash] + handler; | |
OnListenerAddEnd(eventHash, handler); | |
} | |
#endregion AddListener | |
#region RemoveListener | |
//No parameters | |
public static void RemoveListener(int eventHash, Action handler) | |
{ | |
OnListenerRemoveStart(eventHash, handler); | |
_eventHashTable[eventHash] = (Action)_eventHashTable[eventHash] - handler; | |
OnListenerRemoveEnd(eventHash, handler); | |
} | |
//Single parameter | |
public static void RemoveListener<T>(int eventHash, Action<T> handler) | |
{ | |
OnListenerRemoveStart(eventHash, handler); | |
_eventHashTable[eventHash] = (Action<T>)_eventHashTable[eventHash] - handler; | |
OnListenerRemoveEnd(eventHash, handler); | |
} | |
//Two parameters | |
public static void RemoveListener<T, U>(int eventHash, Action<T, U> handler) | |
{ | |
OnListenerRemoveStart(eventHash, handler); | |
_eventHashTable[eventHash] = (Action<T, U>)_eventHashTable[eventHash] - handler; | |
OnListenerRemoveEnd(eventHash, handler); | |
} | |
//Three parameters | |
public static void RemoveListener<T, U, V>(int eventHash, Action<T, U, V> handler) | |
{ | |
OnListenerRemoveStart(eventHash, handler); | |
_eventHashTable[eventHash] = (Action<T, U, V>)_eventHashTable[eventHash] - handler; | |
OnListenerRemoveEnd(eventHash, handler); | |
} | |
#endregion RemoveListener | |
#region Broadcast | |
//No parameters | |
public static bool Broadcast(int eventHash) | |
{ | |
bool result = false; | |
OnBroadcastStart(eventHash); | |
Delegate d; | |
if (_eventHashTable.TryGetValue(eventHash, out d)) | |
{ | |
Action handler = d as Action; | |
if (handler != null) | |
{ | |
OnBroadcasting(eventHash, handler); | |
handler(); | |
result = true; | |
} | |
} | |
OnBroadcastEnd(eventHash); | |
return result; | |
} | |
//Single parameter | |
public static bool Broadcast<T>(int eventHash, T arg1) | |
{ | |
bool result = false; | |
OnBroadcastStart(eventHash); | |
Delegate d; | |
if (_eventHashTable.TryGetValue(eventHash, out d)) | |
{ | |
Action<T> handler = d as Action<T>; | |
if (handler != null) | |
{ | |
OnBroadcasting(eventHash, handler); | |
handler(arg1); | |
result = true; | |
} | |
} | |
OnBroadcastEnd(eventHash); | |
return result; | |
} | |
//Two parameters | |
public static bool Broadcast<T, U>(int eventHash, T arg1, U arg2) | |
{ | |
bool result = false; | |
OnBroadcastStart(eventHash); | |
Delegate d; | |
if (_eventHashTable.TryGetValue(eventHash, out d)) | |
{ | |
Action<T, U> handler = d as Action<T, U>; | |
if (handler != null) | |
{ | |
OnBroadcasting(eventHash, handler); | |
handler(arg1, arg2); | |
result = true; | |
} | |
} | |
OnBroadcastEnd(eventHash); | |
return result; | |
} | |
//Three parameters | |
public static bool Broadcast<T, U, V>(int eventHash, T arg1, U arg2, V arg3) | |
{ | |
bool result = false; | |
OnBroadcastStart(eventHash); | |
Delegate d; | |
if (_eventHashTable.TryGetValue(eventHash, out d)) | |
{ | |
Action<T, U, V> handler = d as Action<T, U, V>; | |
if (handler != null) | |
{ | |
OnBroadcasting(eventHash, handler); | |
handler(arg1, arg2, arg3); | |
result = true; | |
} | |
} | |
OnBroadcastEnd(eventHash); | |
return result; | |
} | |
#endregion Broadcast | |
#region Listener | |
public static bool HaveListener(int eventHash) | |
{ | |
return _eventHashTable.ContainsKey(eventHash); | |
} | |
public static bool TryGetListenerType(int eventHash, out Type type) | |
{ | |
Delegate d; | |
if (_eventHashTable.TryGetValue(eventHash, out d)) | |
{ | |
if (d != null) | |
{ | |
type = d.GetType(); | |
return true; | |
} | |
} | |
type = default(Type); | |
return false; | |
} | |
#endregion Listener | |
#region Private Methods | |
private static void OnListenerAddStart(int eventHash, Delegate handler) | |
{ | |
if (!_eventHashTable.ContainsKey(eventHash)) | |
{ | |
_eventHashTable.Add(eventHash, null); | |
} | |
if (OnListenerAddStartEvent != null) | |
{ | |
OnListenerAddStartEvent(eventHash, handler); | |
} | |
} | |
private static void OnListenerAddEnd(int eventHash, Delegate handler) | |
{ | |
if (OnListenerAddEndEvent != null) | |
{ | |
OnListenerAddEndEvent(eventHash, handler); | |
} | |
} | |
private static void OnListenerRemoveStart(int eventHash, Delegate handler) | |
{ | |
if (OnListenerRemoveStartEvent != null) | |
{ | |
OnListenerRemoveStartEvent(eventHash, handler); | |
} | |
} | |
private static void OnListenerRemoveEnd(int eventHash, Delegate handler) | |
{ | |
if (OnListenerRemoveEndEvent != null) | |
{ | |
OnListenerRemoveEndEvent(eventHash, handler); | |
} | |
if (_eventHashTable[eventHash] == null) | |
{ | |
_eventHashTable.Remove(eventHash); | |
} | |
} | |
private static void OnBroadcastStart(int eventHash) | |
{ | |
if (OnBroadcastStartEvent != null) | |
{ | |
OnBroadcastStartEvent(eventHash); | |
} | |
} | |
private static void OnBroadcasting(int eventHash, Delegate handler) | |
{ | |
if (OnBroadcastingEvent != null) | |
{ | |
OnBroadcastingEvent(eventHash, handler); | |
} | |
} | |
private static void OnBroadcastEnd(int eventHash) | |
{ | |
if (OnBroadcastEndEvent != null) | |
{ | |
OnBroadcastEndEvent(eventHash); | |
} | |
} | |
#endregion Private Methods | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using JFun.Framework; | |
using JFun.Foundation.Loggers; | |
namespace JFun.Gameplay.MessageSystem | |
{ | |
public class MessageBroadcaster : MonoBehaviour | |
{ | |
public string MessageCmd; | |
[ContextMenu("Broadcast")] | |
public void Broadcast() | |
{ | |
if (MessageManager.HaveListener(MessageCmd)) | |
{ | |
MessageManager.Broadcast(MessageCmd); | |
} | |
else | |
{ | |
ShareLogger.Instance.Error("[MessageBroadcaster.Broadcast] None object register MessageCmd: {0}.", MessageCmd); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
namespace JFun.Gameplay.MessageSystem | |
{ | |
[CustomEditor(typeof(MessageBroadcaster), true), CanEditMultipleObjects] | |
public class MessageBroadcasterInspector : Editor | |
{ | |
public MessageBroadcaster Target | |
{ | |
get | |
{ | |
return target as MessageBroadcaster; | |
} | |
} | |
public override void OnInspectorGUI() | |
{ | |
Target.MessageCmd = EditorGUILayout.TextField("MessageCmd", Target.MessageCmd); | |
if (GUILayout.Button("Broadcast")) | |
{ | |
Target.Broadcast(); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace JFun.Gameplay.MessageSystem | |
{ | |
public static class MessageCmd | |
{ | |
public static string OpeningScenario = "OpeningScenario"; | |
public static string EndingScenario = "EndingScenario"; | |
// BattleOpen觸發 | |
public static string PreBattleOpening = "PreBattleOpening"; | |
public static string OnBattleOpening = "OnBattleOpening"; | |
public static string PostBattleOpening = "PostBattleOpening"; | |
// BattleFight觸發 | |
public static string PreBattleFighting = "PreBattleFighting"; | |
public static string OnBattleFighting = "OnBattleFighting"; | |
public static string PostBattleFighting = "PostBattleFighting"; | |
// BattleEnd觸發 | |
public static string PreBattleEnding = "PreBattleEnding"; | |
public static string OnBattleEnding = "OnBattleEnding"; | |
public static string PostBattleEnding = "PostBattleEnding"; | |
// BattleTime觸發 | |
public static string BrocastRemainTime = "BrocastRemainTime"; | |
public static string SetTimeScale = "SetTimeScale"; | |
// BattleUI觸發 | |
public static string BrocastDeployClock = "BrocastDeployClock"; | |
public static string BrocastPopupMana = "BrocastPopupMana"; | |
// DraggableItem觸發 | |
public static string BeginDrag = "BeginDrag"; | |
public static string Dragging = "Dragging"; | |
public static string EndDrag = "EndDrag"; | |
// ClickableObj觸發,在戰場中是專給戰場地板點擊用 | |
public static string PointerDown = "PointerDown"; | |
public static string PointerUp = "PointerUp"; | |
public static string ClickObj = "ClickObj"; | |
// UIHandCard觸發 | |
public static string OnClickHandCard = "OnClickHandCard"; | |
// CardGameManager觸發 | |
public static string CardBeginDrag = "CardBeginDrag"; | |
public static string CardDragging = "CardDragging"; | |
public static string CardEndDrag = "CardEndDrag"; | |
// UseCard觸發 | |
public static string UseCard = "UseCard"; | |
// DisplayCard觸發 | |
public static string DisplayCard = "DisplayCard"; | |
// EntityUnit觸發 | |
public static string SpawnUnitModel = "SpawnUnitModel"; | |
public static string DespawnUnitModel = "DespawnUnitModel"; | |
public static string ShowCourtMask = "ShowCourtMask"; | |
// UnitBaseMarker觸發 | |
public static string OpenUnitMarker = "OpenUnitMarker"; | |
public static string CloseUnitMarker = "CloseUnitMarker"; | |
public static string ShowUnitMarker = "ShowUnitMarker"; | |
public static string HideUnitMarker = "HideUnitMarker"; | |
public static string SetNameOffset = "SetNameOffset"; | |
// TotemUnitMarker觸發 | |
public static string RefreshFieldColor = "RefreshFieldColor"; | |
public static string RecoverFieldColor = "RecoverFieldColor"; | |
// Emoticon觸發 | |
public static string PlayEmoticon = "PlayEmoticon"; | |
// BadConnection觸發 | |
public static string BadConnection = "BadConnection"; | |
// ShakeCamera觸發 | |
public static string ShakeCamera = "ShakeCamera"; | |
// Story觸發 | |
public static string RegisterStroyCanvas = "RegisterStroyCanvas"; | |
// LobbyUI觸發 | |
public static string SetCanvasResolution = "SetCanvasResolution"; | |
public static string DisableLobbyScrollRect = "DisableLobbyScrollRect"; | |
public static string RegisterLobbyCanvas = "RegisterLobbyCanvas"; | |
public static string UnregisterLobbyCanvas = "UnregisterLobbyCanvas"; | |
public static string RegisterResourceCanvas = "RegisterResourceCanvas"; | |
public static string UnregisterResourceCanvas = "UnregisterResourceCanvas"; | |
public static string SetLobbyScrollActive = "SetLobbyScrollActive"; | |
public static string LobbyAutoScroll = "LobbyAutoScroll"; | |
public static string InitLobbyMenu = "InitLobbyMenu"; | |
public static string OnClickLobbyMenu = "OnClickLobbyMenu"; | |
public static string OnLobbyScrollChanged = "OnLobbyScrollChanged"; | |
public static string SetBoundaryActive = "SetBoundaryActive"; | |
// LoadingSceneCanvas觸發 | |
public static string OnLoadingSceneStart = "OnLoadingSceneStart"; | |
// PlayerInfoUI, CardInfoUI判斷是否為其他玩家資訊 | |
public static string IsOtherPlayerInfo = "IsOtherPlayerInfo"; | |
// CardInfoUI觸發 | |
public static string CardInfoUseButtonActive = "CardInfoUseButtonActive"; | |
// BoosterPackUI觸發 | |
public static string BoosterPackOpen = "BoosterPackOpen"; | |
public static string BoosterPackClose = "BoosterPackClose"; | |
public static string BoosterPackRevealReward = "BoosterPackRevealReward"; | |
public static string BoosterPackRewardList = "BoosterPackRewardList"; | |
public static string BoosterPackHeadIconList = "BoosterPackHeadIconList"; | |
public static string TempBoosterPackAddReward = "BoosterPackAddReward";// This is For Guild. | |
// UITitleHelper觸發 | |
public static string TitleResourceAnim = "TitleResourceAnim"; | |
// RewardCurrency觸發 | |
public static string PlayRewardCurrency = "PlayRewardCurrency"; | |
public static string FinishRewardCurrency = "FinishRewardCurrency"; | |
// RewardInfoUI觸發 | |
public static string OpenRewardInfo = "OpenRewardInfo"; | |
public static string CloseRewardInfo = "CloseRewardInfo"; | |
// UITipHelper 觸發 | |
public static string ShowTip = "ShowTip"; | |
// CardLevelUpUI觸發 | |
public static string PlayCardLevelUp = "PlayCardLevelUp"; | |
// UIWaiting | |
public static string ActivateWaitingUI = "ActivateWaitingUI"; | |
public static string InactivateWaitingUI = "InactivateWaitingUI"; | |
// UIResource & UIMainMenu 開關 | |
public static string Lobby_Ready = "Lobby_Ready"; | |
public static string Ready_Lobby = "Ready_Lobby"; | |
// UITutorialBattleHelper觸發 | |
public static string OpenDragHandCard = "OpenDragHandCard"; | |
public static string CloseDragHandCard = "CloseDragHandCard"; | |
public static string LimitDeployPosition = "LimitDeployPosition"; | |
public static string UIToTopCanvas = "UIToTopCanvas"; | |
// System Message | |
public static string SystemMsg = "SystemMsg"; | |
public static string SystemMsgFormat = "SystemMsgFormat"; | |
// UIToShopHelper觸發 | |
public static string UIToDiamondShop = "UIToDiamondShop"; | |
// UIInputEventHelper觸發 | |
public static string EscapeButtonOpen = "EscapeButtonOpen"; | |
public static string EscapeButtonClose = "EscapeButtonClose"; | |
public static string EscapeButtonInactive = "EscapeButtonInactive"; | |
//UIRegionState | |
public static string UIRegionStateClose = "UIRegionStateClose"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#define LOG_ALL_MESSAGES | |
//#define LOG_ADD_LISTENER | |
//#define LOG_BROADCAST_MESSAGE | |
#define REQUIRE_LISTENER | |
using System; | |
using UnityEngine; | |
using JFun.Framework; | |
using JFun.Foundation.Loggers; | |
namespace JFun.Gameplay.MessageSystem | |
{ | |
public class MessageLogger | |
{ | |
[RuntimeInitializeOnLoadMethod] | |
private static void InitializeOnLoadMethod() | |
{ | |
MessageManager.OnListenerAddStartEvent += OnListenerAddStart; | |
MessageManager.OnListenerAddEndEvent += OnListenerAddEnd; | |
MessageManager.OnListenerRemoveStartEvent += OnListenerRemoveStart; | |
MessageManager.OnListenerRemoveEndEvent += OnListenerRemoveEnd; | |
MessageManager.OnBroadcastStartEvent += OnBroadcastStart; | |
MessageManager.OnBroadcastEndEvent += OnBroadcastEnd; | |
} | |
public static void OnListenerAddStart(string eventType, Delegate listenerBeingAdded) | |
{ | |
if (MessageManager.HaveListener(eventType)) | |
{ | |
Type type; | |
if (MessageManager.TryGetListenerType(eventType, out type)) | |
{ | |
if (type != listenerBeingAdded.GetType()) | |
{ | |
//throw new ListenerException(string.Format("Attempting to add listener with inconsistent signature for event type {0}. Current listeners have type {1} and listener being added has type {2}", eventType, type.Name, listenerBeingAdded.GetType().Name)); | |
ShareLogger.Instance.Error(string.Format("Attempting to add listener with inconsistent signature for event type {0}. Current listeners have type {1} and listener being added has type {2}", eventType, type.Name, listenerBeingAdded.GetType().Name)); | |
} | |
} | |
else | |
{ | |
//throw new ListenerException(string.Format("Attempting to add listener with for event type \"{0}\" but current listener is null.", eventType)); | |
ShareLogger.Instance.Error(string.Format("Attempting to add listener with for event type \"{0}\" but current listener is null.", eventType)); | |
} | |
} | |
} | |
public static void OnListenerAddEnd(string eventType, Delegate listenerBeingAdded) | |
{ | |
if (!MessageManager.HaveListener(eventType)) | |
{ | |
//throw new ListenerException(string.Format("Attempting to add listener for type \"{0}\" but Messenger doesn't know about this event type.", eventType)); | |
ShareLogger.Instance.Error(string.Format("Attempting to add listener for type \"{0}\" but Messenger doesn't know about this event type.", eventType)); | |
} | |
} | |
public static void OnListenerRemoveStart(string eventType, Delegate listenerBeingRemoved) | |
{ | |
if (!MessageManager.HaveListener(eventType)) | |
{ | |
//throw new ListenerException(string.Format("Attempting to remove listener for type \"{0}\" but Messenger doesn't know about this event type.", eventType)); | |
ShareLogger.Instance.Error(string.Format("Attempting to remove listener for type \"{0}\" but Messenger doesn't know about this event type.", eventType)); | |
} | |
} | |
public static void OnListenerRemoveEnd(string eventType, Delegate listenerBeingRemoved) | |
{ | |
if (MessageManager.HaveListener(eventType)) | |
{ | |
Type type; | |
if (MessageManager.TryGetListenerType(eventType, out type)) | |
{ | |
if (type != listenerBeingRemoved.GetType()) | |
{ | |
//throw new ListenerException(string.Format("Attempting to remove listener with inconsistent signature for event type {0}. Current listeners have type {1} and listener being removed has type {2}", eventType, type.Name, listenerBeingRemoved.GetType().Name)); | |
ShareLogger.Instance.Error(string.Format("Attempting to remove listener with inconsistent signature for event type {0}. Current listeners have type {1} and listener being removed has type {2}", eventType, type.Name, listenerBeingRemoved.GetType().Name)); | |
} | |
} | |
else | |
{ | |
//throw new ListenerException(string.Format("Attempting to remove listener with for event type \"{0}\" but current listener is null.", eventType)); | |
ShareLogger.Instance.Error(string.Format("Attempting to remove listener with for event type \"{0}\" but current listener is null.", eventType)); | |
} | |
} | |
} | |
public static void OnBroadcastStart(string eventType) | |
{ | |
#if LOG_ALL_MESSAGES || LOG_BROADCAST_MESSAGE | |
ShareLogger.Instance.Debug("MESSENGER\t" + System.DateTime.Now.ToString("hh:mm:ss.fff") + "\t\t\tInvoking \t\"" + eventType + "\""); | |
#endif | |
} | |
public static void OnBroadcastEnd(string eventType) | |
{ | |
#if REQUIRE_LISTENER | |
if (!MessageManager.HaveListener(eventType)) | |
{ | |
//throw new BroadcastException(string.Format("Broadcasting message \"{0}\" but no listener found. Try marking the message with Messenger.MarkAsPermanent.", eventType)); | |
if (DebugLogger.Level >= Foundation.Loggers.LogLevel.Warn) | |
ShareLogger.Instance.Warn(string.Format("Broadcasting message \"{0}\" but no listener found. Try marking the message with Messenger.MarkAsPermanent.", eventType)); | |
} | |
#endif | |
} | |
//public class BroadcastException : Exception | |
//{ | |
// public BroadcastException(string msg) | |
// : base(msg) | |
// { | |
// } | |
//} | |
//public class ListenerException : Exception | |
//{ | |
// public ListenerException(string msg) | |
// : base(msg) | |
// { | |
// } | |
//} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace JFun.Gameplay.MessageSystem | |
{ | |
public static class MessageManager | |
{ | |
public static event Action<string, Delegate> OnListenerAddStartEvent; | |
public static event Action<string, Delegate> OnListenerAddEndEvent; | |
public static event Action<string, Delegate> OnListenerRemoveStartEvent; | |
public static event Action<string, Delegate> OnListenerRemoveEndEvent; | |
public static event Action<string> OnBroadcastStartEvent; | |
public static event Action<string> OnBroadcastEndEvent; | |
#region Helper methods | |
public static int StringToHash(string eventType) | |
{ | |
return eventType.GetHashCode(); | |
} | |
//Marks a certain message as permanent. | |
public static void MarkAsPermanent(string eventType) | |
{ | |
HashMessageManager.MarkAsPermanent(StringToHash(eventType)); | |
} | |
public static void Cleanup() | |
{ | |
HashMessageManager.Cleanup(); | |
} | |
#endregion Helper methods | |
#region AddListener | |
//No parameters | |
public static void AddListener(string eventType, Action handler) | |
{ | |
OnListenerAddStart(eventType, handler); | |
HashMessageManager.AddListener(StringToHash(eventType), handler); | |
OnListenerAddEnd(eventType, handler); | |
} | |
//Single parameter | |
public static void AddListener<T>(string eventType, Action<T> handler) | |
{ | |
OnListenerAddStart(eventType, handler); | |
HashMessageManager.AddListener(StringToHash(eventType), handler); | |
OnListenerAddEnd(eventType, handler); | |
} | |
//Two parameters | |
public static void AddListener<T, U>(string eventType, Action<T, U> handler) | |
{ | |
OnListenerAddStart(eventType, handler); | |
HashMessageManager.AddListener(StringToHash(eventType), handler); | |
OnListenerAddEnd(eventType, handler); | |
} | |
//Three parameters | |
public static void AddListener<T, U, V>(string eventType, Action<T, U, V> handler) | |
{ | |
OnListenerAddStart(eventType, handler); | |
HashMessageManager.AddListener(StringToHash(eventType), handler); | |
OnListenerAddEnd(eventType, handler); | |
} | |
#endregion AddListener | |
#region RemoveListener | |
//No parameters | |
public static void RemoveListener(string eventType, Action handler) | |
{ | |
OnListenerRemoveStart(eventType, handler); | |
HashMessageManager.RemoveListener(StringToHash(eventType), handler); | |
OnListenerRemoveEnd(eventType, handler); | |
} | |
//Single parameter | |
public static void RemoveListener<T>(string eventType, Action<T> handler) | |
{ | |
OnListenerRemoveStart(eventType, handler); | |
HashMessageManager.RemoveListener(StringToHash(eventType), handler); | |
OnListenerRemoveEnd(eventType, handler); | |
} | |
//Two parameters | |
public static void RemoveListener<T, U>(string eventType, Action<T, U> handler) | |
{ | |
OnListenerRemoveStart(eventType, handler); | |
HashMessageManager.RemoveListener(StringToHash(eventType), handler); | |
OnListenerRemoveEnd(eventType, handler); | |
} | |
//Three parameters | |
public static void RemoveListener<T, U, V>(string eventType, Action<T, U, V> handler) | |
{ | |
OnListenerRemoveStart(eventType, handler); | |
HashMessageManager.RemoveListener(StringToHash(eventType), handler); | |
OnListenerRemoveEnd(eventType, handler); | |
} | |
#endregion RemoveListener | |
#region Broadcast | |
//No parameters | |
public static bool Broadcast(string eventType) | |
{ | |
OnBroadcastStart(eventType); | |
bool isDone = HashMessageManager.Broadcast(StringToHash(eventType)); | |
OnBroadcastEnd(eventType); | |
return isDone; | |
} | |
//Single parameter | |
public static bool Broadcast<T>(string eventType, T arg1) | |
{ | |
OnBroadcastStart(eventType); | |
bool isDone = HashMessageManager.Broadcast(StringToHash(eventType), arg1); | |
OnBroadcastEnd(eventType); | |
return isDone; | |
} | |
//Two parameters | |
public static bool Broadcast<T, U>(string eventType, T arg1, U arg2) | |
{ | |
OnBroadcastStart(eventType); | |
bool isDone = HashMessageManager.Broadcast(StringToHash(eventType), arg1, arg2); | |
OnBroadcastEnd(eventType); | |
return isDone; | |
} | |
//Three parameters | |
public static bool Broadcast<T, U, V>(string eventType, T arg1, U arg2, V arg3) | |
{ | |
OnBroadcastStart(eventType); | |
bool isDone = HashMessageManager.Broadcast(StringToHash(eventType), arg1, arg2, arg3); | |
OnBroadcastEnd(eventType); | |
return isDone; | |
} | |
#endregion Broadcast | |
#region Listener | |
public static bool HaveListener(string eventType) | |
{ | |
return HashMessageManager.HaveListener(StringToHash(eventType)); | |
} | |
public static bool TryGetListenerType(string eventType, out Type type) | |
{ | |
return HashMessageManager.TryGetListenerType(StringToHash(eventType), out type); | |
} | |
#endregion Listener | |
#region Private Methods | |
private static void OnListenerAddStart(string eventType, Delegate handler) | |
{ | |
if (OnListenerAddStartEvent != null) | |
{ | |
OnListenerAddStartEvent(eventType, handler); | |
} | |
} | |
private static void OnListenerAddEnd(string eventType, Delegate handler) | |
{ | |
if (OnListenerAddEndEvent != null) | |
{ | |
OnListenerAddEndEvent(eventType, handler); | |
} | |
} | |
private static void OnListenerRemoveStart(string eventType, Delegate handler) | |
{ | |
if (OnListenerRemoveStartEvent != null) | |
{ | |
OnListenerRemoveStartEvent(eventType, handler); | |
} | |
} | |
private static void OnListenerRemoveEnd(string eventType, Delegate handler) | |
{ | |
if (OnListenerRemoveEndEvent != null) | |
{ | |
OnListenerRemoveEndEvent(eventType, handler); | |
} | |
} | |
private static void OnBroadcastStart(string eventType) | |
{ | |
if (OnBroadcastStartEvent != null) | |
{ | |
OnBroadcastStartEvent(eventType); | |
} | |
} | |
private static void OnBroadcastEnd(string eventType) | |
{ | |
if (OnBroadcastEndEvent != null) | |
{ | |
OnBroadcastEndEvent(eventType); | |
} | |
} | |
#endregion Private Methods | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Events; | |
namespace JFun.Gameplay.MessageSystem | |
{ | |
public class MessageReceiver : MonoBehaviour | |
{ | |
public string MessageCmd; | |
public UnityEvent OnReceiverEvent; | |
protected virtual void Awake() | |
{ | |
MessageManager.AddListener(MessageCmd, OnReceiverAction); | |
} | |
protected virtual void OnDestroy() | |
{ | |
MessageManager.RemoveListener(MessageCmd, OnReceiverAction); | |
} | |
protected virtual void OnReceiverAction() | |
{ | |
if (OnReceiverEvent != null) | |
{ | |
OnReceiverEvent.Invoke(); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Events; | |
namespace JFun.Gameplay.MessageSystem | |
{ | |
public abstract class MessageReceivorBase<T, K> : MonoBehaviour where T : UnityEvent<K> | |
{ | |
public string MessageCmd; | |
public T OnReceiverEvent; | |
protected virtual void Awake() | |
{ | |
MessageManager.AddListener<K>(MessageCmd, OnReceiverAction); | |
} | |
protected virtual void OnDestroy() | |
{ | |
MessageManager.RemoveListener<K>(MessageCmd, OnReceiverAction); | |
} | |
protected virtual void OnReceiverAction(K arg) | |
{ | |
if (OnReceiverEvent != null) | |
{ | |
OnReceiverEvent.Invoke(arg); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment