public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (!_toShow)
            {
                return;
            }

            if (_multipleAttributes && _genericDrawerInstance != null)
            {
                try {
                    _genericDrawerInstance.OnGUI(position, property, label);
                } catch (Exception e) {
                    EditorGUI.PropertyField(position, property, label);
                    LogWarning("Unable to instantiate " + _genericAttribute.GetType() + " : " + e, property);
                }
            }
            else if (_specialType && _genericTypeDrawerInstance != null)
            {
                try {
                    _genericTypeDrawerInstance.OnGUI(position, property, label);
                } catch (Exception e) {
                    EditorGUI.PropertyField(position, property, label);
                    LogWarning("Unable to instantiate " + _genericType + " : " + e, property);
                }
            }
            else
            {
                EditorGUI.PropertyField(position, property, label);
            }
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            Initialize(property);
            bool visible = PropertyIsVisible(property, Conditional.m_CompareValues);

            m_Show = Conditional.m_Inverse ^ visible;
            if (!m_Show)
            {
                return;
            }

            if (_multipleAttributes && _genericAttributeDrawerInstance != null)
            {
                try {
                    _genericAttributeDrawerInstance.OnGUI(position, property, label);
                } catch (Exception e) {
                    EditorGUI.PropertyField(position, property, label);
                    LogWarning("Unable to instantiate " + _genericAttribute.GetType() + " : " + e, property);
                }
            }
            else if (_specialType && _genericTypeDrawerInstance != null)
            {
                try {
                    _genericTypeDrawerInstance.OnGUI(position, property, label);
                } catch (Exception e) {
                    EditorGUI.PropertyField(position, property, label);
                    LogWarning("Unable to instantiate " + _genericType + " : " + e, property);
                }
            }
            else
            {
                EditorGUI.PropertyField(position, property, label, true);
            }
        }
Example #3
0
 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;
             if (_modifiers == null)
             {
                 _modifiers = new List <PropertyModifier>();
             }
             _modifiers.Add(modifier);
         }
     }
     else if (attribute is DisplayNameAttribute)
     {
         var dattrib = attribute as DisplayNameAttribute;
         _customDisplayName = dattrib.DisplayName;
         if (dattrib.tooltip != null)
         {
             _customTooltip = dattrib.tooltip;
             base.HandleAttribute(attribute, field, propertyType);
         }
     }
     else if (attribute is TooltipAttribute)
     {
         _customTooltip = (attribute as TooltipAttribute).tooltip;
         base.HandleAttribute(attribute, field, propertyType);
     }
     else if (attribute is ContextMenuItemAttribute)
     {
         base.HandleAttribute(attribute, field, propertyType);
     }
     else
     {
         var drawerTypeForType = ScriptAttributeUtility.GetDrawerTypeForType(attribute.GetType());
         if (drawerTypeForType == null)
         {
             return;
         }
         else if (typeof(PropertyDrawer).IsAssignableFrom(drawerTypeForType))
         {
             base.HandleAttribute(attribute, field, propertyType);
             var drawer = this.InternalDrawer; //this retrieves the drawer that was selected by called 'base.HandleAttribute'
             this.AppendDrawer(drawer);
         }
         else if (typeof(DecoratorDrawer).IsAssignableFrom(drawerTypeForType))
         {
             DecoratorDrawer instance = (DecoratorDrawer)System.Activator.CreateInstance(drawerTypeForType);
             com.spacepuppy.Dynamic.DynamicUtil.SetValue(instance, "m_Attribute", attribute);
             if (this.DecoratorDrawers == null)
             {
                 this.DecoratorDrawers = new List <DecoratorDrawer>();
             }
             this.DecoratorDrawers.Add(instance);
         }
     }
 }
