コード例 #1
0
        ///Returns a new decorator instance for target type
        static T CreateDecorator <T>(Type targetType) where T : IDecorator
        {
            Type decoratorType = null;

            if (!decoratorsTypeMap.TryGetValue(targetType, out decoratorType))
            {
                Type directType     = null;
                Type assignableType = null;
                var  decoratorTypes = ReflectionTools.GetImplementationsOf(typeof(T));
                for (var i = 0; i < decoratorTypes.Length; i++)
                {
                    var current = decoratorTypes[i];
                    var att     = current.RTGetAttribute <DecoratorAttribute>(true);
                    if (att == null)
                    {
                        Debug.LogWarning(string.Format("Decorator type '{0}' does not has a Decorator Attribute.", current.Name));
                        continue;
                    }
                    if (att.targetType == targetType)
                    {
                        directType = current;
                    }
                    if (att.targetType.RTIsAssignableFrom(targetType))
                    {
                        assignableType = current;
                    }
                }
                decoratorType = directType != null ? directType : assignableType;
                decoratorsTypeMap[targetType] = decoratorType;
            }

            if (decoratorType == null)
            {
                Debug.LogError(string.Format("No '{0}' Decorator type found for target type '{1}'.", typeof(T), targetType));
                return(default(T));
            }
            if (decoratorType.IsGenericTypeDefinition)
            {
                decoratorType = decoratorType.MakeGenericType(targetType);
            }
            if (decoratorType.RTIsSubclassOf(typeof(ScriptableObject)))
            {
                return((T)(object)ScriptableObject.CreateInstance(decoratorType));
            }
            return((T)Activator.CreateInstance(decoratorType));
        }
コード例 #2
0
 ///Static init
 static AnimatedParameter()
 {
     parameterModelsMap = new Dictionary <Type, Type>();
     foreach (var type in ReflectionTools.GetImplementationsOf(typeof(IAnimatedParameterModel)))
     {
         var decAtt = type.RTGetAttribute <DecoratorAttribute>(false);
         if (decAtt != null)
         {
             parameterModelsMap[decAtt.targetType] = type;
         }
         else
         {
             Debug.LogError(string.Format("{0} is missing the Decorator attribute", type.Name));
         }
     }
     supportedTypes = parameterModelsMap.Keys.ToArray();
 }
コード例 #3
0
        //Get all non abstract derived types of base type in the current loaded assemplies
        public static List <TypeMetaInfo> GetTypeMetaDerivedFrom(Type baseType)
        {
            var infos = new List <TypeMetaInfo>();

            foreach (var type in ReflectionTools.GetImplementationsOf(baseType))
            {
                if (type.GetCustomAttributes(typeof(System.ObsoleteAttribute), true).FirstOrDefault() != null)
                {
                    continue;
                }

                var info = new TypeMetaInfo();
                info.type = type;

                var nameAtt = type.GetCustomAttributes(typeof(NameAttribute), true).FirstOrDefault() as NameAttribute;
                info.name = nameAtt != null ? nameAtt.name : type.Name.SplitCamelCase();

                var catAtt = type.GetCustomAttributes(typeof(CategoryAttribute), true).FirstOrDefault() as CategoryAttribute;
                if (catAtt != null)
                {
                    info.category = catAtt.category;
                }

                var attachAtt = type.GetCustomAttributes(typeof(AttachableAttribute), true).FirstOrDefault() as AttachableAttribute;
                if (attachAtt != null)
                {
                    info.attachableTypes = attachAtt.types;
                }

                info.isUnique = type.IsDefined(typeof(UniqueElementAttribute), true);

                infos.Add(info);
            }

            infos = infos.OrderBy(i => i.name).OrderBy(i => i.category).ToList();
            return(infos);
        }