public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);

            if (property.propertyType != SerializedPropertyType.Float && property.propertyType != SerializedPropertyType.Integer)
            {
                EditorGUILayout.HelpBox("Field " + property.name + " is not a number", MessageType.Warning);
                return;
            }

            var value          = property.propertyType == SerializedPropertyType.Integer ? property.intValue : property.floatValue;
            var valueFormatted = property.propertyType == SerializedPropertyType.Integer ? value.ToString() : String.Format("{0:0.00}", value);

            ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute <ProgressBarAttribute>(property);
            var   position    = EditorGUILayout.GetControlRect();
            var   maxValue    = progressBarAttribute.MaxValue;
            float lineHight   = EditorGUIUtility.singleLineHeight;
            float padding     = EditorGUIUtility.standardVerticalSpacing;
            var   barPosition = new Rect(position.position.x, position.position.y, position.size.x, lineHight);

            var fillPercentage = value / maxValue;
            var barLabel       = (!string.IsNullOrEmpty(progressBarAttribute.Name) ? "[" + progressBarAttribute.Name + "] " : "") + valueFormatted + "/" + maxValue;

            var color  = GetColor(progressBarAttribute.Color);
            var color2 = Color.white;

            DrawBar(barPosition, Mathf.Clamp01(fillPercentage), barLabel, color, color2);
        }
        public override bool CanDrawProperty(SerializedProperty property)
        {
            ShowIfAttribute showIfAttribute = PropertyUtility.GetAttribute <ShowIfAttribute>(property);

            UnityEngine.Object target = PropertyUtility.GetTargetObject(property);

            FieldInfo conditionField = ReflectionUtility.GetField(target, showIfAttribute.ConditionName);

            if (conditionField != null &&
                conditionField.FieldType == typeof(bool))
            {
                return((bool)conditionField.GetValue(target));
            }

            MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, showIfAttribute.ConditionName);

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

            string warning = showIfAttribute.GetType().Name + " needs a valid boolean condition field or method name to work";

            EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);

            return(true);
        }
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);

            if (property.propertyType == SerializedPropertyType.String)
            {
                EditorGUILayout.LabelField(property.displayName);

                EditorGUI.BeginChangeCheck();

                string textAreaValue = EditorGUILayout.TextArea(property.stringValue, GUILayout.MinHeight(EditorGUIUtility.singleLineHeight * 3f));

                if (EditorGUI.EndChangeCheck())
                {
                    property.stringValue = textAreaValue;
                }
            }
            else
            {
                string warning = PropertyUtility.GetAttribute <ResizableTextAreaAttribute>(property).GetType().Name + " can only be used on string fields";
                EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: PropertyUtility.GetTargetObject(property));

                EditorDrawUtility.DrawPropertyField(property);
            }
        }
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawPropertyField(property);

            if (property.propertyType == SerializedPropertyType.ObjectReference)
            {
                if (property.objectReferenceValue != null)
                {
                    Texture2D previewTexture = AssetPreview.GetAssetPreview(property.objectReferenceValue);
                    if (previewTexture != null)
                    {
                        ShowAssetPreviewAttribute showAssetPreviewAttribute = PropertyUtility.GetAttribute <ShowAssetPreviewAttribute>(property);
                        int width  = Mathf.Clamp(showAssetPreviewAttribute.Width, 0, previewTexture.width);
                        int height = Mathf.Clamp(showAssetPreviewAttribute.Height, 0, previewTexture.height);

                        GUILayout.Label(previewTexture, GUILayout.MaxWidth(width), GUILayout.MaxHeight(height));
                    }
                    else
                    {
                        this.DrawWarningBox(property.name + " doesn't have an asset preview", property);
                    }
                }
            }
            else
            {
                this.DrawWarningBox(property.name + " doesn't have an asset preview", property);
            }
        }
        protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
        {
            if (!IsNumber(property))
            {
                string message = string.Format("Field {0} is not a number", property.name);
                DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
                return;
            }

            ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute <ProgressBarAttribute>(property);
            var value          = property.propertyType == SerializedPropertyType.Integer ? property.intValue : property.floatValue;
            var valueFormatted = property.propertyType == SerializedPropertyType.Integer ? value.ToString() : string.Format("{0:0.00}", value);
            var fillPercentage = value / progressBarAttribute.MaxValue;
            var barLabel       = (!string.IsNullOrEmpty(progressBarAttribute.Name) ? "[" + progressBarAttribute.Name + "] " : "") + valueFormatted + "/" + progressBarAttribute.MaxValue;
            var barColor       = progressBarAttribute.Color.GetColor();
            var labelColor     = Color.white;

            var  indentLength = NaughtyEditorGUI.GetIndentLength(rect);
            Rect barRect      = new Rect()
            {
                x      = rect.x + indentLength,
                y      = rect.y,
                width  = rect.width - indentLength,
                height = EditorGUIUtility.singleLineHeight
            };

            DrawBar(barRect, Mathf.Clamp01(fillPercentage), barLabel, barColor, labelColor);
        }
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawPropertyField(property);

            if (property.propertyType == SerializedPropertyType.ObjectReference)
            {
                if (property.objectReferenceValue != null)
                {
                    Texture2D previewTexture = AssetPreview.GetAssetPreview(property.objectReferenceValue);
                    if (previewTexture != null)
                    {
                        ShowAssetPreviewAttribute showAssetPreviewAttribute = PropertyUtility.GetAttribute <ShowAssetPreviewAttribute>(property);
                        int width  = Mathf.Clamp(showAssetPreviewAttribute.Width, 0, previewTexture.width);
                        int height = Mathf.Clamp(showAssetPreviewAttribute.Height, 0, previewTexture.height);

                        GUILayout.Label(previewTexture, GUILayout.MaxWidth(width), GUILayout.MaxHeight(height));
                    }
                    else
                    {
                        string warning = property.name + " doesn't have an asset preview";
                        EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: PropertyUtility.GetTargetObject(property));
                    }
                }
            }
            else
            {
                string warning = property.name + " doesn't have an asset preview";
                EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: PropertyUtility.GetTargetObject(property));
            }
        }