Example #4
0
        /// <summary>
        /// Gets the property drawer for the supplied field.
        /// </summary>
        /// <returns>The property drawer for the supplied field.</returns>
        /// <param name="field">Field.</param>
        /// <param name="propertyAttribute">Property attribute.</param>
        public static PropertyDrawer GetPropertyDrawer(this FieldInfo field, PropertyAttribute propertyAttribute)
        {
            System.Type fieldType             = GetIListElementType(field.FieldType) ?? field.FieldType;
            System.Type propertyAttributeType = propertyAttribute == null ? null : propertyAttribute.GetType();
            // early out of there is no PropertyDrawer
            if (
                !(
                    drawersForEachType.ContainsKey(fieldType) ||
                    (propertyAttributeType != null && drawersForEachType.ContainsKey(propertyAttributeType))
                    )
                )
            {
                return(null);
            }
            // instantiate the new property drawer
            bool            isTypeDrawer = drawersForEachType.ContainsKey(fieldType);
            ConstructorInfo constructor  = drawersForEachType[
                isTypeDrawer ? fieldType : propertyAttributeType
                                           ].GetConstructor(new System.Type[0]);
            PropertyDrawer result = constructor.Invoke(null) as PropertyDrawer;

            // configure the property drawer's private fields
            if (isTypeDrawer)
            {
                propertyDrawerAttributeField.SetValue(result, null);
            }
            else
            {
                propertyDrawerAttributeField.SetValue(result, propertyAttribute);
            }
            propertyDrawerFieldInfoField.SetValue(result, field);
            return(result);
        }
        public void HandleAttribute(PropertyAttribute attribute, FieldInfo field, Type propertyType)
        {
            if (attribute is TooltipAttribute)
            {
                tooltip = (attribute as TooltipAttribute).tooltip;
                return;
            }

            if (attribute is ContextMenuItemAttribute)
            {
                // Use context menu items on array elements, not on array itself
                if (propertyType.IsArrayOrList())
                {
                    return;
                }
                if (contextMenuItems == null)
                {
                    contextMenuItems = new List <ContextMenuItemAttribute>();
                }
                contextMenuItems.Add(attribute as ContextMenuItemAttribute);
                return;
            }

            // Look for its drawer type of this attribute
            HandleDrawnType(attribute.GetType(), propertyType, field, attribute);
        }
Example #6
0
        /// <summary>
        /// Gets the GUI drawer for the supplied field.
        /// </summary>
        /// <returns>The GUI drawer for the supplied field.</returns>
        /// <param name="field">Field.</param>
        /// <param name="propertyAttribute">
        /// Property attribute. If null, this method will return a default drawer for the field type, if one exists.
        /// </param>
        public static GUIDrawer GetGUIDrawer(FieldInfo field, PropertyAttribute propertyAttribute)
        {
            GUIDrawer result = null;

            if (field == null)
            {
                return(result);
            }
            System.Type fieldType = GetIListElementType(field.FieldType) ?? field.FieldType;
            List <Dictionary <System.Type, System.Type> > registrationTables =
                new List <Dictionary <System.Type, System.Type> >(
                    new Dictionary <System.Type, System.Type>[] { s_DecoratorsForEachType, s_DrawersForEachType }
                    );

            System.Type propertyAttributeType = propertyAttribute == null ? null : propertyAttribute.GetType();
            foreach (Dictionary <System.Type, System.Type> registrationTable in registrationTables)
            {
                // skip if there is no GUIDrawer for this type in the registration table
                if (
                    !(
                        registrationTable.ContainsKey(fieldType) ||
                        (propertyAttributeType != null && registrationTable.ContainsKey(propertyAttributeType))
                        )
                    )
                {
                    continue;
                }
                // instantiate the new GUIDrawer
                bool isTypeDrawer = registrationTable.ContainsKey(fieldType);
                if (isTypeDrawer && propertyAttributeType != null)
                {
                    isTypeDrawer = !registrationTable.ContainsKey(propertyAttributeType);
                }
                ConstructorInfo constructor = registrationTable[
                    isTypeDrawer ? fieldType : propertyAttributeType
                                              ].GetConstructor(new System.Type[0]);
                result = constructor.Invoke(null) as GUIDrawer;
                // configure the drawer's private fields
                if (result is PropertyDrawer)
                {
                    if (isTypeDrawer)
                    {
                        s_PropertyDrawerAttributeField.SetValue(result, null);
                    }
                    else
                    {
                        s_PropertyDrawerAttributeField.SetValue(result, propertyAttribute);
                    }
                    s_PropertyDrawerFieldInfoField.SetValue(result, field);
                }
                else if (result is GUIDrawer)
                {
                    s_DecoratorDrawerAttributeField.SetValue(result, propertyAttribute);
                }
            }
            return(result);
        }
