示例#1
0
        public static bool GetAsFromSource(System.Type tp, object obj, out object result, bool respectProxy = false)
        {
            obj    = obj.SanitizeRef();
            result = null;
            if (obj == null)
            {
                return(false);
            }

            if (respectProxy && obj is IProxy)
            {
                obj = (obj as IProxy).GetTarget();
                if (obj == null)
                {
                    return(false);
                }
            }

            var otp = obj.GetType();

            if (TypeUtil.IsType(otp, tp))
            {
                result = obj;
                return(true);
            }
            if (obj is IComponent)
            {
                var c = (obj as IComponent).component;
                if (!object.ReferenceEquals(c, null) && TypeUtil.IsType(c.GetType(), tp))
                {
                    result = c;
                    return(true);
                }
            }

            var go = GameObjectUtil.GetGameObjectFromSource(obj);

            if (tp == typeof(UnityEngine.GameObject))
            {
                result = go;
                return(true);
            }

            if (go != null)
            {
                if (typeof(SPEntity).IsAssignableFrom(tp))
                {
                    var uobj = SPEntity.Pool.GetFromSource(tp, go);
                    if (uobj == null)
                    {
                        return(false);
                    }

                    result = uobj;
                    return(true);
                }
                else if (ComponentUtil.IsAcceptableComponentType(tp))
                {
                    var uobj = go.GetComponent(tp);
                    if (uobj == null)
                    {
                        return(false);
                    }

                    result = uobj;
                    return(true);
                }
            }

            return(false);
        }
示例#2
0
        public static object[] GetAllFromSource(System.Type tp, object obj, bool includeChildren = false)
        {
            obj = obj.SanitizeRef();
            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>());
            }
        }
示例#3
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);
            }
        }