protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
        {
            if (property.objectReferenceValue == null)
            {
                return(GetPropertyHeight(property));
            }

            System.Type propertyType = PropertyUtility.GetPropertyType(property);
            if (typeof(ScriptableObject).IsAssignableFrom(propertyType))
            {
                ScriptableObject scriptableObject = property.objectReferenceValue as ScriptableObject;
                if (scriptableObject == null)
                {
                    return(GetPropertyHeight(property));
                }

                if (property.isExpanded)
                {
                    using (SerializedObject serializedObject = new SerializedObject(scriptableObject))
                    {
                        float totalHeight = EditorGUIUtility.singleLineHeight;

                        using (var iterator = serializedObject.GetIterator())
                        {
                            if (iterator.NextVisible(true))
                            {
                                do
                                {
                                    SerializedProperty childProperty = serializedObject.FindProperty(iterator.name);
                                    if (childProperty.name.Equals("m_Script", System.StringComparison.Ordinal))
                                    {
                                        continue;
                                    }

                                    bool visible = PropertyUtility.IsVisible(childProperty);
                                    if (!visible)
                                    {
                                        continue;
                                    }

                                    float height = GetPropertyHeight(childProperty);
                                    totalHeight += height;
                                }while (iterator.NextVisible(false));
                            }
                        }

                        totalHeight += EditorGUIUtility.standardVerticalSpacing;
                        return(totalHeight);
                    }
                }
                else
                {
                    return(GetPropertyHeight(property));
                }
            }
            else
            {
                return(GetPropertyHeight(property) + GetHelpBoxHeight());
            }
        }
        protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(rect, label, property);

            if (property.objectReferenceValue == null)
            {
                EditorGUI.PropertyField(rect, property, label, false);
            }
            else
            {
                System.Type propertyType = PropertyUtility.GetPropertyType(property);
                if (typeof(ScriptableObject).IsAssignableFrom(propertyType))
                {
                    ScriptableObject scriptableObject = property.objectReferenceValue as ScriptableObject;
                    if (scriptableObject == null)
                    {
                        EditorGUI.PropertyField(rect, property, label, false);
                    }
                    else
                    {
                        // Draw a foldout
                        Rect foldoutRect = new Rect()
                        {
                            x      = rect.x,
                            y      = rect.y,
                            width  = EditorGUIUtility.labelWidth,
                            height = EditorGUIUtility.singleLineHeight
                        };

                        property.isExpanded = EditorGUI.Foldout(foldoutRect, property.isExpanded, label, toggleOnLabelClick: true);

                        // Draw the scriptable object field
                        Rect propertyRect = new Rect()
                        {
                            x      = rect.x,
                            y      = rect.y,
                            width  = rect.width,
                            height = EditorGUIUtility.singleLineHeight
                        };

                        EditorGUI.PropertyField(propertyRect, property, label, false);

                        property.serializedObject.ApplyModifiedProperties();

                        // Draw the child properties
                        if (property.isExpanded)
                        {
                            DrawChildProperties(rect, property);
                        }
                    }
                }
                else
                {
                    string message = $"{typeof(ExpandableAttribute).Name} can only be used on scriptable objects";
                    DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
                }
            }

            EditorGUI.EndProperty();
        }
Exemplo n.º 3
0
        protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(rect, label, property);

            System.Type propertyType = PropertyUtility.GetPropertyType(property);
            if (propertyType == typeof(ScriptableObject))
            {
                ScriptableObject scriptableObject = property.objectReferenceValue as ScriptableObject;
                if (scriptableObject == null)
                {
                    EditorGUI.PropertyField(rect, property, label, false);
                }
                else
                {
                    // Draw a foldout
                    Rect foldoutRect = new Rect()
                    {
                        x      = rect.x,
                        y      = rect.y,
                        width  = EditorGUIUtility.labelWidth,
                        height = EditorGUIUtility.singleLineHeight
                    };

                    property.isExpanded = EditorGUI.Foldout(foldoutRect, property.isExpanded, label, toggleOnLabelClick: true);

                    // Draw the scriptable object field
                    float indentLength = NaughtyEditorGUI.GetIndentLength(rect);
                    float labelWidth   = EditorGUIUtility.labelWidth - indentLength + NaughtyEditorGUI.HorizontalSpacing;
                    Rect  propertyRect = new Rect()
                    {
                        x      = rect.x + labelWidth,
                        y      = rect.y,
                        width  = rect.width - labelWidth,
                        height = EditorGUIUtility.singleLineHeight
                    };

                    EditorGUI.BeginChangeCheck();
                    property.objectReferenceValue = EditorGUI.ObjectField(propertyRect, GUIContent.none, property.objectReferenceValue, propertyType, false);
                    if (EditorGUI.EndChangeCheck())
                    {
                        property.serializedObject.ApplyModifiedProperties();
                    }

                    // Draw the child properties
                    if (property.isExpanded)
                    {
                        DrawChildProperties(rect, property);
                    }
                }
            }
            else
            {
                string message = $"{typeof(ExpandableAttribute).Name} can only be used on scriptable objects";
                DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
            }

            EditorGUI.EndProperty();
        }
        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)
            {
                Type fieldType     = PropertyUtility.GetPropertyType(property);
                Type parameterType = validationCallback.GetParameters()[0].ParameterType;

                if (fieldType == parameterType)
                {
                    if (!(bool)validationCallback.Invoke(target, new[] { PropertyUtility.GetPropertyValue(property) }))
                    {
                        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);
            }
        }
        private Object GetAssignableObject(Object obj, ReorderableList list)
        {
            System.Type listType    = PropertyUtility.GetPropertyType(list.serializedProperty);
            System.Type elementType = ReflectionUtility.GetListElementType(listType);

            if (elementType == null)
            {
                return(null);
            }

            System.Type objType = obj.GetType();

            if (elementType.IsAssignableFrom(objType))
            {
                return(obj);
            }

            if (objType == typeof(GameObject))
            {
                if (typeof(Transform).IsAssignableFrom(elementType))
                {
                    Transform transform = ((GameObject)obj).transform;
                    if (elementType == typeof(RectTransform))
                    {
                        RectTransform rectTransform = transform as RectTransform;
                        return(rectTransform);
                    }
                    else
                    {
                        return(transform);
                    }
                }
                else if (typeof(MonoBehaviour).IsAssignableFrom(elementType))
                {
                    return(((GameObject)obj).GetComponent(elementType));
                }
            }

            return(null);
        }