Example #7
0
        internal static void ErrorMessage(Rect position, GUIContent label, PropertyAttribute customVector, string compatibleTypes)
        {
            string customVectorTypeName = customVector.GetType().Name;
            string attributeText        = "Attribute";
            int    attributeIndex       = customVectorTypeName.IndexOf(attributeText);
            string customVectorName     = customVectorTypeName.Remove(attributeIndex);

            EditorGUIUnity.LabelField(position, label.text, "Use " + customVectorName + " with " + compatibleTypes + ".");
        }
    public static PropertyDrawer CreatePropertyDrawerInstance(PropertyAttribute propertyAttribute, FieldInfo fieldInfo, PropertyAttribute attribute)
    {
        Type drawerType     = GetDrawerTypeForType(propertyAttribute.GetType());
        var  propertyDrawer = Activator.CreateInstance(drawerType) as PropertyDrawer;

        if (propertyDrawer == null)
        {
            return(null);
        }
        typeof(PropertyDrawer).GetField("m_FieldInfo", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(propertyDrawer, fieldInfo);
        typeof(PropertyDrawer).GetField("m_Attribute", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(propertyDrawer, attribute);
        return(propertyDrawer);
    }
Example #9
0
 public static Type GetPropertyAttribute(object[] attributes)
 {
     object[] objArray = attributes;
     for (int i = 0; i < (int)objArray.Length; i++)
     {
         PropertyAttribute propertyAttribute = objArray[i] as PropertyAttribute;
         if (propertyAttribute != null)
         {
             return(propertyAttribute.GetType());
         }
     }
     return(null);
 }
Example #10
0
 public static bool HasReorderableAttribute(SerializedProperty property)
 {
     PropertyAttribute[] propertyAttributes = ReorderableArrayEditor.GetPropertyAttributes <PropertyAttribute>(property);
     if (propertyAttributes != null)
     {
         PropertyAttribute[] array = propertyAttributes;
         for (int i = 0; i < array.Length; i++)
         {
             PropertyAttribute propertyAttribute = array[i];
             if (propertyAttribute.GetType().Name == "ReorderableAttribute")
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Example #11
0
        public void HandleAttribute(SerializedProperty property, PropertyAttribute attribute, FieldInfo field, Type propertyType)
        {
            if (attribute is TooltipAttribute)
            {
                tooltip = (attribute as TooltipAttribute).tooltip;
                return;
            }

            if (attribute is ContextMenuItemAttribute)
            {
                if (contextMenuItems == null)
                {
                    contextMenuItems = new List <ContextMenuItemAttribute>();
                }
                contextMenuItems.Add(attribute as ContextMenuItemAttribute);
                return;
            }

            // Look for its drawer type of this attribute
            HandleDrawnType(property, attribute.GetType(), propertyType, field, attribute);
        }
Example #12
0
 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;
             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);
     }
 }
Example #13
0
        private void HandleNewAttribute(PropertyAttribute attribute)
        {
            if (!isChild)
            {
                //NOTE: setting tooltip and labels is valid only for parent or single properties
                //it's a bit ugly but, it's the only semi-acceptable way to support built-in TooltipAttribute
                switch (attribute)
                {
                case TooltipAttribute a:
                    label.tooltip = a.tooltip;
                    return;

                case NewLabelAttribute a:
                    label.text = a.NewLabel;
                    return;
                }
            }

            var attributeType = attribute.GetType();

            CheckIfPropertyHasPropertyDrawer(attributeType);
        }
Example #14
0
 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);
 }
 /// <summary>
 /// Extract attribute data for property
 /// </summary>
 public void HandleAttribute(PropertyAttribute attribute, FieldInfo field, Type propertyType)
 {
     if (attribute is TooltipAttribute)
     {
         tooltip = (attribute as TooltipAttribute).tooltip;
     }
     else if (attribute is ContextMenuItemAttribute)
     {
         if (!propertyType.IsArrayOrList())
         {
             if (contextMenuItems == null)
             {
                 contextMenuItems = new List <ContextMenuItemAttribute>();
             }
             contextMenuItems.Add(attribute as ContextMenuItemAttribute);
         }
     }
     else
     {
         HandleDrawnType(attribute.GetType(), propertyType, field, attribute);
     }
 }
Example #16
0
        /// <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);
        }
 /// <summary>
 /// Logs to debug console message relative to given property and attribute.
 /// </summary>
 /// <param name="property"></param>
 /// <param name="message"></param>
 protected static void LogWarning(SerializedProperty property, PropertyAttribute attribute, string message)
 {
     Debug.LogWarning(property.name + " property in " + property.serializedObject.targetObject.GetType() + "[" + attribute.GetType().Name + "]" + ": " + message);
 }
