BetterLocatorEditor.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using UnityEditor;
  7. using UnityEngine;
  8. namespace TheraBytes.BetterUi.Editor
  9. {
  10. [CustomEditor(typeof(BetterLocator)), CanEditMultipleObjects]
  11. public class BetterLocatorEditor : UnityEditor.Editor
  12. {
  13. SerializedProperty transformFallback, transformConfigs;
  14. BetterLocator locator;
  15. Dictionary<RectTransformData, bool> anchorExpands = new Dictionary<RectTransformData, bool>();
  16. static bool autoPullFromTransform = true;
  17. static bool autoPushToTransform = false;
  18. bool autoPull, autoPush;
  19. bool hasOffsetter;
  20. bool pauseAutoPushOnce = false; // recursive infinite loop protection
  21. protected virtual void OnEnable()
  22. {
  23. this.locator = target as BetterLocator;
  24. transformFallback = serializedObject.FindProperty("transformFallback");
  25. transformConfigs = serializedObject.FindProperty("transformConfigs");
  26. hasOffsetter = locator.gameObject.GetComponent<BetterOffsetter>() != null;
  27. autoPull = autoPullFromTransform && !hasOffsetter;
  28. autoPush = autoPushToTransform && !hasOffsetter;
  29. this.locator.OnValidate();
  30. }
  31. public override void OnInspectorGUI()
  32. {
  33. string currentScreen = ResolutionMonitor.CurrentScreenConfiguration?.Name;
  34. bool isCurrentScreenConfig = currentScreen == null // fallback is always present
  35. || locator.CurrentTransformData.ScreenConfigName == currentScreen;
  36. if (isCurrentScreenConfig)
  37. {
  38. EditorGUILayout.PrefixLabel("Live Update");
  39. EditorGUILayout.BeginHorizontal();
  40. EditorGUI.BeginDisabledGroup(!locator.isActiveAndEnabled);
  41. bool newPull = GUILayout.Toggle(autoPull, "↓ Auto-Pull", "ButtonLeft", GUILayout.MinHeight(30));
  42. bool newPush = GUILayout.Toggle(autoPush, "↑ Auto-Push", "ButtonRight", GUILayout.MinHeight(30));
  43. EditorGUI.EndDisabledGroup();
  44. EditorGUILayout.EndHorizontal();
  45. if(newPull != autoPull)
  46. {
  47. autoPull = newPull;
  48. autoPullFromTransform = newPull;
  49. }
  50. if (newPush != autoPush)
  51. {
  52. autoPush = newPush;
  53. autoPushToTransform = newPush;
  54. }
  55. }
  56. else
  57. {
  58. EditorGUILayout.BeginVertical(GUILayout.MinHeight(52));
  59. GUILayout.FlexibleSpace();
  60. EditorGUILayout.HelpBox($"To prevent accidentally overriding the wrong config, live update is disabled. It is enabled only if you have a config for the current screen configuration ('{currentScreen}') present.",
  61. MessageType.Info);
  62. EditorGUILayout.EndHorizontal();
  63. }
  64. if (autoPull && locator.isActiveAndEnabled && isCurrentScreenConfig)
  65. {
  66. locator.CurrentTransformData.PullFromTransform(locator.transform as RectTransform);
  67. }
  68. ScreenConfigConnectionHelper.DrawGui("Rect Transform Override", transformConfigs, ref transformFallback, DrawTransformData);
  69. if (autoPush && !(pauseAutoPushOnce) && isCurrentScreenConfig)
  70. {
  71. locator.CurrentTransformData.PushToTransform(locator.transform as RectTransform);
  72. }
  73. pauseAutoPushOnce = false;
  74. }
  75. void DrawTransformData(string configName, SerializedProperty prop)
  76. {
  77. RectTransformData data = prop.GetValue<RectTransformData>();
  78. bool isCurrent = locator.CurrentTransformData == data;
  79. if (!(anchorExpands.ContainsKey(data)))
  80. {
  81. anchorExpands.Add(data, true);
  82. }
  83. bool anchorExpand = anchorExpands[data];
  84. float height = (anchorExpand)
  85. ? RectTransformDataDrawer.HeightWithAnchorsExpanded
  86. : RectTransformDataDrawer.HeightWithoutAnchorsExpanded;
  87. EditorGUILayout.BeginVertical("box");
  88. Rect bounds = EditorGUILayout.GetControlRect(false, height);
  89. bool canEdit = !(isCurrent) || !(autoPull) || autoPush;
  90. // Pull
  91. EditorGUI.BeginDisabledGroup(isCurrent && autoPull);
  92. if (GUI.Button(new Rect(bounds.position + new Vector2(5, 5), new Vector2(40, 40)), "Pull\n↓"))
  93. {
  94. Undo.RecordObject(locator, "Pull From Rect Transform");
  95. data.PullFromTransform(locator.transform as RectTransform);
  96. }
  97. EditorGUI.EndDisabledGroup();
  98. // Push
  99. EditorGUI.BeginDisabledGroup(!(canEdit) || (isCurrent && autoPush));
  100. if (GUI.Button(new Rect(bounds.position + new Vector2(50, 25), new Vector2(40, 40)), "↑\nPush"))
  101. {
  102. Undo.RecordObject(locator.transform, "Push To Rect Transform");
  103. data.PushToTransform(locator.transform as RectTransform);
  104. pauseAutoPushOnce = true;
  105. }
  106. EditorGUI.EndDisabledGroup();
  107. // Fields
  108. if(!canEdit)
  109. {
  110. EditorGUI.BeginDisabledGroup(true);
  111. }
  112. RectTransformDataDrawer.DrawFields(prop, data, bounds, ref anchorExpand);
  113. anchorExpands[data] = anchorExpand;
  114. if (!canEdit)
  115. {
  116. EditorGUI.EndDisabledGroup();
  117. }
  118. EditorGUILayout.EndVertical();
  119. }
  120. [MenuItem("CONTEXT/RectTransform/♠ Add Better Locator", false)]
  121. public static void AddBetterLocator(MenuCommand command)
  122. {
  123. var ctx = command.context as RectTransform;
  124. var locator = ctx.gameObject.AddComponent<BetterLocator>();
  125. while(UnityEditorInternal.ComponentUtility.MoveComponentUp(locator))
  126. { }
  127. }
  128. [MenuItem("CONTEXT/RectTransform/♠ Add Better Locator", true)]
  129. public static bool CheckBetterLocator(MenuCommand command)
  130. {
  131. var ctx = command.context as RectTransform;
  132. return ctx.gameObject.GetComponent<BetterLocator>() == null;
  133. }
  134. }
  135. }