private object GetValues(SerializedProperty property, string valuesName)
        {
            object target = PropertyUtility.GetTargetObjectWithProperty(property);

            FieldInfo valuesFieldInfo = ReflectionUtility.GetField(target, valuesName);

            if (valuesFieldInfo != null)
            {
                return(valuesFieldInfo.GetValue(target));
            }

            PropertyInfo valuesPropertyInfo = ReflectionUtility.GetProperty(target, valuesName);

            if (valuesPropertyInfo != null)
            {
                return(valuesPropertyInfo.GetValue(target));
            }

            MethodInfo methodValuesInfo = ReflectionUtility.GetMethod(target, valuesName);

            if (methodValuesInfo != null &&
                methodValuesInfo.ReturnType != typeof(void) &&
                methodValuesInfo.GetParameters().Length == 0)
            {
                return(methodValuesInfo.Invoke(target, null));
            }

            return(null);
        }
Example #2
0
        private object GetMaxValue(SerializedProperty property, ProgressBarAttribute progressBarAttribute)
        {
            if (string.IsNullOrEmpty(progressBarAttribute.MaxValueName))
            {
                return(progressBarAttribute.MaxValue);
            }
            else
            {
                object target = PropertyUtility.GetTargetObjectWithProperty(property);

                FieldInfo valuesFieldInfo = ReflectionUtility.GetField(target, progressBarAttribute.MaxValueName);
                if (valuesFieldInfo != null)
                {
                    return(valuesFieldInfo.GetValue(target));
                }

                PropertyInfo valuesPropertyInfo = ReflectionUtility.GetProperty(target, progressBarAttribute.MaxValueName);
                if (valuesPropertyInfo != null)
                {
                    return(valuesPropertyInfo.GetValue(target));
                }

                MethodInfo methodValuesInfo = ReflectionUtility.GetMethod(target, progressBarAttribute.MaxValueName);
                if (methodValuesInfo != null &&
                    (methodValuesInfo.ReturnType == typeof(float) || methodValuesInfo.ReturnType == typeof(int)) &&
                    methodValuesInfo.GetParameters().Length == 0)
                {
                    return(methodValuesInfo.Invoke(target, null));
                }

                return(null);
            }
        }
        protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
        {
            DropdownAttribute dropdownAttribute = (DropdownAttribute)attribute;
            object            values            = GetValues(property, dropdownAttribute.ValuesName);
            FieldInfo         fieldInfo         = ReflectionUtility.GetField(PropertyUtility.GetTargetObjectWithProperty(property), property.name);

            float propertyHeight = AreValuesValid(values, fieldInfo)
                                ? GetPropertyHeight(property)
                                : GetPropertyHeight(property) + GetHelpBoxHeight();

            return(propertyHeight);
        }
Example #4
0
        protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
        {
            DropdownConstantsAttribute dropdownAttribute = (DropdownConstantsAttribute)attribute;
            Type      selectFromType = dropdownAttribute.SelectFromType;
            FieldInfo fieldInfo      = ReflectionUtility.GetField(PropertyUtility.GetTargetObjectWithProperty(property), property.name);

            float propertyHeight = AreValuesValid(selectFromType, fieldInfo)
                                ? GetPropertyHeight(property)
                                : GetPropertyHeight(property) + GetHelpBoxHeight();

            return(propertyHeight);
        }
        protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(rect, label, property);

            DropdownDefinedAttribute dropdownAttribute = (DropdownDefinedAttribute)attribute;
            object target = PropertyUtility.GetTargetObjectWithProperty(property);

            object[]  valuesObject  = dropdownAttribute.ValuesArray;
            FieldInfo dropdownField = ReflectionUtility.GetField(target, property.name);

            if (AreValuesValid(valuesObject, dropdownField))
            {
                if (valuesObject is IList && dropdownField.FieldType == GetElementType(valuesObject))
                {
                    // Selected value
                    object selectedValue = dropdownField.GetValue(target);

                    // Values and display options
                    IList    valuesList     = (IList)valuesObject;
                    object[] values         = new object[valuesList.Count];
                    string[] displayOptions = new string[valuesList.Count];

                    for (int i = 0; i < values.Length; i++)
                    {
                        object value = valuesList[i];
                        values[i]         = value;
                        displayOptions[i] = value == null ? "<null>" : value.ToString();
                    }

                    // Selected value index
                    int selectedValueIndex = Array.IndexOf(values, selectedValue);
                    if (selectedValueIndex < 0)
                    {
                        selectedValueIndex = 0;
                    }

                    NaughtyEditorGUI.Dropdown(
                        rect, property.serializedObject, target, dropdownField, label.text, selectedValueIndex, values,
                        displayOptions);
                }
            }
            else
            {
                string message =
                    $"Invalid values provided to '{dropdownAttribute.GetType().Name}'. The types of the target field and the values provided to the attribute don't match";

                DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
            }

            EditorGUI.EndProperty();
        }
