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); }
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(rect, label, property); 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 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 OnGUI(Rect rect) { InfoBoxAttribute infoBoxAttribute = (InfoBoxAttribute)attribute; float indentLength = NaughtyEditorGUI.GetIndentLength(rect); Rect infoBoxRect = new Rect( rect.x + indentLength, rect.y, rect.width - indentLength, GetHelpBoxHeight()); DrawInfoBox(infoBoxRect, infoBoxAttribute.Text, infoBoxAttribute.Type); }
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(); }
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(); }
public void DrawDefaultPropertyAndHelpBox(Rect rect, SerializedProperty property, string message, MessageType messageType) { float indentLength = NaughtyEditorGUI.GetIndentLength(rect); Rect helpBoxRect = new Rect( rect.x + indentLength, rect.y, rect.width - indentLength, GetHelpBoxHeight()); NaughtyEditorGUI.HelpBox(helpBoxRect, message, MessageType.Warning, context: property.serializedObject.targetObject); Rect propertyRect = new Rect( rect.x, rect.y + GetHelpBoxHeight(), rect.width, GetPropertyHeight(property)); EditorGUI.PropertyField(propertyRect, property, true); }
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) { Rect previewRect = new Rect() { x = rect.x + NaughtyEditorGUI.GetIndentLength(rect), y = rect.y + EditorGUIUtility.singleLineHeight, width = rect.width, height = GetAssetPreviewSize(property).y }; GUI.Label(previewRect, previewTexture); } } else { string message = property.name + " doesn't have an asset preview"; DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning); } EditorGUI.EndProperty(); }
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(rect, label, property); MinMaxSliderAttribute minMaxSliderAttribute = (MinMaxSliderAttribute)attribute; if (property.propertyType == SerializedPropertyType.Vector2 || property.propertyType == SerializedPropertyType.Vector2Int) { EditorGUI.BeginProperty(rect, label, property); float indentLength = NaughtyEditorGUI.GetIndentLength(rect); float labelWidth = EditorGUIUtility.labelWidth + NaughtyEditorGUI.HorizontalSpacing; float floatFieldWidth = EditorGUIUtility.fieldWidth; float sliderWidth = rect.width - labelWidth - 2.0f * floatFieldWidth; float sliderPadding = 5.0f; Rect labelRect = new Rect( rect.x, rect.y, labelWidth, rect.height); Rect sliderRect = new Rect( rect.x + labelWidth + floatFieldWidth + sliderPadding - indentLength, rect.y, sliderWidth - 2.0f * sliderPadding + indentLength, rect.height); Rect minFloatFieldRect = new Rect( rect.x + labelWidth - indentLength, rect.y, floatFieldWidth + indentLength, rect.height); Rect maxFloatFieldRect = new Rect( rect.x + labelWidth + floatFieldWidth + sliderWidth - indentLength, rect.y, floatFieldWidth + indentLength, rect.height); // Draw the label EditorGUI.LabelField(labelRect, label.text); // Draw the slider EditorGUI.BeginChangeCheck(); if (property.propertyType == SerializedPropertyType.Vector2) { Vector2 sliderValue = property.vector2Value; EditorGUI.MinMaxSlider(sliderRect, ref sliderValue.x, ref sliderValue.y, minMaxSliderAttribute.MinValue, minMaxSliderAttribute.MaxValue); sliderValue.x = EditorGUI.FloatField(minFloatFieldRect, sliderValue.x); sliderValue.x = Mathf.Clamp(sliderValue.x, minMaxSliderAttribute.MinValue, Mathf.Min(minMaxSliderAttribute.MaxValue, sliderValue.y)); sliderValue.y = EditorGUI.FloatField(maxFloatFieldRect, sliderValue.y); sliderValue.y = Mathf.Clamp(sliderValue.y, Mathf.Max(minMaxSliderAttribute.MinValue, sliderValue.x), minMaxSliderAttribute.MaxValue); if (EditorGUI.EndChangeCheck()) { property.vector2Value = sliderValue; } } else if (property.propertyType == SerializedPropertyType.Vector2Int) { Vector2Int sliderValue = property.vector2IntValue; float xValue = sliderValue.x; float yValue = sliderValue.y; EditorGUI.MinMaxSlider(sliderRect, ref xValue, ref yValue, minMaxSliderAttribute.MinValue, minMaxSliderAttribute.MaxValue); sliderValue.x = EditorGUI.IntField(minFloatFieldRect, (int)xValue); sliderValue.x = (int)Mathf.Clamp(sliderValue.x, minMaxSliderAttribute.MinValue, Mathf.Min(minMaxSliderAttribute.MaxValue, sliderValue.y)); sliderValue.y = EditorGUI.IntField(maxFloatFieldRect, (int)yValue); sliderValue.y = (int)Mathf.Clamp(sliderValue.y, Mathf.Max(minMaxSliderAttribute.MinValue, sliderValue.x), minMaxSliderAttribute.MaxValue); if (EditorGUI.EndChangeCheck()) { property.vector2IntValue = sliderValue; } } EditorGUI.EndProperty(); } else { string message = minMaxSliderAttribute.GetType().Name + " can be used only on Vector2 or Vector2Int fields"; DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning); } EditorGUI.EndProperty(); }
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(rect, label, property); Texture2D previewTexture = GetAssetPreview(property); HeaderImageAttribute headerImageAttribute = PropertyUtility.GetAttribute <HeaderImageAttribute>(property); if (previewTexture != null) { float indentLength = NaughtyEditorGUI.GetIndentLength(rect); var previewSize = GetAssetPreviewSize(property); float width = rect.width - indentLength; previewSize = RescaleSize(previewSize, width); float alignmentLength = 0f; if (previewSize.x < width) { switch (headerImageAttribute.Alignment) { case EAlignment.Center: alignmentLength = (width - previewSize.x) / 2f; break; case EAlignment.Right: alignmentLength = (width - previewSize.x); break; case EAlignment.Left: default: alignmentLength = 0f; break; } } Rect propertyRect = new Rect() { x = rect.x, y = rect.y + previewSize.y, width = width, height = EditorGUIUtility.singleLineHeight }; EditorGUI.PropertyField(propertyRect, property, label); Rect previewRect = new Rect() { x = rect.x + indentLength + alignmentLength, y = rect.y, // width = rect.width, width = previewSize.x, height = previewSize.y }; GUI.Label(previewRect, previewTexture); } else { string message = property.name + " has no header image for path: " + headerImageAttribute.Path; DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning); } EditorGUI.EndProperty(); }