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 void DrawSerializedProperties() { serializedObject.Update(); // Draw non-grouped serialized properties foreach (var property in GetNonGroupedProperties(_serializedProperties)) { if (property.name.Equals("m_Script", System.StringComparison.Ordinal)) { GUI.enabled = false; EditorGUILayout.PropertyField(property); GUI.enabled = true; } else { NaughtyEditorGUI.PropertyField_Layout(property, true); } } // Draw grouped serialized properties foreach (var group in GetGroupedProperties(_serializedProperties)) { IEnumerable <SerializedProperty> visibleProperties = group.Where(p => PropertyUtility.IsVisible(p)); if (!visibleProperties.Any()) { continue; } EColor newColor = EColor.Gray; foreach (var property in visibleProperties) { newColor = PropertyUtility.GetAttribute <BoxGroupAttribute>(property).myColor; if (newColor != EColor.Gray) { break; } } bool show = BeginBoxGroup_Layout(group.Key, newColor); if (show) { foreach (var property in visibleProperties) { NaughtyEditorGUI.PropertyField_Layout(property, true); } } NaughtyEditorGUI.EndBoxGroup_Layout(); } serializedObject.ApplyModifiedProperties(); }
private Vector2 GetAssetPreviewSize(SerializedProperty property) { Texture2D previewTexture = GetAssetPreview(property); if (previewTexture == null) { return(Vector2.zero); } else { ShowAssetPreviewAttribute showAssetPreviewAttribute = PropertyUtility.GetAttribute <ShowAssetPreviewAttribute>(property); int width = Mathf.Clamp(showAssetPreviewAttribute.Width, 0, previewTexture.width); int height = Mathf.Clamp(showAssetPreviewAttribute.Height, 0, previewTexture.height); return(new Vector2(width, height)); } }
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(); }
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); } }
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) { MaxValueAttribute maxValueAttribute = PropertyUtility.GetAttribute <MaxValueAttribute>(property); 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 { string warning = maxValueAttribute.GetType().Name + " can be used only on int or float fields"; Debug.LogWarning(warning, property.serializedObject.targetObject); } }
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(); var style = new GUIStyle(); style.normal.textColor = Color.white; style.fontStyle = FontStyle.Bold; style.fontSize = 13; style.contentOffset = new Vector2(10, 0); 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.BeginHorizontal(); //EditorGUILayout.LabelField(property.name, style , GUILayout.MinWidth(152)); EditorGUILayout.PropertyField(property, label, includeChildren); //EditorGUILayout.EndHorizontal(); GUI.enabled = true; // Call OnValueChanged callbacks if (EditorGUI.EndChangeCheck()) { PropertyUtility.CallOnValueChangedCallbacks(property); } } else { // We don't need to check for enableIfAttribute //EditorGUILayout.BeginHorizontal(); //EditorGUILayout.LabelField(property.name, style , GUILayout.MinWidth(152)); EditorGUILayout.PropertyField(property, label, includeChildren); //EditorGUILayout.EndHorizontal(); } } }
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); } } }
private static IEnumerable <IGrouping <string, SerializedProperty> > GetGroupedProperties(IEnumerable <SerializedProperty> properties) { return(properties .Where(p => PropertyUtility.GetAttribute <BoxGroupAttribute>(p) != null) .GroupBy(p => PropertyUtility.GetAttribute <BoxGroupAttribute>(p).Name)); }
private static IEnumerable <SerializedProperty> GetNonGroupedProperties(IEnumerable <SerializedProperty> properties) { return(properties.Where(p => PropertyUtility.GetAttribute <BoxGroupAttribute>(p) == null)); }