public static void RemoveComponents(this Component obj, System.Type tp)
        {
            if (obj == null)
            {
                return;
            }
            var arr = obj.GetComponents(tp);

            for (int i = 0; i < arr.Length; i++)
            {
                ObjUtil.SmartDestroy(arr[i]);
            }
        }
        /// <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);
                }
            }
        }
Esempio n. 3
0
        public static T Find <T>(SearchBy search, string query) where T : class
        {
            switch (search)
            {
            case SearchBy.Nothing:
                return(null);

            case SearchBy.Tag:
                return(ObjUtil.GetAsFromSource <T>(GameObjectUtil.FindWithMultiTag(query)));

            case SearchBy.Name:
                return(ObjUtil.GetAsFromSource <T>(UnityEngine.GameObject.Find(query)));

            case SearchBy.Type:
                return(ObjUtil.GetAsFromSource <T>(ObjUtil.FindObjectOfType(TypeUtil.FindType(query))));

            default:
                return(null);
            }
        }
Esempio n. 4
0
        public static UnityEngine.Object Find(SearchBy search, string query)
        {
            switch (search)
            {
            case SearchBy.Nothing:
                return(null);

            case SearchBy.Tag:
                return(GameObjectUtil.FindWithMultiTag(query));

            case SearchBy.Name:
                return(UnityEngine.GameObject.Find(query));

            case SearchBy.Type:
                return(ObjUtil.FindObjectOfType(TypeUtil.FindType(query)));

            default:
                return(null);
            }
        }
Esempio n. 5
0
        /// <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);
                    }
                }
            }
        }
        public static void GetComponents <T>(this GameObject obj, ICollection <T> lst, System.Func <Component, T> filter) where T : class
        {
            if (obj == null)
            {
                return;
            }

            using (var tmpLst = TempCollection.GetList <Component>())
            {
                obj.GetComponents(typeof(Component), tmpLst);
                var e = tmpLst.GetEnumerator();
                T   c;
                while (e.MoveNext())
                {
                    c = (filter != null) ? filter(e.Current) : e.Current as T;
                    if (ObjUtil.IsObjectAlive(c as UnityEngine.Object))
                    {
                        lst.Add(c);
                    }
                }
            }
        }
        /// <summary>
        /// Destroys the entire entity, if the entity contains a KillableEntity component that will handle death first and foremost.
        /// </summary>
        /// <param name="obj"></param>
        public static void KillEntity(this GameObject obj)
        {
            if (obj.IsNullOrDestroyed())
            {
                return;
            }

            using (var lst = TempCollection.GetList <IKillableEntity>())
            {
                obj.FindComponents <IKillableEntity>(lst, true);
                if (lst.Count > 0)
                {
                    var e = lst.GetEnumerator();
                    while (e.MoveNext())
                    {
                        e.Current.Kill();
                    }
                }
                else
                {
                    ObjUtil.SmartDestroy(obj);
                }
            }
        }
Esempio n. 9
0
        public static T[] FindAll <T>(SearchBy search, string query) where T : class
        {
            switch (search)
            {
            case SearchBy.Nothing:
                return(ArrayUtil.Empty <T>());

            case SearchBy.Tag:
            {
                using (var tmp = com.spacepuppy.Collections.TempCollection.GetList <UnityEngine.GameObject>())
                    using (var results = com.spacepuppy.Collections.TempCollection.GetList <T>())
                    {
                        GameObjectUtil.FindGameObjectsWithMultiTag(query, tmp);
                        var e = tmp.GetEnumerator();
                        while (e.MoveNext())
                        {
                            var o = ObjUtil.GetAsFromSource <T>(e.Current);
                            if (o != null)
                            {
                                results.Add(o);
                            }
                        }
                        return(results.ToArray());
                    }
            }

            case SearchBy.Name:
            {
                using (var tmp = com.spacepuppy.Collections.TempCollection.GetList <UnityEngine.GameObject>())
                    using (var results = com.spacepuppy.Collections.TempCollection.GetList <T>())
                    {
                        GameObjectUtil.FindAllByName(query, tmp);
                        var e = tmp.GetEnumerator();
                        while (e.MoveNext())
                        {
                            var o = ObjUtil.GetAsFromSource <T>(e.Current);
                            if (o != null)
                            {
                                results.Add(o);
                            }
                        }
                        return(results.ToArray());
                    }
            }

            case SearchBy.Type:
            {
                using (var results = com.spacepuppy.Collections.TempCollection.GetList <T>())
                {
                    foreach (var o in ObjUtil.FindObjectsOfType(TypeUtil.FindType(query)))
                    {
                        var o2 = ObjUtil.GetAsFromSource <T>(o);
                        if (o2 != null)
                        {
                            results.Add(o2);
                        }
                    }
                    return(results.ToArray());
                }
            }

            default:
                return(null);
            }
        }
 public static bool FindComponent <T>(this GameObject go, out T comp) where T : class
 {
     comp = FindComponent <T>(go);
     return(ObjUtil.IsObjectAlive(comp as UnityEngine.Object));
 }
Esempio n. 11
0
 public static bool FindComponent(this GameObject go, System.Type tp, out Component comp)
 {
     comp = FindComponent(go, tp);
     return(ObjUtil.IsObjectAlive(comp));
 }