Example #1
0
        public static T GetAsFromSource <T>(object obj) where T : class
        {
            obj = obj.SanitizeRef();
            if (obj == null)
            {
                return(null);
            }
            if (obj is T)
            {
                return(obj as T);
            }
            if (obj is IComponent)
            {
                var c = (obj as IComponent).component;
                if (c is T)
                {
                    return(c as T);
                }
            }
            var go = GameObjectUtil.GetGameObjectFromSource(obj);

            if (go is T)
            {
                return(go as T);
            }

            //if (go != null && ComponentUtil.IsAcceptableComponentType(typeof(T))) return go.GetComponentAlt<T>();
            if (go != null)
            {
                var tp = typeof(T);
                if (typeof(SPEntity).IsAssignableFrom(tp))
                {
                    return(SPEntity.Pool.GetFromSource(tp, go) as T);
                }
                else if (ComponentUtil.IsAcceptableComponentType(tp))
                {
                    return(go.GetComponent(tp) as T);
                }
            }

            return(null);
        }
        public static string GetCachedName(UnityEngine.Object obj)
        {
            if (obj == null)
            {
                return(null);
            }
            if (obj is INameable)
            {
                return((obj as INameable).Name);
            }

            var go = GameObjectUtil.GetGameObjectFromSource(obj);

            if (go != null)
            {
                return(go.AddOrGetComponent <GameObjectNameCache>().Name);
            }

            return(obj.name);
        }
        /// <summary>
        /// Attempts to reduce garbage when comparing names.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool CompareName(this UnityEngine.Object obj, string name)
        {
            if (obj == null)
            {
                return(false);
            }

            if (obj is INameable)
            {
                return((obj as INameable).CompareName(name));
            }

            var go = GameObjectUtil.GetGameObjectFromSource(obj);

            if (go != null)
            {
                return(go.AddOrGetComponent <GameObjectNameCache>().CompareName(name));
            }

            return(obj.name == name);
        }
Example #4
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>());
            }
        }
Example #5
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.GetComponents <T>(set);
                        }
                    }
                }

                return(set.Count > 0 ? set.ToArray() : ArrayUtil.Empty <T>());
            }
        }
Example #6
0
        public static bool GetAsFromSource(System.Type tp, object obj, out object result, bool respectProxy = false)
        {
            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);
        }
Example #7
0
        public static bool GetAsFromSource <T>(object obj, out T result, bool respectProxy = false) where T : class
        {
            result = null;
            if (obj == null)
            {
                return(false);
            }

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

            if (obj is T)
            {
                result = obj as T;
                return(true);
            }
            if (obj is IComponent)
            {
                var c = (obj as IComponent).component;
                if (c is T)
                {
                    result = c as T;
                    return(true);
                }
            }
            var go = GameObjectUtil.GetGameObjectFromSource(obj);

            if (go is T)
            {
                result = go as T;
                return(true);
            }

            //if (go != null && ComponentUtil.IsAcceptableComponentType(typeof(T))) return go.GetComponentAlt<T>();
            if (go != null)
            {
                var tp = typeof(T);
                if (typeof(SPEntity).IsAssignableFrom(tp))
                {
                    var uobj = SPEntity.Pool.GetFromSource(tp, go);
                    if (uobj == null)
                    {
                        return(false);
                    }

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

                    result = uobj as T;
                    return(result != null);
                }
            }

            return(false);
        }