Exemplo n.º 7
0
        protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(rect, label, property);

            // Check user error
            if (property.propertyType != SerializedPropertyType.AnimationCurve)
            {
                string message = string.Format("Field {0} is not an AnimationCurve", property.name);
                DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
                return;
            }

            if (_cachedCurveRangeAttribute == null)
            {
                _cachedCurveRangeAttribute = PropertyUtility.GetAttribute <CurveRangeAttribute>(property);
            }

            var attribute = _cachedCurveRangeAttribute;

            EditorGUI.CurveField(
                rect,
                property,
                attribute.Color == EColor.Clear ? Color.green : attribute.Color.GetColor(),
                new Rect(attribute.Min.x, attribute.Min.y, attribute.Max.x - attribute.Min.x, attribute.Max.y - attribute.Min.y),
                label);

            EditorGUI.EndProperty();
        }
Exemplo n.º 8
0
        public override void DrawProperty(SerializedProperty property)
        {
            EditorDrawUtility.DrawHeader(property);
            var attr = PropertyUtility.GetAttribute <PowerSlideAttribute>(property);

            if (attr.BaseRcpLn == 0 | attr.BaseRcpLn == float.NaN)
            {
                EditorDrawUtility.DrawHelpBox($"Invalid Base: {attr.Base}", MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
                EditorDrawUtility.DrawPropertyField(property);
                return;
            }

            if (property.propertyType == SerializedPropertyType.Integer)
            {
                float value = math.log(property.intValue) * attr.BaseRcpLn;
                EditorGUI.BeginChangeCheck();
                EditorGUILayout.PrefixLabel($"{property.displayName}[{property.intValue}]");
                value             = EditorGUILayout.Slider(value, attr.MinPower, attr.MaxPower);
                property.intValue = (int)math.round(math.pow(attr.Base, value));
            }
            else if (property.propertyType == SerializedPropertyType.Float)
            {
                float value = math.log(property.floatValue) * attr.BaseRcpLn;
                EditorGUI.BeginChangeCheck();
                EditorGUILayout.PrefixLabel($"{property.displayName}[{property.floatValue}]");
                value = EditorGUILayout.Slider(value, attr.MinPower, attr.MaxPower);
                property.floatValue = math.pow(attr.Base, value);
            }
            else
            {
                string warning = attr.GetType().Name + " can be used only on int or float fields";
                EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: PropertyUtility.GetTargetObject(property));
                EditorDrawUtility.DrawPropertyField(property);
            }
        }
