using System.Collections.Generic; using UnityEngine; using UnityEditor; using UnityEngine.UIElements; using YKMoon; namespace YKMoonEditor { [CustomPropertyDrawer(typeof(Corner4))] public class Corner4Drawer : PropertyDrawer { protected static float propertySpacing = 2; public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { if(property.isExpanded) { return EditorGUI.GetPropertyHeight(property, label, true) + EditorGUI.GetPropertyHeight(property, label, false); } else { return EditorGUI.GetPropertyHeight(property, label, false); } } public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { //EditorGUI.BeginProperty(); position = DrawHeader(position, property, label); if (property.isExpanded) { EditorGUI.indentLevel += 1; var upperLeft = property.FindPropertyRelative("upperLeft"); var upperRight = property.FindPropertyRelative("upperRight"); var lowerLeft = property.FindPropertyRelative("lowerLeft"); var lowerRight = property.FindPropertyRelative("lowerRight"); position = DrawSingleCorner(position, upperLeft, Corner4Pos.UpperLeft); position = DrawSingleCorner(position, upperRight, Corner4Pos.UpperRight); position = DrawSingleCorner(position, lowerLeft, Corner4Pos.LowerLeft); position = DrawSingleCorner(position, lowerRight, Corner4Pos.LowerRight); if(GUI.Button(position, "ResetAll")) { //property.managedReferenceValue = Corner4.Default; upperLeft.vector2Value = Corner4.DefaultValue(Corner4Pos.UpperLeft); upperRight.vector2Value = Corner4.DefaultValue(Corner4Pos.UpperRight); lowerLeft.vector2Value = Corner4.DefaultValue(Corner4Pos.LowerLeft); lowerRight.vector2Value = Corner4.DefaultValue(Corner4Pos.LowerRight); } EditorGUI.indentLevel -= 1; } //EditorGUI.EndProperty(); } private Rect DrawHeader(Rect position, SerializedProperty property, GUIContent label) { float headerHeight = EditorGUI.GetPropertyHeight(property, label, false); position.height = headerHeight; Rect headerRect = new Rect(position); property.isExpanded = EditorGUI.Foldout(headerRect, property.isExpanded, label); position.y += headerHeight + propertySpacing; return position; } private Rect DrawSingleCorner(Rect position, SerializedProperty property, Corner4Pos pos) { float height = EditorGUI.GetPropertyHeight(property, true); position.height = height; float xPerIndent = 24f; Rect buttonRect = new Rect(position); buttonRect.width = Mathf.Min(64, position.width / 3); buttonRect.x = position.width - buttonRect.width; Rect headerRect = new Rect(position); headerRect.width = position.width / 4; Rect contentRect = new Rect(position); contentRect.width = position.width - buttonRect.width - headerRect.width-32; contentRect.x = headerRect.width + xPerIndent; EditorGUI.PrefixLabel(headerRect, new GUIContent(property.displayName)); EditorGUI.PropertyField(contentRect, property, GUIContent.none); if(GUI.Button(buttonRect, "Reset")) { property.vector2Value = Corner4.DefaultValue(pos); } position.y = position.y + propertySpacing + height; return position; } } }