示例#1
0
        private static T FindComponent <T>(GameObject obj, ComponentSearchMode mode) where T : Component
        {
            switch (mode)
            {
            case ComponentSearchMode.Self:
                return(obj.GetComponent <T>());

            case ComponentSearchMode.Parent:
                return(obj.GetComponentInParent <T>());

            case ComponentSearchMode.Children:
                return(obj.GetComponentInChildren <T>());
            }

            Debug.Assert(false);
            return(null);
        }
示例#2
0
        private static Dictionary <GameObject, Dictionary <System.Type, Component> > GetCache(ComponentSearchMode mode)
        {
            switch (mode)
            {
            case ComponentSearchMode.Self:
                return(s_selfCache);

            case ComponentSearchMode.Parent:
                return(s_parentCache);

            case ComponentSearchMode.Children:
                return(s_childrenCache);
            }

            Debug.Assert(false);
            return(null);
        }
示例#3
0
        private static T GetCachedComponentInternal <T>(GameObject obj, bool throwIfNotFound, ComponentSearchMode mode) where T : Component
        {
            var cache = GetCache(mode);

            Dictionary <System.Type, Component> behaviourCache;

            if (!cache.TryGetValue(obj, out behaviourCache))
            {
                behaviourCache = new Dictionary <System.Type, Component>();
                cache[obj]     = behaviourCache;
            }

            Component comp;

            if (!behaviourCache.TryGetValue(typeof(T), out comp))
            {
                comp = FindComponent <T>(obj, mode);

                if (comp == null && throwIfNotFound)
                {
                    throw new MissingComponentException($"Component of type '{typeof(T).Name}' not found");
                }

                behaviourCache[typeof(T)] = comp;
            }

            return(comp as T);
        }