Inheritance: System.Attribute
        /// <summary>
        /// Returns the custom drawer for the supplied property attribute or null.
        /// <param name="attribute">The property attribute to search for a custom drawer.</param>
        /// <returns>The custom property drawer or null.</returns>
        /// </summary>
        public static NodePropertyDrawer GetDrawer (PropertyAttribute attribute) {
            if (attribute == null)
                return null;

            // The custom drawer dictionary is not loaded?
            if (s_CustomDrawers == null) {
                s_CustomDrawers = new Dictionary<Type, Type>();
                foreach (Type t in EditorTypeUtility.GetDerivedTypes(typeof(NodePropertyDrawer))) {
                    var customDrawerAttr = AttributeUtility.GetAttribute<CustomNodePropertyDrawerAttribute>(t, false);
                    if (customDrawerAttr != null && !s_CustomDrawers.ContainsKey(customDrawerAttr.type))
                        s_CustomDrawers.Add(customDrawerAttr.type, t);
                }
            }

            // Try to get the type of the custom property drawer
            Type drawerType;
            s_CustomDrawers.TryGetValue(attribute.GetType(), out drawerType);
            if (drawerType != null) {
                // Create the drawer
                var drawer = Activator.CreateInstance(drawerType) as NodePropertyDrawer;
                if (drawer != null) {
                    // Set the attribute and return the drawer
                    drawer.attribute = attribute;
                    return drawer;
                }
            }

            return null;
        }
            public ArrayChildren(FieldInfo fieldInfo, UnityEngine.PropertyAttribute attribute) : base(fieldInfo, attribute)
            {
                if (baseMethodInfo == null)
                {
                    //looking for T[] Component.GetComponentsInChildren<T>(bool includeInactive);
                    foreach (var methodInfo in typeof(Component).GetMethods())
                    {
                        if (methodInfo.Name == "GetComponentsInChildren" && methodInfo.ContainsGenericParameters &&
                            methodInfo.GetParameters().Length == 1 &&
                            methodInfo.GetParameters()[0].ParameterType == typeof(bool))
                        {
                            baseMethodInfo = methodInfo;
                            break;
                        }
                    }
                    Assert.IsNotNull(baseMethodInfo);
                }
                //generate method info for T = ElementType
                _genericMethodInfo = baseMethodInfo.MakeGenericMethod(ElementType());

                //create method parameter list
                var getComponentProcessorAttribute = attribute as GetComponentProcessorAttribute;

                _includeInactive = getComponentProcessorAttribute.IncludeInactive;
            }
		public void HandleDrawnType(Type drawnType, Type propertyType, FieldInfo field, PropertyAttribute attribute)
		{
			Type drawerTypeForType = ScriptAttributeUtility.GetDrawerTypeForType(drawnType);
			if (drawerTypeForType != null)
			{
				if (typeof(PropertyDrawer).IsAssignableFrom(drawerTypeForType))
				{
					if (propertyType != null && propertyType.IsArrayOrList())
					{
						return;
					}
					this.m_PropertyDrawer = (PropertyDrawer)Activator.CreateInstance(drawerTypeForType);
					this.m_PropertyDrawer.m_FieldInfo = field;
					this.m_PropertyDrawer.m_Attribute = attribute;
				}
				else
				{
					if (typeof(DecoratorDrawer).IsAssignableFrom(drawerTypeForType))
					{
						if (field != null && field.FieldType.IsArrayOrList() && !propertyType.IsArrayOrList())
						{
							return;
						}
						DecoratorDrawer decoratorDrawer = (DecoratorDrawer)Activator.CreateInstance(drawerTypeForType);
						decoratorDrawer.m_Attribute = attribute;
						if (this.m_DecoratorDrawers == null)
						{
							this.m_DecoratorDrawers = new List<DecoratorDrawer>();
						}
						this.m_DecoratorDrawers.Add(decoratorDrawer);
					}
				}
			}
		}
 private static void AddBuiltinAttribute(string componentTypeName, string propertyPath, PropertyAttribute attr)
 {
   string key = componentTypeName + "_" + propertyPath;
   if (!ScriptAttributeUtility.s_BuiltinAttributes.ContainsKey(key))
     ScriptAttributeUtility.s_BuiltinAttributes.Add(key, new List<PropertyAttribute>());
   ScriptAttributeUtility.s_BuiltinAttributes[key].Add(attr);
 }
        public MultiPropertyAttributePropertyHandler(System.Reflection.FieldInfo fieldInfo, PropertyAttribute[] attribs)
        {
            if (fieldInfo == null) throw new System.ArgumentNullException("fieldInfo");
            if (attribs == null) throw new System.ArgumentNullException("attribs");
            _fieldInfo = fieldInfo;

            this.Init(attribs);
        }
