5using System.Collections.Generic;
6using System.Reflection;
10using System.Threading.Tasks;
64 MessageType =
typeof(MessageType)
121 UnityEngine.Object.Destroy(
s_worker.gameObject);
139 #region Coroutine Support
209 void System.Collections.IEnumerator.Reset()
216 #region Async Support
220 public static async Task<MessageType> ListenAsync<MessageType>()
222 var tcs =
new TaskCompletionSource<MessageType>();
224 var handler = RegisterInternal<object, MessageType>(
s_entries,
225 (msg) => tcs.SetResult(msg),
HandlerFlags.DontInvokeIfAddedInAHandler);
229 return await tcs.Task;
233 EventBetter.UnlistenHandler(typeof(MessageType), handler);
375 var invocationCount = ++
entry.invocationCount;
381 for (
int i = 0;
i <
entry.Count; ++
i)
401 if (!
go.activeInHierarchy)
428 if (invocationCount == 1)
438 entry.needsCleanup =
true;
446 UnityEngine.Debug.Assert(invocationCount ==
entry.invocationCount);
447 --
entry.invocationCount;
449 if (invocationCount == 1 &&
entry.needsCleanup)
451 entry.needsCleanup =
false;
510 for (
int i = 0;
i <
entry.Count; ++
i)
512 if (
entry.listeners[
i] ==
null)
519 if (
entry.invocationCount == 0)
528 entry.needsCleanup =
true;
553 for (
int i = 0;
i <
entry.Count; ++
i)
558 if ((UnityEngine.Object)
listener !=
null)
567 if (
entry.invocationCount == 0)
585 go.hideFlags =
HideFlags.HideAndDontSave;
586 GameObject.DontDestroyOnLoad(
go);
readonly List< Delegate > handlers
void Add(object listener, Delegate handler, HandlerFlags flag)
bool HasFlag(int i, HandlerFlags flag)
readonly List< object > listeners
void SetFlag(int i, HandlerFlags flag, bool value)
readonly List< HandlerFlags > flags
void OnMessage(MessageType msg)
bool System.Collections.IEnumerator. MoveNext()
List< MessageType > Messages
Intentionally made partial, in case you want to extend it easily.
static Delegate RegisterInternal< ListenerType, MessageType >(ListenerType listener, System.Action< MessageType > handler, HandlerFlags flags)
static bool Unlisten< MessageType >(UnityEngine.Object listener)
Unregisters all MessageType handlers for a given listener.
static object GetAliveTarget(object target)
static void Listen< ListenerType, MessageType >(ListenerType listener, System.Action< MessageType > handler, bool once=false, bool exculdeInactive=false)
Register a message handler.
static IDisposable ListenManual< MessageType >(System.Action< MessageType > handler)
Register a message handler. No listener, you unregister by calling Dispose on returned object....
static bool RaiseInternal< T >(T message)
static void EnsureWorkerExistsAndIsActive()
static List< EventEntry > s_entriesList
For faster iteration.
@ DontInvokeIfAddedInAHandler
static YieldListener< MessageType > ListenWait< MessageType >()
Use this in coroutines. Yield will return when at least one event of type MessageType has been raise...
static bool UnlistenAll(UnityEngine.Object listener)
Unregisters all message types for a given listener.
static bool Raise< MessageType >(MessageType message)
Invoke all registered handlers for this message type immediately.
static Dictionary< Type, EventEntry > s_entries
For lookups.
static bool UnlistenHandler(Type messageType, Delegate handler)
static void RemoveUnusedHandlers(EventEntry entry)
static void Clear()
Unregisters everything.
static void RemoveUnusedHandlers()
Removes handlers that will now longer be called because their listeners have been destroyed....
static Delegate RegisterInternal< T >(object listener, Action< T > handler, HandlerFlags flags)
static bool UnregisterInternal< ParamType >(Type messageType, ParamType param, Func< EventEntry, int, ParamType, bool > predicate)
static EventBetterWorker s_worker
For removing dead handlers.