Example #6
0
        public override void ValidateProperty(SerializedProperty property)
        {
            ValidateInputAttribute validateInputAttribute = PropertyUtility.GetAttribute <ValidateInputAttribute>(property);
            object target = PropertyUtility.GetTargetObjectWithProperty(property);

            MethodInfo validationCallback = ReflectionUtility.GetMethod(target, validateInputAttribute.CallbackName);

            if (validationCallback != null &&
                validationCallback.ReturnType == typeof(bool) &&
                validationCallback.GetParameters().Length == 1)
            {
                FieldInfo fieldInfo     = ReflectionUtility.GetField(target, property.name);
                Type      fieldType     = fieldInfo.FieldType;
                Type      parameterType = validationCallback.GetParameters()[0].ParameterType;

                if (fieldType == parameterType)
                {
                    if (!(bool)validationCallback.Invoke(target, new object[] { fieldInfo.GetValue(target) }))
                    {
                        if (string.IsNullOrEmpty(validateInputAttribute.Message))
                        {
                            NaughtyEditorGUI.HelpBox_Layout(
                                property.name + " is not valid", MessageType.Error, context: property.serializedObject.targetObject);
                        }
                        else
                        {
                            NaughtyEditorGUI.HelpBox_Layout(
                                validateInputAttribute.Message, MessageType.Error, context: property.serializedObject.targetObject);
                        }
                    }
                }
                else
                {
                    string warning = "The field type is not the same as the callback's parameter type";
                    NaughtyEditorGUI.HelpBox_Layout(warning, MessageType.Warning, context: property.serializedObject.targetObject);
                }
            }
            else
            {
                string warning =
                    validateInputAttribute.GetType().Name +
                    " needs a callback with boolean return type and a single parameter of the same type as the field";

                NaughtyEditorGUI.HelpBox_Layout(warning, MessageType.Warning, context: property.serializedObject.targetObject);
            }
        }
Example #7
0
        private static AnimatorController GetAnimatorController(SerializedProperty property, string animatorName)
        {
            object target = PropertyUtility.GetTargetObjectWithProperty(property);

            FieldInfo animatorFieldInfo = ReflectionUtility.GetField(target, animatorName);

            if (animatorFieldInfo != null &&
                animatorFieldInfo.FieldType == typeof(Animator))
            {
                Animator animator = animatorFieldInfo.GetValue(target) as Animator;
                if (animator != null)
                {
                    AnimatorController animatorController = animator.runtimeAnimatorController as AnimatorController;
                    return(animatorController);
                }
            }

            PropertyInfo animatorPropertyInfo = ReflectionUtility.GetProperty(target, animatorName);

            if (animatorPropertyInfo != null &&
                animatorPropertyInfo.PropertyType == typeof(Animator))
            {
                Animator animator = animatorPropertyInfo.GetValue(target) as Animator;
                if (animator != null)
                {
                    AnimatorController animatorController = animator.runtimeAnimatorController as AnimatorController;
                    return(animatorController);
                }
            }

            MethodInfo animatorGetterMethodInfo = ReflectionUtility.GetMethod(target, animatorName);

            if (animatorGetterMethodInfo != null &&
                animatorGetterMethodInfo.ReturnType == typeof(Animator) &&
                animatorGetterMethodInfo.GetParameters().Length == 0)
            {
                Animator animator = animatorGetterMethodInfo.Invoke(target, null) as Animator;
                if (animator != null)
                {
                    AnimatorController animatorController = animator.runtimeAnimatorController as AnimatorController;
                    return(animatorController);
                }
            }

            return(null);
        }
        protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(rect, label, property);

            DropdownAttribute dropdownAttribute = (DropdownAttribute)attribute;
            object            target            = PropertyUtility.GetTargetObjectWithProperty(property);

            object    valuesObject  = GetValues(property, dropdownAttribute.ValuesName);
            FieldInfo dropdownField = ReflectionUtility.GetField(target, property.name);

            if (AreValuesValid(valuesObject, dropdownField))
            {
                if (valuesObject is IList && dropdownField.FieldType == GetElementType(valuesObject))
                {
                    // Selected value
                    object selectedValue = dropdownField.GetValue(target);

                    // Values and display options
                    IList    valuesList     = (IList)valuesObject;
                    object[] values         = new object[valuesList.Count];
                    string[] displayOptions = new string[valuesList.Count];

                    for (int i = 0; i < values.Length; i++)
                    {
                        object value = valuesList[i];
                        values[i]         = value;
                        displayOptions[i] = value == null ? "<null>" : value.ToString();
                    }

                    // Selected value index
                    int selectedValueIndex = Array.IndexOf(values, selectedValue);
                    if (selectedValueIndex < 0)
                    {
                        selectedValueIndex = 0;
                    }

                    NaughtyEditorGUI.Dropdown(
                        rect, property.serializedObject, target, dropdownField, label.text, selectedValueIndex, values, displayOptions);
                }
                else if (valuesObject is IDropdownList)
                {
                    // Current value
                    object selectedValue = dropdownField.GetValue(target);

                    // Current value index, values and display options
                    int           index = -1;
                    int           selectedValueIndex = -1;
                    List <object> values             = new List <object>();
                    List <string> displayOptions     = new List <string>();
                    IDropdownList dropdown           = (IDropdownList)valuesObject;

                    using (IEnumerator <KeyValuePair <string, object> > dropdownEnumerator = dropdown.GetEnumerator())
                    {
                        while (dropdownEnumerator.MoveNext())
                        {
                            index++;

                            KeyValuePair <string, object> current = dropdownEnumerator.Current;
                            if (current.Value?.Equals(selectedValue) == true)
                            {
                                selectedValueIndex = index;
                            }

                            values.Add(current.Value);

                            if (current.Key == null)
                            {
                                displayOptions.Add("<null>");
                            }
                            else if (string.IsNullOrWhiteSpace(current.Key))
                            {
                                displayOptions.Add("<empty>");
                            }
                            else
                            {
                                displayOptions.Add(current.Key);
                            }
                        }
                    }

                    if (selectedValueIndex < 0)
                    {
                        selectedValueIndex = 0;
                    }

                    NaughtyEditorGUI.Dropdown(
                        rect, property.serializedObject, target, dropdownField, label.text, selectedValueIndex, values.ToArray(), displayOptions.ToArray());
                }
            }
            else
            {
                string message = string.Format("Invalid values with name '{0}' provided to '{1}'. Either the values name is incorrect or the types of the target field and the values field/property/method don't match",
                                               dropdownAttribute.ValuesName, dropdownAttribute.GetType().Name);

                DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
            }

            EditorGUI.EndProperty();
        }
