Esempio n. 1
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;

                ShowAssetPreviewAttribute showAssetPreviewAttribute = PropertyUtility.GetAttribute <ShowAssetPreviewAttribute>(property);
                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));
            }
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            TooltipAttribute tooltip = PropertyUtility.GetAttribute <TooltipAttribute>(property);

            label.tooltip = tooltip?.tooltip;

            EditorGUI.PropertyField(position, property.FindPropertyRelative("_length"), label);
        }
        protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
        {
            QuadraticSliderAttribute quadraticAttribute = PropertyUtility.GetAttribute <QuadraticSliderAttribute>(property);

            if (quadraticAttribute != null)
            {
                NonLinearSliderDrawer.Draw(rect, property, quadraticAttribute.Min, quadraticAttribute.Max, GetQuadraticFunction(quadraticAttribute.Power), label);
            }
        }
        protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
        {
            ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute <ProgressBarAttribute>(property);
            var maxValue = GetMaxValue(property, progressBarAttribute);

            return(IsNumber(property) && maxValue is float
                   ?GetPropertyHeight(property)
                       : GetPropertyHeight(property) + GetHelpBoxHeight());
        }
        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));
        }
Esempio n. 7
0
        protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
        {
            if (property.hasChildren)
            {
                InlineAttribute attribute = PropertyUtility.GetAttribute <InlineAttribute>(property);

                if (attribute.ShowHeaderAndBox)
                {
                    rect.position += new Vector2(0, EditorGUIUtility.singleLineHeight * 0.2f);

                    EditorGUI.LabelField(rect, label, EditorStyles.boldLabel);
                    NaughtyEditorGUI.BeginBoxGroup_Layout();
                }

                //Move to first child
                SerializedProperty child = property.Copy();
                child.Next(true);

                SerializedProperty endProperty = property.GetEndProperty(true);

                bool enterChildren;
                if (attribute.ShowHeaderAndBox)
                {
                    enterChildren = EditorGUILayout.PropertyField(child);
                }
                else
                {
                    enterChildren = EditorGUI.PropertyField(rect, child);
                }

                while (child.Next(enterChildren) && !SerializedProperty.EqualContents(child, endProperty))
                {
                    enterChildren = EditorGUILayout.PropertyField(child);
                }

                if (attribute.ShowHeaderAndBox)
                {
                    NaughtyEditorGUI.EndBoxGroup_Layout();
                }
            }
            else
            {
                EditorGUILayout.PropertyField(property);
                NaughtyEditorGUI.HelpBox_Layout("Can't use [Inline] on a property with no children.", MessageType.Warning);
            }
        }
        /// <summary>
        /// Tries to find the member property. Not guaranteed to find one, as the given name (or auto name) could be wrong.
        /// </summary>
        static PropertyInfo FindProperty(SerializedProperty serializedProperty)
        {
            UsePropertySetterAttribute attribute = PropertyUtility.GetAttribute <UsePropertySetterAttribute>(serializedProperty);

            string propertyName;

            if (attribute.autoFindProperty)
            {
                propertyName = serializedProperty.displayName.Replace(" ", string.Empty);
            }
            else
            {
                propertyName = attribute.propertyName;
            }
            Object targetObject = serializedProperty.serializedObject.targetObject;

            return(ReflectionUtility.GetProperty(targetObject, propertyName));
        }
Esempio n. 9
0
        public override void OnInspectorGUI()
        {
            GetSerializedProperties(ref _serializedProperties);

            bool anyNaughtyAttribute = _serializedProperties.Any(p => PropertyUtility.GetAttribute <INaughtyAttribute>(p) != null);

            if (!anyNaughtyAttribute)
            {
                DrawDefaultInspector();
            }
            else
            {
                DrawSerializedProperties();
            }

            DrawNonSerializedFields();
            DrawNativeProperties();
            DrawButtons();
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            MinMaxRange.SliderAttribute attribute = PropertyUtility.GetAttribute <MinMaxRange.SliderAttribute>(property);

            if (attribute != null)
            {
                Draw(position, property, attribute.min, attribute.max, label);
            }
            else
            {
                EditorGUI.PropertyField(position, property, label);
                if (property.isExpanded)
                {
                    EditorGUI.indentLevel++;
                    NaughtyEditorGUI.HelpBox_Layout("MinMaxRange needs a [MinMaxRange.Slider(min, max)] attribute for the fancy editor.", MessageType.Info);
                    EditorGUI.indentLevel--;
                }
            }
        }
        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;
            }

            var attribute = PropertyUtility.GetAttribute <CurveRangeAttribute>(property);

            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));

            EditorGUI.EndProperty();
        }
        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 maxValue       = GetMaxValue(property, progressBarAttribute);

            if (maxValue != null && maxValue is float)
            {
                var fillPercentage = value / (float)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",
                    nameof(progressBarAttribute.MaxValueName));

                DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
            }
        }
        protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
        {
            AnimatorParamAttribute animatorParamAttribute = PropertyUtility.GetAttribute <AnimatorParamAttribute>(property);

            AnimatorController animatorController = GetAnimatorController(property, animatorParamAttribute.AnimatorName);

            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;
            }
        }
        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;
                    }

                    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);
            }
        }
        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";
                Debug.LogWarning(warning, property.serializedObject.targetObject);
            }
        }
