A game about forced loneliness, made by TACStudios
at master 106 lines 4.1 kB view raw
1using UnityEngine; 2using UnityEngine.UI; 3using UnityEditorInternal; 4using UnityEditor.AnimatedValues; 5 6namespace UnityEditor.UI 7{ 8 [CustomEditor(typeof(LayoutElement), true)] 9 [CanEditMultipleObjects] 10 /// <summary> 11 /// Custom editor for the LayoutElement component 12 /// Extend this class to write a custom editor for a component derived from LayoutElement. 13 /// </summary> 14 public class LayoutElementEditor : Editor 15 { 16 SerializedProperty m_IgnoreLayout; 17 SerializedProperty m_MinWidth; 18 SerializedProperty m_MinHeight; 19 SerializedProperty m_PreferredWidth; 20 SerializedProperty m_PreferredHeight; 21 SerializedProperty m_FlexibleWidth; 22 SerializedProperty m_FlexibleHeight; 23 SerializedProperty m_LayoutPriority; 24 25 protected virtual void OnEnable() 26 { 27 m_IgnoreLayout = serializedObject.FindProperty("m_IgnoreLayout"); 28 m_MinWidth = serializedObject.FindProperty("m_MinWidth"); 29 m_MinHeight = serializedObject.FindProperty("m_MinHeight"); 30 m_PreferredWidth = serializedObject.FindProperty("m_PreferredWidth"); 31 m_PreferredHeight = serializedObject.FindProperty("m_PreferredHeight"); 32 m_FlexibleWidth = serializedObject.FindProperty("m_FlexibleWidth"); 33 m_FlexibleHeight = serializedObject.FindProperty("m_FlexibleHeight"); 34 m_LayoutPriority = serializedObject.FindProperty("m_LayoutPriority"); 35 } 36 37 public override void OnInspectorGUI() 38 { 39 serializedObject.Update(); 40 41 EditorGUILayout.PropertyField(m_IgnoreLayout); 42 43 if (!m_IgnoreLayout.boolValue) 44 { 45 EditorGUILayout.Space(); 46 47 LayoutElementField(m_MinWidth, 0); 48 LayoutElementField(m_MinHeight, 0); 49 LayoutElementField(m_PreferredWidth, t => t.rect.width); 50 LayoutElementField(m_PreferredHeight, t => t.rect.height); 51 LayoutElementField(m_FlexibleWidth, 1); 52 LayoutElementField(m_FlexibleHeight, 1); 53 } 54 55 EditorGUILayout.PropertyField(m_LayoutPriority); 56 57 serializedObject.ApplyModifiedProperties(); 58 } 59 60 void LayoutElementField(SerializedProperty property, float defaultValue) 61 { 62 LayoutElementField(property, _ => defaultValue); 63 } 64 65 void LayoutElementField(SerializedProperty property, System.Func<RectTransform, float> defaultValue) 66 { 67 Rect position = EditorGUILayout.GetControlRect(); 68 69 // Label 70 GUIContent label = EditorGUI.BeginProperty(position, null, property); 71 72 // Rects 73 Rect fieldPosition = EditorGUI.PrefixLabel(position, label); 74 75 Rect toggleRect = fieldPosition; 76 toggleRect.width = 16; 77 78 Rect floatFieldRect = fieldPosition; 79 floatFieldRect.xMin += 16; 80 81 // Checkbox 82 EditorGUI.BeginChangeCheck(); 83 bool enabled = EditorGUI.ToggleLeft(toggleRect, GUIContent.none, property.floatValue >= 0); 84 if (EditorGUI.EndChangeCheck()) 85 { 86 // This could be made better to set all of the targets to their initial width, but mimizing code change for now 87 property.floatValue = (enabled ? defaultValue((target as LayoutElement).transform as RectTransform) : -1); 88 } 89 90 if (!property.hasMultipleDifferentValues && property.floatValue >= 0) 91 { 92 // Float field 93 EditorGUIUtility.labelWidth = 4; // Small invisible label area for drag zone functionality 94 EditorGUI.BeginChangeCheck(); 95 float newValue = EditorGUI.FloatField(floatFieldRect, new GUIContent(" "), property.floatValue); 96 if (EditorGUI.EndChangeCheck()) 97 { 98 property.floatValue = Mathf.Max(0, newValue); 99 } 100 EditorGUIUtility.labelWidth = 0; 101 } 102 103 EditorGUI.EndProperty(); 104 } 105 } 106}