コード例 #1
0
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        ConditionalFieldAttribute condHAtt = (ConditionalFieldAttribute)attribute;
        bool enabled = GetConditionalFieldAttributeResult(condHAtt, property);

        if (!condHAtt.HideInInspector || enabled)
        {
            return(EditorGUI.GetPropertyHeight(property, label));
        }
        else
        {
            //The property is not being drawn
            //We want to undo the spacing added before and after the property
            return(-EditorGUIUtility.standardVerticalSpacing);
            //return 0.0f;
        }


        /*
         * //Get the base height when not expanded
         * var height = base.GetPropertyHeight(property, label);
         *
         * // if the property is expanded go through all its children and get their height
         * if (property.isExpanded)
         * {
         *  var propEnum = property.GetEnumerator();
         *  while (propEnum.MoveNext())
         *      height += EditorGUI.GetPropertyHeight((SerializedProperty)propEnum.Current, GUIContent.none, true);
         * }
         * return height;*/
    }
コード例 #2
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        ConditionalFieldAttribute condHAtt = (ConditionalFieldAttribute)attribute;
        bool enabled = GetConditionalFieldAttributeResult(condHAtt, property);

        bool wasEnabled = GUI.enabled;

        GUI.enabled = enabled;
        if (!condHAtt.HideInInspector || enabled)
        {
            EditorGUI.PropertyField(position, property, label, true);
        }

        GUI.enabled = wasEnabled;
    }
コード例 #3
0
        public static bool BehaviourPropertyIsVisible(MonoBehaviour behaviour, string propertyName,
                                                      ConditionalFieldAttribute appliedAttribute)
        {
            if (string.IsNullOrEmpty(appliedAttribute.FieldToCheck))
            {
                return(true);
            }

            var so             = new SerializedObject(behaviour);
            var property       = so.FindProperty(propertyName);
            var targetProperty = ConditionalFieldUtility.FindRelativeProperty(property, appliedAttribute.FieldToCheck);

            return(ConditionalFieldUtility.PropertyIsVisible(targetProperty, appliedAttribute.Inverse,
                                                             appliedAttribute.CompareValues));
        }
コード例 #4
0
        private void Initialize(SerializedProperty property)
        {
            if (_attribute == null)
            {
                _attribute = attribute as ConditionalFieldAttribute;
            }
            if (_attribute == null)
            {
                return;
            }

            if (!_conditionalToTarget.ContainsKey(property))
            {
                _conditionalToTarget.Add(property,
                                         ConditionalFieldUtility.FindRelativeProperty(property, _attribute.FieldToCheck));
            }


            if (_customDrawersCached)
            {
                return;
            }
            if (_typesCache == null)
            {
                _typesCache = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes())
                              .Where(x => typeof(PropertyDrawer).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract);
            }

            if (fieldInfo.GetCustomAttributes(typeof(PropertyAttribute), false).Count() > 1)
            {
                _multipleAttributes = true;
                GetPropertyDrawerType(property);
            }
            else if (!fieldInfo.FieldType.Module.ScopeName.Equals(typeof(int).Module.ScopeName))
            {
                _specialType = true;
                GetTypeDrawerType(property);
            }

            _customDrawersCached = true;
        }