Exemplo n.º 9
0
        public override void ValidateProperty(SerializedProperty property)
        {
            MinValueAttribute minValueAttribute = PropertyUtility.GetAttribute <MinValueAttribute>(property);

            if (property.propertyType == SerializedPropertyType.Integer)
            {
                if (property.intValue < minValueAttribute.MinValue)
                {
                    property.intValue = (int)minValueAttribute.MinValue;
                }
            }
            else if (property.propertyType == SerializedPropertyType.Float)
            {
                if (property.floatValue < minValueAttribute.MinValue)
                {
                    property.floatValue = minValueAttribute.MinValue;
                }
            }
            else
            {
                string warning = minValueAttribute.GetType().Name + " can be used only on int or float fields";
                EditorGUILayout.HelpBox(warning, MessageType.Warning);
                Debug.LogWarning(warning, PropertyUtility.GetTargetObject(property));
            }
        }
Exemplo n.º 10
0
        public override void ValidateProperty(SerializedProperty property, bool drawField)
        {
            RequiredAttribute requiredAttribute = PropertyUtility.GetAttribute <RequiredAttribute>(property);

            if (requiredAttribute.HideWithField && !drawField)
            {
                return;
            }

            if (property.propertyType == SerializedPropertyType.ObjectReference)
            {
                if (property.objectReferenceValue == null)
                {
                    string errorMessage = property.name + " is required";
                    if (!string.IsNullOrEmpty(requiredAttribute.Message))
                    {
                        errorMessage = requiredAttribute.Message;
                    }

                    EditorDrawUtility.DrawHelpBox(errorMessage, MessageType.Error, logToConsole: true, context: PropertyUtility.GetTargetObject(property));
                }
            }
            else
            {
                string warning = requiredAttribute.GetType().Name + " works only on reference types";
                EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: PropertyUtility.GetTargetObject(property));
            }
        }
Exemplo n.º 11
0
        public override void ValidateProperty(SerializedProperty property)
        {
            if (_cachedRequiredAttribute == null)
            {
                _cachedRequiredAttribute = PropertyUtility.GetAttribute <RequiredAttribute>(property);
            }

            RequiredAttribute requiredAttribute = _cachedRequiredAttribute;

            if (property.propertyType == SerializedPropertyType.ObjectReference)
            {
                if (property.objectReferenceValue == null)
                {
                    string errorMessage = property.name + " is required";
                    if (!string.IsNullOrEmpty(requiredAttribute.Message))
                    {
                        errorMessage = requiredAttribute.Message;
                    }

                    NaughtyEditorGUI.HelpBox_Layout(errorMessage, MessageType.Error, context: property.serializedObject.targetObject);
                }
            }
            else
            {
                string warning = requiredAttribute.GetType().Name + " works only on reference types";
                NaughtyEditorGUI.HelpBox_Layout(warning, MessageType.Warning, context: property.serializedObject.targetObject);
            }
        }