Example #18
0
        /// <summary> Write a Property XML Element from attributes in a member. </summary>
        public virtual void WriteProperty(System.Xml.XmlWriter writer, System.Reflection.MemberInfo member, PropertyAttribute attribute, BaseAttribute parentAttribute, System.Type mappedClass)
        {
            writer.WriteStartElement( "property" );
            // Attribute: <name>
            writer.WriteAttributeString("name", attribute.Name==null ? DefaultHelper.Get_Property_Name_DefaultValue(member) : GetAttributeValue(attribute.Name, mappedClass));
            // Attribute: <node>
            if(attribute.Node != null)
            writer.WriteAttributeString("node", GetAttributeValue(attribute.Node, mappedClass));
            // Attribute: <access>
            if(attribute.Access != null)
            writer.WriteAttributeString("access", GetAttributeValue(attribute.Access, mappedClass));
            // Attribute: <type>
            if(attribute.Type != null)
            writer.WriteAttributeString("type", GetAttributeValue(attribute.Type, mappedClass));
            else
            {
                System.Type type = null;
                if(member is System.Reflection.PropertyInfo)
                    type = (member as System.Reflection.PropertyInfo).PropertyType;
                else if(member is System.Reflection.FieldInfo)
                    type = (member as System.Reflection.FieldInfo).FieldType;
                if(type != null) // Transform using RegularExpressions
                {
                    string typeName = type.FullName + ", " + type.Assembly.GetName().Name;
                    foreach(System.Collections.DictionaryEntry pattern in Patterns)
                    {
                        if(System.Text.RegularExpressions.Regex.IsMatch(typeName, pattern.Key as string))
                        {
                            writer.WriteAttributeString( "type",
                                System.Text.RegularExpressions.Regex.Replace(typeName,
                                    pattern.Key as string,
                                    pattern.Value as string) );
                            break;
                        }
                    }
                }
            }
            // Attribute: <column>
            if(attribute.Column != null)
            writer.WriteAttributeString("column", GetAttributeValue(attribute.Column, mappedClass));
            // Attribute: <length>
            if(attribute.Length != -1)
            writer.WriteAttributeString("length", attribute.Length.ToString());
            // Attribute: <precision>
            if(attribute.Precision != -1)
            writer.WriteAttributeString("precision", attribute.Precision.ToString());
            // Attribute: <scale>
            if(attribute.Scale != -1)
            writer.WriteAttributeString("scale", attribute.Scale.ToString());
            // Attribute: <not-null>
            if( attribute.NotNullSpecified )
            writer.WriteAttributeString("not-null", attribute.NotNull ? "true" : "false");
            // Attribute: <unique>
            if( attribute.UniqueSpecified )
            writer.WriteAttributeString("unique", attribute.Unique ? "true" : "false");
            // Attribute: <unique-key>
            if(attribute.UniqueKey != null)
            writer.WriteAttributeString("unique-key", GetAttributeValue(attribute.UniqueKey, mappedClass));
            // Attribute: <index>
            if(attribute.Index != null)
            writer.WriteAttributeString("index", GetAttributeValue(attribute.Index, mappedClass));
            // Attribute: <update>
            if( attribute.UpdateSpecified )
            writer.WriteAttributeString("update", attribute.Update ? "true" : "false");
            // Attribute: <insert>
            if( attribute.InsertSpecified )
            writer.WriteAttributeString("insert", attribute.Insert ? "true" : "false");
            // Attribute: <optimistic-lock>
            if( attribute.OptimisticLockSpecified )
            writer.WriteAttributeString("optimistic-lock", attribute.OptimisticLock ? "true" : "false");
            // Attribute: <formula>
            if(attribute.Formula != null)
            writer.WriteAttributeString("formula", GetAttributeValue(attribute.Formula, mappedClass));
            // Attribute: <lazy>
            if( attribute.LazySpecified )
            writer.WriteAttributeString("lazy", attribute.Lazy ? "true" : "false");
            // Attribute: <generated>
            if(attribute.Generated != PropertyGeneration.Unspecified)
            writer.WriteAttributeString("generated", GetXmlEnumValue(typeof(PropertyGeneration), attribute.Generated));

            WriteUserDefinedContent(writer, member, null, attribute);

            System.Collections.ArrayList memberAttribs = GetSortedAttributes(member);
            int attribPos; // Find the position of the PropertyAttribute (its <sub-element>s must be after it)
            for(attribPos=0; attribPos<memberAttribs.Count; attribPos++)
                if( memberAttribs[attribPos] is PropertyAttribute
                    && ((BaseAttribute)memberAttribs[attribPos]).Position == attribute.Position )
                    break; // found
            int i = attribPos + 1;

            // Element: <meta>
            for(; i<memberAttribs.Count; i++)
            {
                BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute;
                if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType())
                    || IsNextElement(memberAttrib, attribute, typeof(MetaAttribute)) )
                    break; // next attributes are 'elements' of the same level OR for 'sub-elements'
                else
                {
                    if( memberAttrib is PropertyAttribute )
                        break; // Following attributes are for this Property
                    if( memberAttrib is MetaAttribute )
                        WriteMeta(writer, member, memberAttrib as MetaAttribute, attribute, mappedClass);
                }
            }
            WriteUserDefinedContent(writer, member, typeof(MetaAttribute), attribute);
            // Element: <column>
            for(; i<memberAttribs.Count; i++)
            {
                BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute;
                if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType())
                    || IsNextElement(memberAttrib, attribute, typeof(ColumnAttribute)) )
                    break; // next attributes are 'elements' of the same level OR for 'sub-elements'
                else
                {
                    if( memberAttrib is PropertyAttribute )
                        break; // Following attributes are for this Property
                    if( memberAttrib is ColumnAttribute )
                        WriteColumn(writer, member, memberAttrib as ColumnAttribute, attribute, mappedClass);
                }
            }
            WriteUserDefinedContent(writer, member, typeof(ColumnAttribute), attribute);
            // Element: <formula>
            for(; i<memberAttribs.Count; i++)
            {
                BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute;
                if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType())
                    || IsNextElement(memberAttrib, attribute, typeof(FormulaAttribute)) )
                    break; // next attributes are 'elements' of the same level OR for 'sub-elements'
                else
                {
                    if( memberAttrib is PropertyAttribute )
                        break; // Following attributes are for this Property
                    if( memberAttrib is FormulaAttribute )
                        WriteFormula(writer, member, memberAttrib as FormulaAttribute, attribute, mappedClass);
                }
            }
            WriteUserDefinedContent(writer, member, typeof(FormulaAttribute), attribute);
            // Element: <type>
            for(; i<memberAttribs.Count; i++)
            {
                BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute;
                if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType())
                    || IsNextElement(memberAttrib, attribute, typeof(TypeAttribute)) )
                    break; // next attributes are 'elements' of the same level OR for 'sub-elements'
                else
                {
                    if( memberAttrib is PropertyAttribute )
                        break; // Following attributes are for this Property
                    if( memberAttrib is TypeAttribute )
                        WriteType(writer, member, memberAttrib as TypeAttribute, attribute, mappedClass);
                }
            }
            WriteUserDefinedContent(writer, member, typeof(TypeAttribute), attribute);

            writer.WriteEndElement();
        }