Esempio n. 16
0
 private static IEnumerable <IGrouping <string, SerializedProperty> > GetFoldoutProperties(IEnumerable <SerializedProperty> properties)
 {
     return(properties
            .Where(p => PropertyUtility.GetAttribute <FoldoutAttribute>(p) != null)
            .GroupBy(p => PropertyUtility.GetAttribute <FoldoutAttribute>(p).Name));
 }
Esempio n. 17
0
    /// <summary>
    /// Takes a SerializedProperty and finds a local component that can be slotted into it.
    /// Local in this context means its a component attached to the same GameObject.
    /// This could easily be changed to use GetComponentInParent/GetComponentInChildren
    /// </summary>
    /// <param name="property"></param>
    /// <returns></returns>
    private Component FindAutohookTarget(SerializedProperty property)
    {
        var root = property.serializedObject;

        if (root.targetObject is Component)
        {
            // first, lets find the type of component were trying to autohook...
            var type = GetTypeFromProperty(property);

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

            // ...then use GetComponent(type) to see if there is one on our object.
            var component = (Component)root.targetObject;

            Component ret = component.GetComponent(type);

            if (ret != null)
            {
                return(ret);
            }

            AutohookAttribute autohookAttribute = PropertyUtility.GetAttribute <AutohookAttribute>(property);
            switch (autohookAttribute.Mode)
            {
            case AutohookAttribute.AutohookMode.AlsoChildren:
                return(component.GetComponentInChildren(type));

            case AutohookAttribute.AutohookMode.AlsoParent:
                return(component.GetComponentInParent(type));

            case AutohookAttribute.AutohookMode.AllParents: {
                Transform t = component.transform.parent;
                while (t != null)
                {
                    ret = t.GetComponent(type);
                    if (ret != null)
                    {
                        return(ret);
                    }
                    t = t.parent;
                }
                return(null);
            }

            case AutohookAttribute.AutohookMode.SiblingsAndChildren: {
                return(component.transform.parent?.GetComponentInChildren(type));
            }

            case AutohookAttribute.AutohookMode.UpwardsAllChildren: {
                Transform t = component.transform.parent;
                while (t != null)
                {
                    ret = t.GetComponentInChildren(type);
                    if (ret != null)
                    {
                        return(ret);
                    }
                    t = t.parent;
                }
                return(null);
            }
            }


            return(ret);
        }
        else
        {
            Debug.Log("OH NO handle fails here better pls");
        }

        return(null);
    }
 public sealed override PropertyCondition OnGuiValidate(SerializedProperty property)
 {
     return(OnGuiValidate(property, PropertyUtility.GetAttribute <T>(property)));
 }
Esempio n. 19
0
 private static IEnumerable <SerializedProperty> GetNonGroupedProperties(IEnumerable <SerializedProperty> properties)
 {
     return(properties.Where(p => PropertyUtility.GetAttribute <IGroupAttribute>(p) == null));
 }
Esempio n. 20
0
 bool ShouldUseEnumKeys(SerializedProperty property)
 {
     return(keyType.IsEnum && PropertyUtility.GetAttribute <EnumKeysAttribute>(property) != null);
 }
Esempio n. 21
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))
            {
                ParameterInfo[] callbackParameters = validationCallback.GetParameters();

                if (callbackParameters.Length == 0)
                {
                    if (!(bool)validationCallback.Invoke(target, null))
                    {
                        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 if (callbackParameters.Length == 1)
                {
                    FieldInfo fieldInfo     = ReflectionUtility.GetField(target, property.name);
                    Type      fieldType     = fieldInfo.FieldType;
                    Type      parameterType = callbackParameters[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 an optional single parameter of the same type as the field";

                    NaughtyEditorGUI.HelpBox_Layout(warning, MessageType.Warning, context: property.serializedObject.targetObject);
                }
            }
        }
 public override sealed void OnGui(SerializedProperty property, GUIContent label)
 {
     OnGui(property, label, PropertyUtility.GetAttribute <T>(property));
 }