Exemplo n.º 12
0
        public virtual void Prepare()
        {
            _nonSerializedFields = ReflectionUtility.GetAllFields(
                target, f => f.GetCustomAttributes(typeof(ShowNonSerializedFieldAttribute), true).Length > 0).ToList();

            _nativeProperties = ReflectionUtility.GetAllProperties(
                target, p => p.GetCustomAttributes(typeof(ShowNativePropertyAttribute), true).Length > 0).ToList();

            _methods = ReflectionUtility.GetAllMethods(
                target, m => m.GetCustomAttributes(typeof(ButtonAttribute), true).Length > 0).ToList();

            GetSerializedProperties(ref _serializedProperties);

            _anyNaughtyAttribute = _serializedProperties.Any(p => PropertyUtility.GetAttribute <INaughtyAttribute>(p.serializedProperty) != null);

            _nonGroupedSerializedProperty = GetNonGroupedProperties(_serializedProperties).ToList();

            //.First(...) doesnt work for some reason because the m_Script field isnt loaded yet I assume
            NaughtyProperty[] mScripts = _serializedProperties.Where(p => p.serializedProperty.name.Equals("m_Script")).ToArray();
            m_ScriptProperty = mScripts.Length > 0 ? mScripts[0].serializedProperty : null;

            _groupedSerialzedProperty = GetGroupedProperties(_serializedProperties).ToList();

            _foldoutGroupedSerializedProperty = GetFoldoutProperties(_serializedProperties).ToList();

            _useCachedMetaAttributes = false;
        }
Exemplo n.º 13
0
        protected virtual void OnEnable()
        {
            SerializedData entry;

            if (!data.ContainsKey(target))
            {
                entry = new SerializedData();
                data.Add(target, entry);

                entry._nonSerializedFields = ReflectionUtility.GetAllFields(
                    target, f => f.GetCustomAttributes(typeof(ShowNonSerializedFieldAttribute), true).Length > 0).ToList();
                entry._nativeProperties = ReflectionUtility.GetAllProperties(
                    target, p => p.GetCustomAttributes(typeof(ShowNativePropertyAttribute), true).Length > 0).ToList();
                entry._methods = ReflectionUtility.GetAllMethods(
                    target, m => m.GetCustomAttributes(typeof(ButtonAttribute), true).Length > 0).ToList();
                GetSerializedProperties(ref _serializedProperties);
                entry._serializedProperties = _serializedProperties;
                entry._anyNaughtyAttribute  = _serializedProperties.Any(p => PropertyUtility.GetAttribute <INaughtyAttribute>(p) != null);

                entry._anyNativeProperties = entry._nativeProperties.Any();
                entry._anyMethods          = entry._methods.Any();
            }


            entry = data[target];
            _serializedProperties = entry._serializedProperties;
            _nonSerializedFields  = entry._nonSerializedFields;
            _nativeProperties     = entry._nativeProperties;
            _anyNativeProperties  = entry._anyNativeProperties;
            _methods             = entry._methods;
            _anyMethods          = entry._anyMethods;
            _anyNaughtyAttribute = entry._anyNaughtyAttribute;
        }
Exemplo n.º 14
0
        public override void DrawProperty(SerializedProperty property)
        {
            var labelAttribute = PropertyUtility.GetAttribute <LabelAttribute>(property);
            var guiContent     = new GUIContent(labelAttribute.Label);

            EditorGUILayout.PropertyField(property, guiContent, true);
        }
        public override bool CanDrawProperty(SerializedProperty property)
        {
            HideIfAttribute hideIfAttribute = PropertyUtility.GetAttribute <HideIfAttribute>(property);

            UnityEngine.Object target = PropertyUtility.GetTargetObject(property);

            FieldInfo conditionField = ReflectionUtility.GetField(target, hideIfAttribute.ConditionName);

            if (conditionField != null &&
                conditionField.FieldType == typeof(bool))
            {
                return(!(bool)conditionField.GetValue(target));
            }

            MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, hideIfAttribute.ConditionName);

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

            string warning = hideIfAttribute.GetType().Name + " needs a valid boolean condition field or method name to work";

            EditorGUILayout.HelpBox(warning, MessageType.Warning);
            Debug.LogWarning(warning, target);

            return(true);
        }
Exemplo n.º 16
0
        public override void ValidateProperty(SerializedProperty property)
        {
            RequiredAttribute requiredAttribute = PropertyUtility.GetAttribute <RequiredAttribute>(property);

            if (property.propertyType == SerializedPropertyType.ObjectReference)
            {
                if (property.objectReferenceValue == null)
                {
                    string errorMessage = property.name + " is required";
                    if (!string.IsNullOrEmpty(requiredAttribute.Message))
                    {
                        errorMessage = requiredAttribute.Message;
                    }

                    EditorGUILayout.HelpBox(errorMessage, MessageType.Error);
                    Debug.LogError(errorMessage, PropertyUtility.GetTargetObject(property));
                }
            }
            else
            {
                string warning = requiredAttribute.GetType().Name + " works only on reference types";
                EditorGUILayout.HelpBox(warning, MessageType.Warning);
                Debug.LogWarning(warning, PropertyUtility.GetTargetObject(property));
            }
        }