Example #19
0
        private void GetPropertyDrawerType(SerializedProperty property)
        {
            if (_genericAttributeDrawerInstance != null)
            {
                return;
            }

            //Get the second attribute flag
            try
            {
                _genericAttribute = (PropertyAttribute)fieldInfo.GetCustomAttributes(typeof(PropertyAttribute), false)
                                    .FirstOrDefault(a => !(a is ConditionalFieldAttribute));

                //TODO: wtf man
                if (_genericAttribute is ContextMenuItemAttribute ||
                    _genericAttribute is SeparatorAttribute | _genericAttribute is AutoPropertyAttribute)
                {
                    LogWarning("[ConditionalField] does not work with " + _genericAttribute.GetType(), property);
                    return;
                }

                if (_genericAttribute is TooltipAttribute)
                {
                    return;
                }
            }
            catch (Exception e)
            {
                LogWarning("Can't find stacked propertyAttribute after ConditionalProperty: " + e, property);
                return;
            }

            //Get the associated attribute drawer
            try
            {
                _genericAttributeDrawerType = _allPropertyDrawerAttributeTypes.First(x =>
                                                                                     (Type)CustomAttributeData.GetCustomAttributes(x).First().ConstructorArguments.First().Value == _genericAttribute.GetType());
            }
            catch (Exception e)
            {
                LogWarning("Can't find property drawer from CustomPropertyAttribute of " + _genericAttribute.GetType() + " : " + e, property);
                return;
            }

            //Create instances of each (including the arguments)
            try
            {
                _genericAttributeDrawerInstance = (PropertyDrawer)Activator.CreateInstance(_genericAttributeDrawerType);
                //Get arguments
                IList <CustomAttributeTypedArgument> attributeParams = fieldInfo.GetCustomAttributesData()
                                                                       .First(a => a.AttributeType == _genericAttribute.GetType()).ConstructorArguments;
                IList <CustomAttributeTypedArgument> unpackedParams = new List <CustomAttributeTypedArgument>();
                //Unpack any params object[] args
                foreach (CustomAttributeTypedArgument singleParam in attributeParams)
                {
                    if (singleParam.Value.GetType() == typeof(ReadOnlyCollection <CustomAttributeTypedArgument>))
                    {
                        foreach (CustomAttributeTypedArgument unpackedSingleParam in (ReadOnlyCollection <CustomAttributeTypedArgument>)singleParam
                                 .Value)
                        {
                            unpackedParams.Add(unpackedSingleParam);
                        }
                    }
                    else
                    {
                        unpackedParams.Add(singleParam);
                    }
                }

                object[] attributeParamsObj = unpackedParams.Select(x => x.Value).ToArray();

                if (attributeParamsObj.Any())
                {
                    _genericAttribute = (PropertyAttribute)Activator.CreateInstance(_genericAttribute.GetType(), attributeParamsObj);
                }
                else
                {
                    _genericAttribute = (PropertyAttribute)Activator.CreateInstance(_genericAttribute.GetType());
                }
            }
            catch (Exception e)
            {
                LogWarning("No constructor available in " + _genericAttribute.GetType() + " : " + e, property);
                return;
            }

            //Reassign the attribute field in the drawer so it can access the argument values
            try
            {
                var genericDrawerAttributeField = _genericAttributeDrawerType.GetField("m_Attribute", BindingFlags.Instance | BindingFlags.NonPublic);
                genericDrawerAttributeField.SetValue(_genericAttributeDrawerInstance, _genericAttribute);
            }
            catch (Exception e)
            {
                LogWarning("Unable to assign attribute to " + _genericAttributeDrawerInstance.GetType() + " : " + e, property);
            }
        }