Beispiel #6
0
 public static void SetAttribute( UnityEditor.PropertyDrawer drawer, PropertyAttribute attribute )
 {
     FieldInfo fi = TargetType.GetField( "m_Attribute", BindingFlags.NonPublic | BindingFlags.Instance );
     if( fi == null ){
         Debug.Log( "Error, FieldInfo not found" );
         return;
     }
     fi.SetValue( drawer, attribute );
 }
 public void HandleAttribute(PropertyAttribute attribute, System.Reflection.FieldInfo field, System.Type propertyType)
 {
   if (attribute is TooltipAttribute)
     this.tooltip = (attribute as TooltipAttribute).tooltip;
   else if (attribute is ContextMenuItemAttribute)
   {
     if (propertyType.IsArrayOrList())
       return;
     if (this.contextMenuItems == null)
       this.contextMenuItems = new List<ContextMenuItemAttribute>();
     this.contextMenuItems.Add(attribute as ContextMenuItemAttribute);
   }
   else
     this.HandleDrawnType(attribute.GetType(), propertyType, field, attribute);
 }
        protected virtual void Init(PropertyAttribute[] attribs)
        {
            var fieldType = _fieldInfo.FieldType;
            if (fieldType.IsListType()) fieldType = fieldType.GetElementTypeOfListType();

            var fieldTypePropertyDrawerType = ScriptAttributeUtility.GetDrawerTypeForType(fieldType);
            if (fieldTypePropertyDrawerType != null && TypeUtil.IsType(fieldTypePropertyDrawerType, typeof(PropertyDrawer)))
            {
                _drawer = PropertyDrawerActivator.Create(fieldTypePropertyDrawerType, null, _fieldInfo);
                if (_drawer != null && _fieldInfo.FieldType.IsListType()) _drawer = new ArrayPropertyDrawer(_drawer);
            }


            foreach(var attrib in attribs)
            {
                this.HandleAttribute(attrib, _fieldInfo, fieldType);
            }
        }
 public ListSelf(FieldInfo fieldInfo, UnityEngine.PropertyAttribute attribute) : base(fieldInfo, attribute)
 {
     if (baseMethodInfo == null)
     {
         //looking for void Component.GetComponents<T>(List<T> results);
         foreach (var methodInfo in typeof(Component).GetMethods())
         {
             if (methodInfo.Name == "GetComponents" && methodInfo.ContainsGenericParameters && methodInfo.GetParameters().Length == 1)
             {
                 baseMethodInfo = methodInfo;
                 break;
             }
         }
         Assert.IsNotNull(baseMethodInfo);
     }
     //generate method info for T = ElementType
     _genericMethodInfo = baseMethodInfo.MakeGenericMethod(ElementType());
 }
 protected override void HandleAttribute(PropertyAttribute attribute, System.Reflection.FieldInfo field, System.Type propertyType)
 {
     if(attribute is PropertyModifierAttribute)
     {
         var mtp = ScriptAttributeUtility.GetDrawerTypeForType(attribute.GetType());
         if (TypeUtil.IsType(mtp, typeof(PropertyModifier)))
         {
             var modifier = PropertyDrawerActivator.Create(mtp, attribute, field) as PropertyModifier;
             modifier.Init(false);
             if (_modifiers == null) _modifiers = new List<PropertyModifier>();
             _modifiers.Add(modifier);
         }
     }
     else
     {
         base.HandleAttribute(attribute, field, propertyType);
         var drawer = this.InternalDrawer; //this retrieves the drawer that was selected by called 'base.HandleAttribute'
         this.AppendDrawer(drawer);
     }
 }
		public void HandleAttribute(PropertyAttribute attribute, FieldInfo field, Type propertyType)
		{
			if (attribute is TooltipAttribute)
			{
				this.tooltip = (attribute as TooltipAttribute).tooltip;
				return;
			}
			if (!(attribute is ContextMenuItemAttribute))
			{
				this.HandleDrawnType(attribute.GetType(), propertyType, field, attribute);
				return;
			}
			if (propertyType.IsArrayOrList())
			{
				return;
			}
			if (this.contextMenuItems == null)
			{
				this.contextMenuItems = new List<ContextMenuItemAttribute>();
			}
			this.contextMenuItems.Add(attribute as ContextMenuItemAttribute);
		}
 public void HandleDrawnType(System.Type drawnType, System.Type propertyType, System.Reflection.FieldInfo field, PropertyAttribute attribute)
 {
   System.Type drawerTypeForType = ScriptAttributeUtility.GetDrawerTypeForType(drawnType);
   if (drawerTypeForType == null)
     return;
   if (typeof (PropertyDrawer).IsAssignableFrom(drawerTypeForType))
   {
     if (propertyType != null && propertyType.IsArrayOrList())
       return;
     this.m_PropertyDrawer = (PropertyDrawer) Activator.CreateInstance(drawerTypeForType);
     this.m_PropertyDrawer.m_FieldInfo = field;
     this.m_PropertyDrawer.m_Attribute = attribute;
   }
   else
   {
     if (!typeof (DecoratorDrawer).IsAssignableFrom(drawerTypeForType) || field != null && field.FieldType.IsArrayOrList() && !propertyType.IsArrayOrList())
       return;
     DecoratorDrawer instance = (DecoratorDrawer) Activator.CreateInstance(drawerTypeForType);
     instance.m_Attribute = attribute;
     if (this.m_DecoratorDrawers == null)
       this.m_DecoratorDrawers = new List<DecoratorDrawer>();
     this.m_DecoratorDrawers.Add(instance);
   }
 }
 public static void InitializePropertyDrawer(PropertyDrawer drawer, PropertyAttribute attrib, System.Reflection.FieldInfo fieldInfo)
 {
     if (drawer == null) throw new System.ArgumentNullException("drawer");
     ObjUtil.SetValue(drawer, "m_Attribute", attrib);
     ObjUtil.SetValue(drawer, "m_FieldInfo", fieldInfo);
 }
 public static PropertyDrawer Create(System.Type propertyDrawerType, PropertyAttribute attrib, System.Reflection.FieldInfo fieldInfo)
 {
     var drawer = System.Activator.CreateInstance(propertyDrawerType) as PropertyDrawer;
     if(drawer != null) InitializePropertyDrawer(drawer, attrib, fieldInfo);
     return drawer;
 }