Exemplo n.º 17
0
        private Vector2 GetAssetPreviewSize(SerializedProperty property)
        {
            Texture2D previewTexture = GetAssetPreview(property);

            if (previewTexture == null)
            {
                return(Vector2.zero);
            }
            else
            {
                int targetWidth  = ShowAssetPreviewAttribute.DefaultWidth;
                int targetHeight = ShowAssetPreviewAttribute.DefaultHeight;

                if (_cachedShowAssetPreviewAttribute == null)
                {
                    _cachedShowAssetPreviewAttribute =
                        PropertyUtility.GetAttribute <ShowAssetPreviewAttribute>(property);
                }

                ShowAssetPreviewAttribute showAssetPreviewAttribute = _cachedShowAssetPreviewAttribute;
                if (showAssetPreviewAttribute != null)
                {
                    targetWidth  = showAssetPreviewAttribute.Width;
                    targetHeight = showAssetPreviewAttribute.Height;
                }

                int width  = Mathf.Clamp(targetWidth, 0, previewTexture.width);
                int height = Mathf.Clamp(targetHeight, 0, previewTexture.height);

                return(new Vector2(width, height));
            }
        }
Exemplo n.º 18
0
        private Texture2D GetAssetPreview(SerializedProperty property)
        {
            HeaderImageAttribute headerImageAttribute = PropertyUtility.GetAttribute <HeaderImageAttribute>(property);
            Texture2D            previewTexture       = AssetDatabase.LoadAssetAtPath <Texture2D>(headerImageAttribute.Path);

            return(previewTexture);
        }
Exemplo n.º 19
0
        protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
        {
            ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute <ProgressBarAttribute>(property);
            var maxValue = GetMaxValue(property, progressBarAttribute);

            return(IsNumber(property) && IsNumber(maxValue)
                ? GetPropertyHeight(property)
                : GetPropertyHeight(property) + GetHelpBoxHeight());
        }
Exemplo n.º 20
0
        protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
        {
            AnimatorParamAttribute animatorParamAttribute = PropertyUtility.GetAttribute <AnimatorParamAttribute>(property);
            bool validAnimatorController = GetAnimatorController(property, animatorParamAttribute.AnimatorName) != null;
            bool validPropertyType       = property.propertyType == SerializedPropertyType.Integer || property.propertyType == SerializedPropertyType.String;

            return((validAnimatorController && validPropertyType)
                ? GetPropertyHeight(property)
                : GetPropertyHeight(property) + GetHelpBoxHeight());
        }
        protected float GetPropertyHeight(SerializedProperty property)
        {
            SpecialCaseDrawerAttribute specialCaseAttribute = PropertyUtility.GetAttribute <SpecialCaseDrawerAttribute>(property);

            if (specialCaseAttribute != null)
            {
                return(specialCaseAttribute.GetDrawer().GetPropertyHeight(property));
            }

            return(EditorGUI.GetPropertyHeight(property, includeChildren: true));
        }
Exemplo n.º 22
0
        private static void PropertyField_Implementation(Rect rect, SerializedProperty property, bool includeChildren, PropertyFieldFunction propertyFieldFunction)
        {
            SpecialCaseDrawerAttribute specialCaseAttribute = PropertyUtility.GetAttribute <SpecialCaseDrawerAttribute>(property);

            if (specialCaseAttribute != null)
            {
                specialCaseAttribute.GetDrawer().OnGUI(rect, property);
            }
            else
            {
                GUIContent label = new GUIContent(PropertyUtility.GetLabel(property));
                bool       anyDrawerAttribute = PropertyUtility.GetAttributes <DrawerAttribute>(property).Any();

                if (!anyDrawerAttribute)
                {
                    // Drawer attributes check for visibility, enableability and validator themselves,
                    // so if a property doesn't have a DrawerAttribute we need to check for these explicitly

                    // Check if visible
                    bool visible = PropertyUtility.IsVisible(property);
                    if (!visible)
                    {
                        return;
                    }

                    // Validate
                    ValidatorAttribute[] validatorAttributes = PropertyUtility.GetAttributes <ValidatorAttribute>(property);
                    foreach (var validatorAttribute in validatorAttributes)
                    {
                        validatorAttribute.GetValidator().ValidateProperty(property);
                    }

                    // Check if enabled and draw
                    EditorGUI.BeginChangeCheck();
                    bool enabled = PropertyUtility.IsEnabled(property);

                    using (new EditorGUI.DisabledScope(disabled: !enabled))
                    {
                        propertyFieldFunction.Invoke(rect, property, label, includeChildren);
                    }

                    // Call OnValueChanged callbacks
                    if (EditorGUI.EndChangeCheck())
                    {
                        PropertyUtility.CallOnValueChangedCallbacks(property);
                    }
                }
                else
                {
                    // We don't need to check for enableIfAttribute
                    propertyFieldFunction.Invoke(rect, property, label, includeChildren);
                }
            }
        }
Exemplo n.º 23
0
        public static bool DrawHeader(SerializedProperty property)
        {
            HeaderAttribute headerAttr = PropertyUtility.GetAttribute <HeaderAttribute>(property);

            if (headerAttr != null)
            {
                DrawHeader(headerAttr.header);
                return(true);
            }

            return(false);
        }
Exemplo n.º 24
0
 private static IEnumerable <IGrouping <string, SerializedProperty> > GetGroupedProperties(IEnumerable <SerializedProperty> properties)
 {
     return(properties
            .Where(p =>
     {
         return PropertyUtility.GetAttribute <BoxGroupAttribute>(p) != null;
     })
            .GroupBy(p =>
     {
         return PropertyUtility.GetAttribute <BoxGroupAttribute>(p).Name;
     }));
 }