Example #9
0
        protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(rect, label, property);

            DropdownConstantsAttribute dropdownAttribute = (DropdownConstantsAttribute)attribute;
            object target = PropertyUtility.GetTargetObjectWithProperty(property);

            Type      selectFromType = dropdownAttribute.SelectFromType;
            FieldInfo dropdownField  = ReflectionUtility.GetField(target, property.name);

            if (AreValuesValid(selectFromType, dropdownField))
            {
                var searchFlags               = BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy;
                var allPublicStaticFields     = dropdownAttribute.SelectFromType.GetFields(searchFlags);
                var allPublicStaticProperties = dropdownAttribute.SelectFromType.GetProperties(searchFlags);

                // IsLiteral determines if its value is written at compile time and not changeable
                // IsInitOnly determines if the field can be set in the body of the constructor
                // for C# a field which is readonly keyword would have both true but a const field would have only IsLiteral equal to true
                foreach (FieldInfo field in allPublicStaticFields)
                {
                    if ((field.IsInitOnly || field.IsLiteral) && field.FieldType == selectFromType)
                    {
                        _constants.Add(field);
                    }
                }
                foreach (var prop in allPublicStaticProperties)
                {
                    if (prop.PropertyType == selectFromType)
                    {
                        _constants.Add(prop);
                    }
                }


                if (IsNullOrEmpty(_constants))
                {
                    return;
                }

                string[] names  = new string[_constants.Count];
                object[] values = new object[_constants.Count];
                for (var i = 0; i < _constants.Count; i++)
                {
                    names[i]  = _constants[i].Name;
                    values[i] = GetValue(i);
                }

                // Selected value
                object selectedValue = dropdownField.GetValue(target);

                int  selectedValueIndex = -1;
                bool valueFound         = false;

                if (selectedValue != null)
                {
                    for (var i = 0; i < values.Length; i++)
                    {
                        if (selectedValue.Equals(values[i]))
                        {
                            valueFound         = true;
                            selectedValueIndex = i;
                        }
                    }
                }

                if (!valueFound)
                {
                    names  = InsertAt(names, 0);
                    values = InsertAt(values, 0);
                    var actualValue = selectedValue;
                    var value       = actualValue != null ? actualValue : "NULL";
                    names[0]  = "NOT FOUND: " + value;
                    values[0] = actualValue;
                }

                NaughtyEditorGUI.Dropdown(
                    rect, property.serializedObject, target, dropdownField, label.text, selectedValueIndex, values, names);
            }
            else
            {
                string message =
                    $"Invalid values provided to '{dropdownAttribute.GetType().Name}'. The types of the target field and the type provided to the attribute don't match";

                DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
            }

            EditorGUI.EndProperty();
        }