コード例 #1
0
        /// <summary>
        /// Grab all the component instances that derive from the interface
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T[] FindComponentsOfType <T>()
        {
            Type lInterfaceType = typeof(T);

            Type[] lTypes = GetInterfaceTypes(lInterfaceType);
            if (lTypes == null || lTypes.Length == 0)
            {
                return(null);
            }

            List <T> lInstances = new List <T>();

            for (int i = 0; i < lTypes.Length; i++)
            {
                Type lType = lTypes[i];

                // Only grab components
#if !UNITY_EDITOR && (NETFX_CORE || WINDOWS_UWP || UNITY_WP8 || UNITY_WP_8_1 || UNITY_WSA || UNITY_WSA_8_0 || UNITY_WSA_8_1 || UNITY_WSA_10_0)
                if (lType.GetTypeInfo().IsAssignableFrom(typeof(Component).GetTypeInfo()))
#else
                if (lType.IsSubclassOf(typeof(Component)))
#endif
                {
                    lInstances.AddRange(Component.FindObjectsOfType(lType).Cast <T>());
                }
            }

            // Grab the distinct set of objects
            return(lInstances.Distinct().ToArray());
        }
コード例 #2
0
        /// <summary>
        /// Grab all the component instances that derive from the interface
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T[] FindComponentsOfType <T>()
        {
            Type lInterfaceType = typeof(T);

            Type[] lTypes = GetInterfaceTypes(lInterfaceType);
            if (lTypes == null || lTypes.Length == 0)
            {
                return(null);
            }

            List <T> lInstances = new List <T>();

            for (int i = 0; i < lTypes.Length; i++)
            {
                Type lType = lTypes[i];

                // Only grab components
                if (lType.IsSubclassOf(typeof(Component)))
                {
                    lInstances.AddRange(Component.FindObjectsOfType(lType).Cast <T>());
                }
            }

            // Grab the distinct set of objects
            return(lInstances.Distinct().ToArray());
        }