Beispiel #15
0
        /// <summary>
        /// Returns a set of serialized properties in an array.
        /// <param name="serializedNode">The target serialized node.</param>
        /// <param name="target">The target object (node, variable or generic object).</param>
        /// <param name="targetArray">The target array.</param>
        /// <param name="type">The elements type in the array.</param>
        /// <param name="currentPath">The property path of the target array.</param>
        /// <param name="variableInfo">The variable info in the array or null.</param>
        /// <param name="propertyAttr">The property attribute in the array.</param>
        /// <returns>The serialized properties in the array.</returns>
        /// </summary>
        static SerializedNodeProperty[] GetPropertiesData (SerializedNode serializedNode, object target, Array targetArray, Type type, string currentPath, VariableInfoAttribute variableInfo, PropertyAttribute propertyAttr) {
            // Create the property data list
            var propertyData = new List<SerializedNodeProperty>();
            // Get the property type
            var propertyType = SerializedNode.GetPropertyType(type);

            if (targetArray != null) {
                // Variable?
                if (propertyType == NodePropertyType.Variable) {
                    for (int i = 0; i < targetArray.Length; i++) {
                        // Get the field value
                        object elementValue = targetArray.GetValue(i);
                        // Create the variable data
                        var variableData = new SerializedArrayElement(target, serializedNode, currentPath + "data[" + i.ToString() + "]", type, propertyType, targetArray, i, variableInfo);

                        // Create the variable children
                        SerializedNodeProperty[] children = SerializedNode.GetPropertiesData(serializedNode, elementValue, elementValue != null ? elementValue.GetType() : type, variableData.path + ".");

                        // Create the property drawer for the "Value" child property
                        if (propertyAttr != null && variableData.isConcreteVariable) {
                            foreach (var child in children) {
                                // It is the "Value" property?
                                if (child.label == "Value")
                                    child.customDrawer = NodePropertyDrawer.GetDrawer(propertyAttr);
                            }
                        }

                        // Set children
                        variableData.SetChildren(children);

                        // Add the variable data to the list
                        propertyData.Add(variableData);
                    }
                }
               // Array?
                else if (propertyType == NodePropertyType.Array) {
                    for (int i = 0; i < targetArray.Length; i++) {
                        // Create the current property data
                        var currentPropertyData = new SerializedArrayElement(target, serializedNode, currentPath + "data[" + i.ToString() + "]" , type, propertyType, targetArray, i, variableInfo);
                        // Get the array value
                        var array = targetArray.GetValue(i) as Array;
                        // Get array element type
                        var elementType = type.GetElementType();
                        // Create the array children list
                        var childrenList = new List<SerializedNodeProperty>();

                        // Create the array size
                        childrenList.Add(new SerializedArraySize(target, serializedNode, currentPropertyData.path + ".size", currentPropertyData, array, elementType));
                        // Create array children
                        childrenList.AddRange(SerializedNode.GetPropertiesData(serializedNode, target, array, elementType, currentPropertyData.path + ".", variableInfo, propertyAttr));

                        // Set array data children
                        currentPropertyData.SetChildren(childrenList.ToArray());

                        // Add to list
                        propertyData.Add(currentPropertyData);
                    }
                }
                else {
                    for (int i = 0; i < targetArray.Length; i++) {
                        // Create the current property data
                        var currentPropertyData = new SerializedArrayElement(target, serializedNode, currentPath + "data[" + i.ToString() + "]" , type, propertyType, targetArray, i, variableInfo);
                        // Try to get a property drawer
                        if (propertyAttr != null)
                            currentPropertyData.customDrawer = NodePropertyDrawer.GetDrawer(propertyAttr);
                        // Add to list
                        propertyData.Add(currentPropertyData);
                    }
                }
            }

            return propertyData.ToArray();
        }
 protected virtual void HandleAttribute(PropertyAttribute attribute, System.Reflection.FieldInfo field, System.Type propertyType)
 {
     if (_imp_HandleAttribute == null) _imp_HandleAttribute = _internalPropertyHandler.GetMethod("HandleAttribute", typeof(System.Action<PropertyAttribute, System.Reflection.FieldInfo, System.Type>)) as System.Action<PropertyAttribute, System.Reflection.FieldInfo, System.Type>;
     _imp_HandleAttribute(attribute, field, propertyType);
 }
 public InternalProcessor(FieldInfo fieldInfo, UnityEngine.PropertyAttribute attribute)
 {
     _fieldInfo = fieldInfo;
 }
 private void Init(PropertyAttribute[] attribs)
 {
     var fieldType = _fieldInfo.FieldType;
     if (fieldType.IsListType()) fieldType = fieldType.GetElementTypeOfListType();
     foreach(var attrib in attribs)
     {
         this.HandleAttribute(attrib, _fieldInfo, fieldType);
     }
 }
 public FieldParent(FieldInfo fieldInfo, UnityEngine.PropertyAttribute attribute) : base(fieldInfo, attribute)
 {
 }
            public FieldChildren(FieldInfo fieldInfo, UnityEngine.PropertyAttribute attribute) : base(fieldInfo, attribute)
            {
                var getComponentProcessorAttribute = attribute as GetComponentProcessorAttribute;

                _includeInactive = getComponentProcessorAttribute.IncludeInactive;
            }
 protected override void HandleAttribute(PropertyAttribute attribute, System.Reflection.FieldInfo field, System.Type propertyType)
 {
     if(attribute is PropertyModifierAttribute)
     {
         var mtp = ScriptAttributeUtility.GetDrawerTypeForType(attribute.GetType());
         if (TypeUtil.IsType(mtp, typeof(PropertyModifier)))
         {
             var modifier = PropertyDrawerActivator.Create(mtp, attribute, field) as PropertyModifier;
             modifier.Init(false);
             if (_modifiers == null) _modifiers = new List<PropertyModifier>();
             _modifiers.Add(modifier);
         }
     }
     else
     {
         base.HandleAttribute(attribute, field, propertyType);
         var drawer = this.InternalDrawer;
         if (_drawer == null)
         {
             //no drawer has been set before... lets see if we got one
             if (drawer != null)
             {
                 //we got a new drawer, set it
                 if (field.FieldType.IsListType()) drawer = new ArrayPropertyDrawer(drawer);
                 _drawer = drawer;
             }
         }
         else if (drawer != _drawer)
         {
             //a new drawer was created, lets see what we need to do with it compared to the last one
             if (drawer is PropertyModifier)
             {
                 if (_modifiers == null) _modifiers = new List<PropertyModifier>();
                 _modifiers.Add(drawer as PropertyModifier);
                 this.InternalDrawer = _drawer;
             }
             else if (drawer is IArrayHandlingPropertyDrawer)
             {
                 //got an array drawer, this overrides previous drivers
                 if (_drawer is IArrayHandlingPropertyDrawer)
                 {
                     var temp = _drawer as IArrayHandlingPropertyDrawer;
                     _drawer = drawer;
                     (_drawer as IArrayHandlingPropertyDrawer).InternalDrawer = temp.InternalDrawer;
                 }
                 else if (_drawer != null)
                 {
                     var temp = _drawer;
                     _drawer = drawer;
                     (_drawer as IArrayHandlingPropertyDrawer).InternalDrawer = temp;
                 }
                 else
                 {
                     _drawer = drawer;
                 }
             }
             else if (_drawer is IArrayHandlingPropertyDrawer)
             {
                 //got an internal drawer for the existing array drawer
                 (_drawer as IArrayHandlingPropertyDrawer).InternalDrawer = drawer;
                 this.InternalDrawer = _drawer;
             }
             else
             {
                 //we got a new drawer, set it
                 if (field.FieldType.IsListType())
                 {
                     _drawer = new ArrayPropertyDrawer(drawer);
                     this.InternalDrawer = _drawer;
                 }
                 else
                 {
                     _drawer = drawer;
                 }
             }
         }
     }
 }
        /// <summary>
        /// Constructor, generate method info appropriately
        /// </summary>
        /// <param name="fieldInfo"></param>
        /// <param name="attribute"></param>
        public GetComponentProcessor(FieldInfo fieldInfo, UnityEngine.PropertyAttribute attribute) : base(fieldInfo, attribute)
        {
            var getComponentProcessorAttribute = attribute as GetComponentProcessorAttribute;

            //arrays and lists are treated differently
            if (fieldInfo.FieldType.IsArray)
            {
                switch (getComponentProcessorAttribute.Source)
                {
                case Source.Self:
                    _internalProcessor = new ArraySelf(fieldInfo, attribute);
                    break;

                case Source.Children:
                    _internalProcessor = new ArrayChildren(fieldInfo, attribute);
                    break;

                case Source.Parent:
                    _internalProcessor = new ArrayParent(fieldInfo, attribute);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            else if (fieldInfo.FieldType.IsList())
            {
                switch (getComponentProcessorAttribute.Source)
                {
                case Source.Self:
                    _internalProcessor = new ListSelf(fieldInfo, attribute);
                    break;

                case Source.Children:
                    _internalProcessor = new ListChildren(fieldInfo, attribute);
                    break;

                case Source.Parent:
                    _internalProcessor = new ListParent(fieldInfo, attribute);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            else
            {
                switch (getComponentProcessorAttribute.Source)
                {
                case Source.Self:
                    _internalProcessor = new FieldSelf(fieldInfo, attribute);
                    break;

                case Source.Children:
                    _internalProcessor = new FieldChildren(fieldInfo, attribute);
                    break;

                case Source.Parent:
                    _internalProcessor = new FieldParent(fieldInfo, attribute);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }