SmartParentWindow.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace TheraBytes.BetterUi.Editor
  8. {
  9. public class SmartParentWindow : EditorWindow
  10. {
  11. bool isFreeMovementEnabled;
  12. RectTransform selection;
  13. RectTransformData previousTransform;
  14. Texture2D snapAllPic, snapVerticalPic, snapHorizontalPic, freeParentModeOnPic, freeParentModeOffPic;
  15. GUIContent snapAllContent, snapVerticalContent, snapHorizontalContent, freeParentModeOnContent, freeParentModeOffContent;
  16. [MenuItem("Tools/Better UI/Smart Parent", false, 61)]
  17. public static void ShowWindow()
  18. {
  19. EditorWindow.GetWindow(typeof(SmartParentWindow), false, "Smart Parent");
  20. }
  21. void OnEnable()
  22. {
  23. minSize = new Vector2(195, 245);
  24. isFreeMovementEnabled = false;
  25. snapAllPic = Resources.Load<Texture2D>("snap_to_childs_all");
  26. snapHorizontalPic = Resources.Load<Texture2D>("snap_to_childs_h");
  27. snapVerticalPic = Resources.Load<Texture2D>("snap_to_childs_v");
  28. freeParentModeOnPic = Resources.Load<Texture2D>("free_parent_mode_on");
  29. freeParentModeOffPic = Resources.Load<Texture2D>("free_parent_mode_off");
  30. snapAllContent = new GUIContent(snapAllPic, "Trims size to children horizontally and vertically. Also snap Anchors to borders.");
  31. snapVerticalContent = new GUIContent(snapVerticalPic, "Trims size to children vertically. Also snap Anchors to borders vertically.");
  32. snapHorizontalContent = new GUIContent(snapHorizontalPic, "Trims size to children horizontally. Also snap Anchors to borders horizontally.");
  33. freeParentModeOnContent = new GUIContent(freeParentModeOnPic, "When this mode is enabled children are not moved along with the parent.");
  34. freeParentModeOffContent = new GUIContent(freeParentModeOffPic, "When this mode is enabled children are not moved along with the parent.");
  35. Selection.selectionChanged += SelectionChanged;
  36. EditorApplication.update += UpdateTransforms;
  37. SelectionChanged();
  38. }
  39. void OnDisable()
  40. {
  41. isFreeMovementEnabled = false;
  42. Selection.selectionChanged -= SelectionChanged;
  43. EditorApplication.update -= UpdateTransforms;
  44. }
  45. void OnGUI()
  46. {
  47. EditorGUILayout.Space();
  48. var go = Selection.activeObject as GameObject;
  49. bool canSelectParent = Selection.objects.Length == 1
  50. && go != null
  51. && go.transform as RectTransform != null
  52. && go.transform.parent != null;
  53. if (canSelectParent)
  54. {
  55. if (GUILayout.Button("Select Parent", EditorStyles.miniButton))
  56. {
  57. Selection.activeObject = go.transform.parent.gameObject;
  58. }
  59. }
  60. else
  61. {
  62. GUILayout.Label("");
  63. }
  64. EditorGUILayout.Space();
  65. EditorGUI.BeginDisabledGroup(selection == null);
  66. if (selection != null)
  67. {
  68. EditorGUILayout.LabelField(selection.name, EditorStyles.centeredGreyMiniLabel);
  69. }
  70. else
  71. {
  72. DrawEmphasisedLabel("No valid object selected.");
  73. }
  74. EditorGUILayout.Space();
  75. EditorGUILayout.BeginHorizontal();
  76. GUILayout.FlexibleSpace();
  77. #region snap all
  78. if (GUILayout.Button(snapAllContent, GUILayout.Width(120), GUILayout.Height(120)))
  79. {
  80. SnapToChildren(true, true);
  81. }
  82. #endregion
  83. #region snap vertically
  84. if (GUILayout.Button(snapVerticalContent, GUILayout.Width(60), GUILayout.Height(120)))
  85. {
  86. SnapToChildren(false, true);
  87. }
  88. #endregion
  89. GUILayout.FlexibleSpace();
  90. EditorGUILayout.EndHorizontal();
  91. EditorGUILayout.BeginHorizontal();
  92. GUILayout.FlexibleSpace();
  93. #region snap horizontally
  94. if (GUILayout.Button(snapHorizontalContent, GUILayout.Width(120), GUILayout.Height(60)))
  95. {
  96. SnapToChildren(true, false);
  97. }
  98. #endregion
  99. EditorGUI.EndDisabledGroup();
  100. #region free parent mode
  101. bool prev = isFreeMovementEnabled;
  102. var content = (prev) ? freeParentModeOnContent : freeParentModeOffContent;
  103. isFreeMovementEnabled = GUILayout.Toggle(isFreeMovementEnabled, content, "Button", GUILayout.Width(60), GUILayout.Height(60));
  104. bool turnedOn = !prev && isFreeMovementEnabled;
  105. if (turnedOn && selection != null)
  106. {
  107. previousTransform = new RectTransformData(selection);
  108. }
  109. #endregion
  110. GUILayout.FlexibleSpace();
  111. EditorGUILayout.EndHorizontal();
  112. if (isFreeMovementEnabled && selection != null)
  113. {
  114. DrawEmphasisedLabel("Children are detached.");
  115. }
  116. }
  117. private static void DrawEmphasisedLabel(string text)
  118. {
  119. GUIStyle warn = GUI.skin.GetStyle("WarningOverlay");
  120. EditorGUILayout.BeginHorizontal();
  121. GUILayout.Space(5);
  122. GUILayout.Label(text, warn);
  123. GUILayout.Space(5);
  124. EditorGUILayout.EndHorizontal();
  125. }
  126. private void SelectionChanged()
  127. {
  128. var sel = Selection.GetFiltered(typeof(RectTransform), SelectionMode.TopLevel);
  129. if (sel.Length != 1)
  130. {
  131. selection = null;
  132. this.Repaint();
  133. return;
  134. }
  135. var rt = sel[0] as RectTransform;
  136. if(rt.childCount == 0 || rt.parent == null)
  137. {
  138. selection = null;
  139. this.Repaint();
  140. return;
  141. }
  142. if (rt == selection)
  143. return;
  144. selection = rt;
  145. previousTransform = new RectTransformData(selection);
  146. this.Repaint();
  147. }
  148. private void UpdateTransforms()
  149. {
  150. if (!isFreeMovementEnabled || selection == null)
  151. return;
  152. RectTransformData currentTransform = new RectTransformData(selection);
  153. UpdateTransforms(selection, currentTransform, previousTransform);
  154. previousTransform = currentTransform;
  155. }
  156. private void SnapToChildren(bool snapHorizontally, bool snapVertically)
  157. {
  158. if (selection == null)
  159. return;
  160. if (selection.childCount == 0)
  161. return;
  162. float xMin = float.MaxValue;
  163. float yMin = float.MaxValue;
  164. float xMax = float.MinValue;
  165. float yMax = float.MinValue;
  166. foreach (var child in selection)
  167. {
  168. var rt = child as RectTransform;
  169. if (rt == null || !rt.gameObject.activeSelf)
  170. continue;
  171. Rect rect = rt.ToScreenRect(startAtBottom: true);
  172. xMin = Mathf.Min(xMin, rect.xMin);
  173. yMin = Mathf.Min(yMin, rect.yMin);
  174. xMax = Mathf.Max(xMax, rect.xMax);
  175. yMax = Mathf.Max(yMax, rect.yMax);
  176. }
  177. Rect childBounds = Rect.MinMaxRect(xMin, yMin, xMax, yMax);
  178. var parent = selection.parent as RectTransform;
  179. Rect parentRect = (parent != null)
  180. ? parent.ToScreenRect(startAtBottom: true)
  181. : new Rect(0, 0, Screen.width, Screen.height);
  182. RectTransformData prev = new RectTransformData(selection);
  183. RectTransformData cur = new RectTransformData().PullFromData(prev);
  184. if (snapHorizontally)
  185. {
  186. Snap(cur, 0, childBounds.xMin, childBounds.xMax, parentRect.xMin, parentRect.width);
  187. }
  188. if (snapVertically)
  189. {
  190. Snap(cur, 1, childBounds.yMin, childBounds.yMax, parentRect.yMin, parentRect.height);
  191. }
  192. #region do actual operation with undo
  193. Undo.RecordObject(selection, "Snap To Children " + DateTime.Now.ToFileTime());
  194. int group = Undo.GetCurrentGroup();
  195. // push!
  196. cur.PushToTransform(selection);
  197. foreach(Transform child in selection)
  198. {
  199. Undo.RecordObject(child, "transform child");
  200. }
  201. if (!isFreeMovementEnabled)
  202. {
  203. // update child positions
  204. UpdateTransforms(selection, cur, prev);
  205. }
  206. Undo.CollapseUndoOperations(group);
  207. #endregion
  208. }
  209. private static void Snap(RectTransformData data, int axis, float min, float max, float parentMin, float parentSize)
  210. {
  211. data.AnchorMin[axis] = (min - parentMin) / parentSize;
  212. data.AnchorMax[axis] = (max - parentMin) / parentSize;
  213. data.AnchoredPosition[axis] = 0;
  214. data.SizeDelta[axis] = 0;
  215. }
  216. private static void UpdateTransforms(RectTransform selection, RectTransformData currentTransform, RectTransformData previousTransform)
  217. {
  218. if (currentTransform == previousTransform)
  219. return;
  220. RectTransform parent = selection.parent as RectTransform;
  221. Rect parentRect = parent.rect;
  222. Rect cur = currentTransform.ToRect(parentRect, relativeSpace: true);
  223. bool isCurZero = Mathf.Approximately(cur.width, 0) || Mathf.Approximately(cur.height, 0);
  224. Rect prev = previousTransform.ToRect(parentRect, relativeSpace: true);
  225. bool isPrevZero = Mathf.Approximately(prev.width, 0) || Mathf.Approximately(prev.height, 0);
  226. if (isCurZero || isPrevZero)
  227. {
  228. return;
  229. }
  230. float scaleH = 1 / cur.width;
  231. float scaleV = 1 / cur.height;
  232. foreach (var child in selection)
  233. {
  234. RectTransform rt = child as RectTransform;
  235. if (rt == null)
  236. continue;
  237. // prev to parent-parent-relative-space
  238. float xMin = prev.x + prev.width * rt.anchorMin.x;
  239. float xMax = prev.x + prev.width * rt.anchorMax.x;
  240. float yMin = prev.y + prev.height * rt.anchorMin.y;
  241. float yMax = prev.y + prev.height * rt.anchorMax.y;
  242. // parent-parent-relative-space to cur
  243. xMin = xMin * scaleH - cur.x * scaleH;
  244. xMax = xMax * scaleH - cur.x * scaleH;
  245. yMin = yMin * scaleV - cur.y * scaleV;
  246. yMax = yMax * scaleV - cur.y * scaleV;
  247. // assign calculated values
  248. rt.anchorMin = new Vector2(xMin, yMin);
  249. rt.anchorMax = new Vector2(xMax, yMax);
  250. }
  251. }
  252. }
  253. }