Ejemplo n.º 1
0
        public static object[] GetAllFromSource(System.Type tp, object obj, bool includeChildren = false)
        {
            if (obj == null)
            {
                return(ArrayUtil.Empty <object>());
            }

            using (var set = TempCollection.GetSet <object>())
            {
                var otp = obj.GetType();
                if (TypeUtil.IsType(otp, tp))
                {
                    set.Add(obj);
                }
                if (obj is IComponent)
                {
                    var c = (obj as IComponent).component;
                    if (!object.ReferenceEquals(c, null) && TypeUtil.IsType(c.GetType(), tp))
                    {
                        set.Add(c);
                    }
                }

                var go = GameObjectUtil.GetGameObjectFromSource(obj);
                if (go != null)
                {
                    if (typeof(SPEntity).IsAssignableFrom(tp))
                    {
                        var entity = SPEntity.Pool.GetFromSource(tp, go);
                        if (entity != null)
                        {
                            set.Add(entity);
                        }
                    }
                    else if (typeof(UnityEngine.GameObject).IsAssignableFrom(tp))
                    {
                        if (includeChildren)
                        {
                            using (var lst = TempCollection.GetList <UnityEngine.Transform>())
                            {
                                go.GetComponentsInChildren <UnityEngine.Transform>(lst);

                                var e = lst.GetEnumerator();
                                while (e.MoveNext())
                                {
                                    set.Add(e.Current.gameObject);
                                }
                            }
                        }
                        else
                        {
                            set.Add(go);
                        }
                    }
                    else if (ComponentUtil.IsAcceptableComponentType(tp))
                    {
                        using (var lst = TempCollection.GetList <UnityEngine.Component>())
                        {
                            if (includeChildren)
                            {
                                ComponentUtil.GetChildComponents(go, tp, lst, true);
                            }
                            else
                            {
                                go.GetComponents(tp, lst);
                            }

                            var e = lst.GetEnumerator();
                            while (e.MoveNext())
                            {
                                set.Add(e.Current);
                            }
                        }
                    }
                }

                return(set.Count > 0 ? set.ToArray() : ArrayUtil.Empty <object>());
            }
        }
        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);
            }
        }
Ejemplo n.º 3
0
        public static T[] GetAllFromSource <T>(object obj, bool includeChildren = false) where T : class
        {
            if (obj == null)
            {
                return(ArrayUtil.Empty <T>());
            }

            using (var set = TempCollection.GetSet <T>())
            {
                if (obj is T)
                {
                    set.Add(obj as T);
                }
                if (obj is IComponent)
                {
                    var c = (obj as IComponent).component;
                    if (c is T)
                    {
                        set.Add(c as T);
                    }
                }

                var go = GameObjectUtil.GetGameObjectFromSource(obj);
                if (go is T)
                {
                    set.Add(go as T);
                }

                //if (go != null && ComponentUtil.IsAcceptableComponentType(typeof(T))) go.GetComponentsAlt<T>(set);
                if (go != null)
                {
                    var tp = typeof(T);
                    if (typeof(SPEntity).IsAssignableFrom(tp))
                    {
                        var entity = SPEntity.Pool.GetFromSource(tp, go) as T;
                        if (entity != null)
                        {
                            set.Add(entity);
                        }
                    }
                    else if (typeof(UnityEngine.GameObject).IsAssignableFrom(tp))
                    {
                        if (includeChildren)
                        {
                            using (var lst = TempCollection.GetList <UnityEngine.Transform>())
                            {
                                go.GetComponentsInChildren <UnityEngine.Transform>(lst);

                                var e = lst.GetEnumerator();
                                while (e.MoveNext())
                                {
                                    set.Add(e.Current.gameObject as T);
                                }
                            }
                        }
                    }
                    if (ComponentUtil.IsAcceptableComponentType(tp))
                    {
                        if (includeChildren)
                        {
                            go.GetChildComponents <T>(set, true);
                        }
                        else
                        {
                            go.GetComponentsAlt <T>(set);
                        }
                    }
                }

                return(set.Count > 0 ? set.ToArray() : ArrayUtil.Empty <T>());
            }
        }