/// <summary> /// Create a MessageToken to invoke at a later point. If no targets found null is returned. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="includeDisabledComponents"></param> /// <returns></returns> public static MessageToken <T> CreateFindAndBroadcastToken <T>(bool includeDisabledComponents = false) where T : class { using (var lst = TempCollection.GetSet <T>()) { ObjUtil.FindObjectsOfInterface <T>(lst); if (lst.Count == 0) { return(null); } if (includeDisabledComponents) { return(new MessageToken <T>(lst.ToArray())); } using (var lst2 = TempCollection.GetList <T>(lst.Count)) { var e = lst.GetEnumerator(); while (e.MoveNext()) { if (TargetIsValid(e.Current)) { lst.Add(e.Current); } } if (lst.Count > 0) { return(new MessageToken <T>(lst2.ToArray())); } } } return(null); }
/// <summary> /// Broadcast a message globally. This can be slow, use sparingly. /// </summary> /// <typeparam name="TInterface"></typeparam> /// <typeparam name="TArg"></typeparam> /// <param name="arg"></param> /// <param name="functor"></param> public static void Broadcast <TInterface, TArg>(TArg arg, System.Action <TInterface, TArg> functor) where TInterface : class { if (functor == null) { throw new System.ArgumentNullException("functor"); } using (var lst = TempCollection.GetList <TInterface>()) { ObjUtil.FindObjectsOfInterface <TInterface>(lst); var e = lst.GetEnumerator(); while (e.MoveNext()) { functor(e.Current, arg); } } }
/// <summary> /// Broadcast a message globally to all that match T. This can be slow, use sparingly. /// </summary> /// <typeparam name="TInterface"></typeparam> /// <typeparam name="TArg"></typeparam> /// <param name="arg"></param> /// <param name="functor"></param> public static void FindAndBroadcast <TInterface, TArg>(TArg arg, System.Action <TInterface, TArg> functor, bool includeDisabledComponents = false) where TInterface : class { if (functor == null) { throw new System.ArgumentNullException("functor"); } using (var lst = TempCollection.GetSet <TInterface>()) { ObjUtil.FindObjectsOfInterface <TInterface>(lst); var e = lst.GetEnumerator(); while (e.MoveNext()) { if (includeDisabledComponents || TargetIsValid(e.Current)) { functor(e.Current, arg); } } } }
/// <summary> /// Broadcast a message globally to all that match T. This can be slow, use sparingly. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="functor"></param> public static void FindAndBroadcast <T>(System.Action <T> functor, bool includeDisabledComponents = false) where T : class { if (functor == null) { throw new System.ArgumentNullException("functor"); } using (var coll = TempCollection.GetSet <T>()) { ObjUtil.FindObjectsOfInterface <T>(coll); GlobalMessagePool <T> .CopyReceivers(coll); var e = coll.GetEnumerator(); while (e.MoveNext()) { if (includeDisabledComponents || TargetIsValid(e.Current)) { functor(e.Current); } } } }