Exemplo n.º 25
0
        public static void PropertyField_Layout(SerializedProperty property, bool includeChildren)
        {
            SpecialCaseDrawerAttribute specialCaseAttribute = PropertyUtility.GetAttribute <SpecialCaseDrawerAttribute>(property);

            if (specialCaseAttribute != null)
            {
                specialCaseAttribute.GetDrawer().OnGUI(property);
            }
            else
            {
                GUIContent label = new GUIContent(PropertyUtility.GetLabel(property));
                bool       anyDrawerAttribute = PropertyUtility.GetAttributes <DrawerAttribute>(property).Any();

                if (!anyDrawerAttribute)
                {
                    // Drawer attributes check for visibility, enableability and validator themselves,
                    // so if a property doesn't have a DrawerAttribute we need to check for these explicitly

                    // Check if visible
                    bool visible = PropertyUtility.IsVisible(property);
                    if (!visible)
                    {
                        return;
                    }

                    // Validate
                    ValidatorAttribute[] validatorAttributes = PropertyUtility.GetAttributes <ValidatorAttribute>(property);
                    foreach (var validatorAttribute in validatorAttributes)
                    {
                        validatorAttribute.GetValidator().ValidateProperty(property);
                    }

                    // Check if enabled and draw
                    EditorGUI.BeginChangeCheck();
                    bool enabled = PropertyUtility.IsEnabled(property);
                    GUI.enabled = enabled;
                    EditorGUILayout.PropertyField(property, label, includeChildren);
                    GUI.enabled = true;

                    // Call OnValueChanged callbacks
                    if (EditorGUI.EndChangeCheck())
                    {
                        PropertyUtility.CallOnValueChangedCallbacks(property);
                    }
                }
                else
                {
                    // We don't need to check for enableIfAttribute
                    EditorGUILayout.PropertyField(property, label, includeChildren);
                }
            }
        }
        protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(rect, label, property);

            if (property.propertyType == SerializedPropertyType.ObjectReference)
            {
                Rect propertyRect = new Rect()
                {
                    x      = rect.x,
                    y      = rect.y,
                    width  = rect.width,
                    height = EditorGUIUtility.singleLineHeight
                };

                EditorGUI.PropertyField(propertyRect, property, label);

                Texture2D previewTexture = GetAssetPreview(property);
                if (previewTexture != null)
                {
                    var previewAttribute = PropertyUtility.GetAttribute <ShowAssetPreviewAttribute>(property);
                    var previewSize      = GetAssetPreviewSize(property, previewAttribute);
                    var previewPosition  = rect.x + NaughtyEditorGUI.GetIndentLength(rect);
                    switch (previewAttribute.Alignment)
                    {
                    case TextAlignment.Center:
                        previewPosition += (rect.width - previewSize.x) / 2;
                        break;

                    case TextAlignment.Right:
                        previewPosition = rect.max.x - previewSize.x;
                        break;
                    }
                    Rect previewRect = new Rect
                    {
                        x      = previewPosition,
                        y      = rect.y + EditorGUIUtility.singleLineHeight,
                        width  = rect.width,
                        height = previewSize.y
                    };

                    GUI.Label(previewRect, previewTexture);
                }
            }
            else
            {
                string message = property.name + " doesn't have an asset preview";
                DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
            }

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

            if (!IsNumber(property))
            {
                string message = string.Format("Field {0} is not a number", property.name);
                DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
                return;
            }

            if (_cachedProgressBarAttribute == null)
            {
                _cachedProgressBarAttribute = PropertyUtility.GetAttribute <ProgressBarAttribute>(property);
            }

            ProgressBarAttribute progressBarAttribute = _cachedProgressBarAttribute;
            var value          = property.propertyType == SerializedPropertyType.Integer ? property.intValue : property.floatValue;
            var valueFormatted = property.propertyType == SerializedPropertyType.Integer ? value.ToString() : string.Format("{0:0.00}", value);
            var maxValue       = GetMaxValue(property, progressBarAttribute);

            if (maxValue != null && IsNumber(maxValue))
            {
                var fillPercentage = value / CastToFloat(maxValue);
                var barLabel       = (!string.IsNullOrEmpty(progressBarAttribute.Name) ? "[" + progressBarAttribute.Name + "] " : "") + valueFormatted + "/" + maxValue;
                var barColor       = progressBarAttribute.Color.GetColor();
                var labelColor     = Color.white;

                var  indentLength = NaughtyEditorGUI.GetIndentLength(rect);
                Rect barRect      = new Rect()
                {
                    x      = rect.x + indentLength,
                    y      = rect.y,
                    width  = rect.width - indentLength,
                    height = EditorGUIUtility.singleLineHeight
                };

                DrawBar(barRect, Mathf.Clamp01(fillPercentage), barLabel, barColor, labelColor);
            }
            else
            {
                string message = string.Format(
                    "The provided dynamic max value for the progress bar is not correct. Please check if the '{0}' is correct, or the return type is float/int",
                    nameof(progressBarAttribute.MaxValueName));

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

            EditorGUI.EndProperty();
        }