Example #20
0
        /// <inheritdoc/>
        public bool TryGetForDecoratorDrawer([NotNull] PropertyAttribute fieldAttribute, [NotNull] Type propertyAttributeType, [CanBeNull] IParentDrawer parent, [NotNull] LinkedMemberInfo attributeTarget, [CanBeNull] out IDecoratorDrawerDrawer result)
        {
            var drawerType = GetDrawerTypeForDecoratorDrawerAttribute(propertyAttributeType);

            if (drawerType != null)
            {
                                #if DEV_MODE && (DEBUG_GET_FOR_FIELD || DEBUG_GET_FOR_DECORATOR_DRAWER)
                Debug.Log("<color=green>DecoratorDrawer drawer</color> for field " + attributeTarget.Name + " and attribute " + fieldAttribute.GetType().Name + ": " + drawerType.Name);
                                #endif

                result = GetOrCreateInstance <IDecoratorDrawerDrawer>(drawerType);
                                #if UNITY_EDITOR
                Type decoratorDrawerType;
                if (CustomEditorUtility.TryGetDecoratorDrawerType(propertyAttributeType, out decoratorDrawerType))
                {
                    result.SetupInterface(fieldAttribute, decoratorDrawerType, parent, attributeTarget);
                    result.LateSetup();
                    return(true);
                }
                                #endif

                if (!result.RequiresDecoratorDrawerType)
                {
                    result.SetupInterface(fieldAttribute, null, parent, attributeTarget);
                    result.LateSetup();
                    return(true);
                }

                                #if DEV_MODE
                Debug.LogError(result.GetType().Name + " was returned for propertyAttribute " + propertyAttributeType.Name + " but drawer requires a decoratorDrawerType which was not found.");
                                #endif
            }

                        #if DEV_MODE && (DEBUG_GET_FOR_FIELD || DEBUG_GET_FOR_DECORATOR_DRAWER)
            Debug.Log("<color=green>DecoratorDrawer drawer</color> for field " + attributeTarget.Name + " and attribute " + fieldAttribute.GetType().Name + ": " + StringUtils.Null);
                        #endif

            result = null;
            return(false);
        }