コード例 #5
0
    private bool GetConditionalFieldAttributeResult(ConditionalFieldAttribute condHAtt, SerializedProperty property)
    {
        bool enabled = (condHAtt.UseOrLogic) ?false :true;

        //Handle primary property
        SerializedProperty sourcePropertyValue = null;

        //Get the full relative property path of the sourcefield so we can have nested hiding.Use old method when dealing with arrays
        if (!property.isArray)
        {
            string propertyPath  = property.propertyPath;                                                //returns the property path of the property we want to apply the attribute to
            string conditionPath = propertyPath.Replace(property.name, condHAtt.ConditionalSourceField); //changes the path to the conditionalsource property path
            sourcePropertyValue = property.serializedObject.FindProperty(conditionPath);

            //if the find failed->fall back to the old system
            if (sourcePropertyValue == null)
            {
                //original implementation (doens't work with nested serializedObjects)
                sourcePropertyValue = property.serializedObject.FindProperty(condHAtt.ConditionalSourceField);
            }
        }
        else
        {
            //original implementation (doens't work with nested serializedObjects)
            sourcePropertyValue = property.serializedObject.FindProperty(condHAtt.ConditionalSourceField);
        }


        if (sourcePropertyValue != null)
        {
            enabled = CheckPropertyType(sourcePropertyValue);
            if (condHAtt.InverseCondition1)
            {
                enabled = !enabled;
            }
        }
        else
        {
            //Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField);
        }


        //handle secondary property
        SerializedProperty sourcePropertyValue2 = null;

        if (!property.isArray)
        {
            string propertyPath  = property.propertyPath;                                                 //returns the property path of the property we want to apply the attribute to
            string conditionPath = propertyPath.Replace(property.name, condHAtt.ConditionalSourceField2); //changes the path to the conditionalsource property path
            sourcePropertyValue2 = property.serializedObject.FindProperty(conditionPath);

            //if the find failed->fall back to the old system
            if (sourcePropertyValue2 == null)
            {
                //original implementation (doens't work with nested serializedObjects)
                sourcePropertyValue2 = property.serializedObject.FindProperty(condHAtt.ConditionalSourceField2);
            }
        }
        else
        {
            // original implementation(doens't work with nested serializedObjects)
            sourcePropertyValue2 = property.serializedObject.FindProperty(condHAtt.ConditionalSourceField2);
        }

        //Combine the results
        if (sourcePropertyValue2 != null)
        {
            bool prop2Enabled = CheckPropertyType(sourcePropertyValue2);
            if (condHAtt.InverseCondition2)
            {
                prop2Enabled = !prop2Enabled;
            }

            if (condHAtt.UseOrLogic)
            {
                enabled = enabled || prop2Enabled;
            }
            else
            {
                enabled = enabled && prop2Enabled;
            }
        }
        else
        {
            //Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField);
        }

        //Handle the unlimited property array
        string[] conditionalSourceFieldArray        = condHAtt.ConditionalSourceFields;
        bool[]   conditionalSourceFieldInverseArray = condHAtt.ConditionalSourceFieldInverseBools;
        for (int index = 0; index < conditionalSourceFieldArray.Length; ++index)
        {
            SerializedProperty sourcePropertyValueFromArray = null;
            if (!property.isArray)
            {
                string propertyPath  = property.propertyPath;                                                   //returns the property path of the property we want to apply the attribute to
                string conditionPath = propertyPath.Replace(property.name, conditionalSourceFieldArray[index]); //changes the path to the conditionalsource property path
                sourcePropertyValueFromArray = property.serializedObject.FindProperty(conditionPath);

                //if the find failed->fall back to the old system
                if (sourcePropertyValueFromArray == null)
                {
                    //original implementation (doens't work with nested serializedObjects)
                    sourcePropertyValueFromArray = property.serializedObject.FindProperty(conditionalSourceFieldArray[index]);
                }
            }
            else
            {
                // original implementation(doens't work with nested serializedObjects)
                sourcePropertyValueFromArray = property.serializedObject.FindProperty(conditionalSourceFieldArray[index]);
            }

            //Combine the results
            if (sourcePropertyValueFromArray != null)
            {
                bool propertyEnabled = CheckPropertyType(sourcePropertyValueFromArray);
                if (conditionalSourceFieldInverseArray.Length >= (index + 1) && conditionalSourceFieldInverseArray[index])
                {
                    propertyEnabled = !propertyEnabled;
                }

                if (condHAtt.UseOrLogic)
                {
                    enabled = enabled || propertyEnabled;
                }
                else
                {
                    enabled = enabled && propertyEnabled;
                }
            }
            else
            {
                //Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField);
            }
        }


        //wrap it all up
        if (condHAtt.Inverse)
        {
            enabled = !enabled;
        }

        return(enabled);
    }