BetterHorizontalOrVerticalLayoutGroupEditor.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. namespace TheraBytes.BetterUi.Editor
  9. {
  10. [CanEditMultipleObjects]
  11. public abstract class BetterHorizontalOrVerticalLayoutGroupEditor<TSource, TBetter> : UnityEditor.Editor
  12. where TSource : HorizontalOrVerticalLayoutGroup
  13. where TBetter : TSource, IBetterHorizontalOrVerticalLayoutGroup
  14. {
  15. private SerializedProperty paddingFallback;
  16. private SerializedProperty paddingConfigs;
  17. private SerializedProperty spacingFallback;
  18. private SerializedProperty spacingConfigs;
  19. protected SerializedProperty settingsFallback;
  20. protected SerializedProperty settingsConfigs;
  21. bool hasReverseOption;
  22. bool hasChildScale;
  23. protected static TBetter MakeBetterLogic(MenuCommand command)
  24. {
  25. TSource lg = command.context as TSource;
  26. var pad = new Margin(lg.padding);
  27. var space = lg.spacing;
  28. var newLg = Betterizer.MakeBetter<TSource, TBetter>(lg, "m_Padding");
  29. var betterLg = newLg as TBetter;
  30. if (betterLg != null)
  31. {
  32. betterLg.PaddingSizer.SetSize(newLg, pad);
  33. betterLg.SpacingSizer.SetSize(newLg, space);
  34. }
  35. else if(newLg != null)
  36. {
  37. pad.CopyValuesTo(newLg.padding);
  38. }
  39. Betterizer.Validate(newLg);
  40. return newLg as TBetter;
  41. }
  42. protected virtual void OnEnable()
  43. {
  44. this.paddingFallback = base.serializedObject.FindProperty("paddingSizerFallback");
  45. this.paddingConfigs = base.serializedObject.FindProperty("customPaddingSizers");
  46. this.spacingFallback = base.serializedObject.FindProperty("spacingSizerFallback");
  47. this.spacingConfigs = base.serializedObject.FindProperty("customSpacingSizers");
  48. this.settingsFallback = base.serializedObject.FindProperty("settingsFallback");
  49. this.settingsConfigs = base.serializedObject.FindProperty("customSettings");
  50. this.hasReverseOption = base.serializedObject.FindProperty("m_ReverseArrangement") != null;
  51. this.hasChildScale = base.serializedObject.FindProperty("m_ChildScaleWidth") != null;
  52. }
  53. public override void OnInspectorGUI()
  54. {
  55. DrawDefaultPaddingAndSpacing();
  56. DrawSettings("", null);
  57. serializedObject.ApplyModifiedProperties();
  58. }
  59. protected void DrawObsoleteWarning()
  60. {
  61. EditorGUILayout.HelpBox(
  62. @"Component is obsolete!
  63. Better Horizontal- and Better Vertical Layout Groups only exist for backwards compatibility.
  64. Please use 'Better Axis Aligned Layout Group' instead.
  65. To do so, just right click on the component and select '♠ Make Better' as usual.", MessageType.Warning);
  66. }
  67. protected void DrawDefaultPaddingAndSpacing()
  68. {
  69. EditorGUILayout.LabelField("Padding", EditorStyles.boldLabel);
  70. EditorGUILayout.PropertyField(paddingFallback);
  71. EditorGUILayout.LabelField("Spacing", EditorStyles.boldLabel);
  72. EditorGUILayout.PropertyField(spacingFallback);
  73. }
  74. protected void DrawPaddingAndSpacingConfigurations()
  75. {
  76. ScreenConfigConnectionHelper.DrawSizerGui("Padding", paddingConfigs, ref paddingFallback);
  77. ScreenConfigConnectionHelper.DrawSizerGui("Spacing", spacingConfigs, ref spacingFallback);
  78. }
  79. protected void DrawSettings(string configName, SerializedProperty parent)
  80. {
  81. Func<string, string, SerializedProperty> findProp = (nameOrig, nameSetting) =>
  82. {
  83. return (parent == null)
  84. ? serializedObject.FindProperty(nameOrig)
  85. : parent.FindPropertyRelative(nameSetting);
  86. };
  87. var orientation = findProp("orientation", "Orientation");
  88. var childAlignment = findProp("m_ChildAlignment", "ChildAlignment");
  89. if (parent != null)
  90. {
  91. EditorGUILayout.BeginVertical("box");
  92. }
  93. if (orientation != null)
  94. {
  95. EditorGUILayout.PropertyField(orientation);
  96. }
  97. // Child Alignment
  98. EditorGUILayout.PropertyField(childAlignment, true);
  99. // Reverse Arrangement
  100. if (hasReverseOption)
  101. {
  102. var reverseArrangement = findProp("m_ReverseArrangement", "ReverseArrangement");
  103. EditorGUILayout.PropertyField(reverseArrangement, true);
  104. }
  105. // Child Control Size
  106. var version = UnityEditorInternal.InternalEditorUtility.GetUnityVersion();
  107. if (version >= new Version(5, 5))
  108. {
  109. var childControlSizeWidth = findProp("m_ChildControlWidth", "ChildControlWidth");
  110. var childControlSizeHeight = findProp("m_ChildControlHeight", "ChildControlHeight");
  111. DrawWidthHeightProperty("Control Child Size", -1, childControlSizeWidth, childControlSizeHeight);
  112. }
  113. // Use Child Scale
  114. if (hasChildScale)
  115. {
  116. var childScaleWidth = findProp("m_ChildScaleWidth", "ChildScaleWidth");
  117. var childScaleHeight = findProp("m_ChildScaleHeight", "ChildScaleHeight");
  118. DrawWidthHeightProperty("Use Child Scale", -3, childScaleWidth, childScaleHeight);
  119. }
  120. // Child Force Expand
  121. var childForceExpandWidth = findProp("m_ChildForceExpandWidth", "ChildForceExpandWidth");
  122. var childForceExpandHeight = findProp("m_ChildForceExpandHeight", "ChildForceExpandHeight");
  123. DrawWidthHeightProperty("Child Force Expand", -2, childForceExpandWidth, childForceExpandHeight);
  124. if (parent != null)
  125. {
  126. EditorGUILayout.EndVertical();
  127. }
  128. }
  129. private static void DrawWidthHeightProperty(string label, int id, SerializedProperty widthProp, SerializedProperty heightProp)
  130. {
  131. Rect rect = EditorGUILayout.GetControlRect();
  132. rect = EditorGUI.PrefixLabel(rect, id, new GUIContent(label));
  133. rect.width = Mathf.Max(50f, (rect.width - 4f) / 3f);
  134. float labelWidth = EditorGUIUtility.labelWidth;
  135. EditorGUIUtility.labelWidth = 50f;
  136. ToggleLeft(rect, widthProp, new GUIContent("Width"));
  137. rect.x = rect.x + (rect.width + 2f);
  138. ToggleLeft(rect, heightProp, new GUIContent("Height"));
  139. EditorGUIUtility.labelWidth = labelWidth;
  140. }
  141. private static void ToggleLeft(Rect position, SerializedProperty property, GUIContent label)
  142. {
  143. bool flag = property.boolValue;
  144. EditorGUI.showMixedValue = property.hasMultipleDifferentValues;
  145. EditorGUI.BeginChangeCheck();
  146. int num = EditorGUI.indentLevel;
  147. EditorGUI.indentLevel = 0;
  148. flag = EditorGUI.ToggleLeft(position, label, flag);
  149. EditorGUI.indentLevel = num;
  150. if (EditorGUI.EndChangeCheck())
  151. {
  152. property.boolValue = (!property.hasMultipleDifferentValues ? !property.boolValue : true);
  153. }
  154. EditorGUI.showMixedValue = false;
  155. }
  156. }
  157. }