diff --git a/Signals.cs b/Signals.cs index 9f3085e..c7b987e 100644 --- a/Signals.cs +++ b/Signals.cs @@ -1,10 +1,10 @@ -// =================================================================================== +// ======================================================================================== // Signals - A typesafe, lightweight messaging lib for Unity. -// =================================================================================== -// 2017, Yanko Oliveira / http://yankooliveira.com / http://twitter.com/yankooliveira +// ======================================================================================== +// 2017-2019, Yanko Oliveira / http://yankooliveira.com / http://twitter.com/yankooliveira // Special thanks to Max Knoblich for code review and Aswhin Sudhir for the anonymous // function asserts suggestion. -// =================================================================================== +// ======================================================================================== // Inspired by StrangeIOC, minus the clutter. // Based on http://wiki.unity3d.com/index.php/CSharpMessenger_Extended // Converted to use strongly typed parameters and prevent use of strings as ids. @@ -20,9 +20,10 @@ // Signals.Get().Dispatch(userScore); // 4) Don't forget to remove the listeners upon destruction! Eg on OnDestroy(): // Signals.Get().RemoveListener(OnScore); +// 5) If you don't want to use global Signals, you can have your very own SignalHub +// instance in your class // -// -// =================================================================================== +// ======================================================================================== using System; using System.Collections.Generic; @@ -38,18 +39,42 @@ public interface ISignal } /// - /// Signals main facade class + /// Signals main facade class for global, game-wide signals /// - public class Signals + public static class Signals { - private static Dictionary signals = new Dictionary(); + public static SignalHub hub = new SignalHub(); + + public static SType Get() where SType : ISignal, new() + { + return hub.Get(); + } + + public static void AddListenerToHash(string signalHash, Action handler) + { + hub.AddListenerToHash(signalHash, handler); + } + + public static void RemoveListenerFromHash(string signalHash, Action handler) + { + hub.RemoveListenerFromHash(signalHash, handler); + } + + } + + /// + /// A hub for Signals you can implement in your classes + /// + public class SignalHub + { + private Dictionary signals = new Dictionary(); /// /// Getter for a signal of a given type /// /// Type of signal /// The proper signal binding - public static SType Get() where SType : ISignal, new() + public SType Get() where SType : ISignal, new() { Type signalType = typeof(SType); ISignal signal; @@ -69,7 +94,7 @@ public class Signals /// /// Unique hash for signal /// Callback for signal listener - public static void AddListenerToHash(string signalHash, Action handler) + public void AddListenerToHash(string signalHash, Action handler) { ISignal signal = GetSignalByHash(signalHash); if(signal != null && signal is ASignal) @@ -85,7 +110,7 @@ public static void AddListenerToHash(string signalHash, Action handler) /// /// Unique hash for signal /// Callback for signal listener - public static void RemoveListenerFromHash(string signalHash, Action handler) + public void RemoveListenerFromHash(string signalHash, Action handler) { ISignal signal = GetSignalByHash(signalHash); if (signal != null && signal is ASignal) @@ -94,7 +119,7 @@ public static void RemoveListenerFromHash(string signalHash, Action handler) } } - private static ISignal Bind(Type signalType) + private ISignal Bind(Type signalType) { ISignal signal; if(signals.TryGetValue(signalType, out signal)) @@ -108,12 +133,12 @@ private static ISignal Bind(Type signalType) return signal; } - private static ISignal Bind() where T : ISignal, new() + private ISignal Bind() where T : ISignal, new() { return Bind(typeof(T)); } - private static ISignal GetSignalByHash(string signalHash) + private ISignal GetSignalByHash(string signalHash) { foreach (ISignal signal in signals.Values) { @@ -320,4 +345,3 @@ public void Dispatch(T arg1, U arg2, V arg3) } } } -