Exemplo n.º 28
0
        public override void ValidateProperty(SerializedProperty property, bool drawField)
        {
            ValidateInputAttribute validateInputAttribute = PropertyUtility.GetAttribute <ValidateInputAttribute>(property);

            if (validateInputAttribute.HideWithField && !drawField)
            {
                return;
            }

            UnityEngine.Object target = PropertyUtility.GetTargetObject(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))
                        {
                            EditorDrawUtility.DrawHelpBox(property.name + " is not valid", MessageType.Error, logToConsole: true, context: target);
                        }
                        else
                        {
                            EditorDrawUtility.DrawHelpBox(validateInputAttribute.Message, MessageType.Error, logToConsole: true, context: target);
                        }
                    }
                }
                else
                {
                    string warning = "The field type is not the same as the callback's parameter type";
                    EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);
                }
            }
            else
            {
                string warning =
                    validateInputAttribute.GetType().Name +
                    " needs a callback with boolean return type and a single parameter of the same type as the field";

                EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target);
            }
        }
        public override void ValidateProperty(SerializedProperty property)
        {
            if (_cachedMaxValueAttribute == null)
            {
                _cachedMaxValueAttribute = PropertyUtility.GetAttribute <MaxValueAttribute>(property);
            }

            MaxValueAttribute maxValueAttribute = _cachedMaxValueAttribute;

            if (property.propertyType == SerializedPropertyType.Integer)
            {
                if (property.intValue > maxValueAttribute.MaxValue)
                {
                    property.intValue = (int)maxValueAttribute.MaxValue;
                }
            }
            else if (property.propertyType == SerializedPropertyType.Float)
            {
                if (property.floatValue > maxValueAttribute.MaxValue)
                {
                    property.floatValue = maxValueAttribute.MaxValue;
                }
            }
            else if (property.propertyType == SerializedPropertyType.Vector2)
            {
                property.vector2Value = Vector2.Min(property.vector2Value, new Vector2(maxValueAttribute.MaxValue, maxValueAttribute.MaxValue));
            }
            else if (property.propertyType == SerializedPropertyType.Vector3)
            {
                property.vector3Value = Vector3.Min(property.vector3Value, new Vector3(maxValueAttribute.MaxValue, maxValueAttribute.MaxValue, maxValueAttribute.MaxValue));
            }
            else if (property.propertyType == SerializedPropertyType.Vector4)
            {
                property.vector4Value = Vector4.Min(property.vector4Value, new Vector4(maxValueAttribute.MaxValue, maxValueAttribute.MaxValue, maxValueAttribute.MaxValue, maxValueAttribute.MaxValue));
            }
            else if (property.propertyType == SerializedPropertyType.Vector2Int)
            {
                property.vector2IntValue = Vector2Int.Min(property.vector2IntValue, new Vector2Int((int)maxValueAttribute.MaxValue, (int)maxValueAttribute.MaxValue));
            }
            else if (property.propertyType == SerializedPropertyType.Vector3Int)
            {
                property.vector3IntValue = Vector3Int.Min(property.vector3IntValue, new Vector3Int((int)maxValueAttribute.MaxValue, (int)maxValueAttribute.MaxValue, (int)maxValueAttribute.MaxValue));
            }
            else
            {
                string warning = maxValueAttribute.GetType().Name + " can be used only on int, float, Vector or VectorInt fields";
                Debug.LogWarning(warning, property.serializedObject.targetObject);
            }
        }
        protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(rect, label, property);

            if (_cachedAnimatorParamAttribute == null)
            {
                _cachedAnimatorParamAttribute = PropertyUtility.GetAttribute <AnimatorParamAttribute>(property);
            }

            AnimatorParamAttribute animatorParamAttribute = _cachedAnimatorParamAttribute;

            AnimatorController animatorController = _cachedAnimatorController;

            if (animatorController == null)
            {
                DrawDefaultPropertyAndHelpBox(rect, property, InvalidAnimatorControllerWarningMessage, MessageType.Warning);
                return;
            }

            int parametersCount = animatorController.parameters.Length;
            List <AnimatorControllerParameter> animatorParameters = new List <AnimatorControllerParameter>(parametersCount);

            for (int i = 0; i < parametersCount; i++)
            {
                AnimatorControllerParameter parameter = animatorController.parameters[i];
                if (animatorParamAttribute.AnimatorParamType == null || parameter.type == animatorParamAttribute.AnimatorParamType)
                {
                    animatorParameters.Add(parameter);
                }
            }

            switch (property.propertyType)
            {
            case SerializedPropertyType.Integer:
                DrawPropertyForInt(rect, property, label, animatorParameters);
                break;

            case SerializedPropertyType.String:
                DrawPropertyForString(rect, property, label, animatorParameters);
                break;

            default:
                DrawDefaultPropertyAndHelpBox(rect, property, string.Format(InvalidTypeWarningMessage, property.name), MessageType.Warning);
                break;
            }

            EditorGUI.EndProperty();
        }