UIAssistanceToolsWindow.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEditor;
  6. public class UIAssistanceToolsWindow : EditorWindow {
  7. private Texture anchorsToRectIcon;
  8. private Texture rectToAnchorsIcon;
  9. private Texture anchorsToParentIcon;
  10. private Texture rectToParentIcon;
  11. private Texture rAndAToParentIcon;
  12. private Texture centerAnchorstIcon;
  13. private Vector2 scrollPos;
  14. [MenuItem("Tools/ZUI/UI Assistance/Open window...", false, 26)]
  15. static void OpenWindow()
  16. {
  17. GetWindow(typeof(UIAssistanceToolsWindow));
  18. }
  19. void Awake()
  20. {
  21. anchorsToRectIcon = (Texture)EditorGUIUtility.Load("ZUI/UIAssistanceIcons/AnchorsToRect.png");
  22. rectToAnchorsIcon = (Texture)EditorGUIUtility.Load("ZUI/UIAssistanceIcons/RectToAnchors.png");
  23. anchorsToParentIcon = (Texture)EditorGUIUtility.Load("ZUI/UIAssistanceIcons/AnchorsToParent.png");
  24. rectToParentIcon = (Texture)EditorGUIUtility.Load("ZUI/UIAssistanceIcons/RectToParent.png");
  25. rAndAToParentIcon = (Texture)EditorGUIUtility.Load("ZUI/UIAssistanceIcons/RectAndAnchorsToParent.png");
  26. centerAnchorstIcon = (Texture)EditorGUIUtility.Load("ZUI/UIAssistanceIcons/CenterAnchors.png");
  27. }
  28. void OnGUI()
  29. {
  30. bool horizontal = position.width > position.height;
  31. scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
  32. if (horizontal)
  33. EditorGUILayout.BeginHorizontal();
  34. else
  35. EditorGUILayout.BeginVertical();
  36. if (GUILayout.Button(anchorsToRectIcon, GUILayout.Height(80), GUILayout.Width(80)))
  37. FitAnchorsToRect();
  38. if (GUILayout.Button(rectToAnchorsIcon, GUILayout.Height(80), GUILayout.Width(80)))
  39. FitRectToAnchors();
  40. GUILayout.Space(20);
  41. GUILayout.Box("", GUILayout.Height(horizontal? 80 : 5), GUILayout.Width(horizontal? 5 : 80));
  42. GUILayout.Space(20);
  43. if (GUILayout.Button(anchorsToParentIcon, GUILayout.Height(80), GUILayout.Width(80)))
  44. FitAnchorsToParent();
  45. if (GUILayout.Button(rectToParentIcon, GUILayout.Height(80), GUILayout.Width(80)))
  46. FitRectToParent();
  47. if (GUILayout.Button(rAndAToParentIcon, GUILayout.Height(80), GUILayout.Width(80)))
  48. FitAnchorAndRectToParent();
  49. GUILayout.Space(20);
  50. GUILayout.Box("", GUILayout.Height(horizontal ? 80 : 5), GUILayout.Width(horizontal ? 5 : 80));
  51. GUILayout.Space(20);
  52. if (GUILayout.Button(centerAnchorstIcon, GUILayout.Height(80), GUILayout.Width(80)))
  53. CenterAnchors();
  54. if (horizontal)
  55. EditorGUILayout.EndHorizontal();
  56. else
  57. EditorGUILayout.EndVertical();
  58. EditorGUILayout.EndScrollView();
  59. }
  60. void OnDestroy()
  61. {
  62. anchorsToRectIcon = null;
  63. EditorUtility.UnloadUnusedAssetsImmediate();
  64. }
  65. //[MenuItem("ZUI/UI Assistance/", false, 20)]
  66. [MenuItem("Tools/ZUI/UI Assistance/Fit Anchors to Rect %#x", false, 0)]
  67. static void FitAnchorsToRect()
  68. {
  69. foreach (GameObject go in Selection.gameObjects)
  70. {
  71. RectTransform selectedRT = go.GetComponent<RectTransform>();
  72. RectTransform parentRT = null;
  73. if (go.transform.parent)
  74. parentRT = go.transform.parent.GetComponent<RectTransform>();
  75. if (!selectedRT || !parentRT) continue;
  76. AspectRatioFitter arf = selectedRT.GetComponent<AspectRatioFitter>();
  77. bool arfDisabled = false;
  78. if (arf && arf.enabled)
  79. {
  80. arf.enabled = false;
  81. arfDisabled = true;
  82. }
  83. Undo.RecordObject(selectedRT, "Change Anchor");
  84. selectedRT.anchorMin = new Vector2(selectedRT.anchorMin.x + selectedRT.offsetMin.x / parentRT.rect.width,
  85. selectedRT.anchorMin.y + selectedRT.offsetMin.y / parentRT.rect.height);
  86. selectedRT.anchorMax = new Vector2(selectedRT.anchorMax.x + selectedRT.offsetMax.x / parentRT.rect.width,
  87. selectedRT.anchorMax.y + selectedRT.offsetMax.y / parentRT.rect.height);
  88. selectedRT.offsetMin = selectedRT.offsetMax = Vector2.zero;
  89. if (arfDisabled)
  90. arf.enabled = true;
  91. }
  92. }
  93. [MenuItem("Tools/ZUI/UI Assistance/Fit Rect to Anchors %#c", false, 1)]
  94. static void FitRectToAnchors()
  95. {
  96. foreach (GameObject go in Selection.gameObjects)
  97. {
  98. RectTransform selectedRT = go.GetComponent<RectTransform>();
  99. if (!selectedRT) continue;
  100. Undo.RecordObject(selectedRT, "Change Rect");
  101. selectedRT.offsetMin = selectedRT.offsetMax = Vector2.zero;
  102. }
  103. }
  104. [MenuItem("Tools/ZUI/UI Assistance/Fit Anchors to Parent", false, 2)]
  105. static void FitAnchorsToParent()
  106. {
  107. foreach (GameObject go in Selection.gameObjects)
  108. {
  109. RectTransform selectedRT = go.GetComponent<RectTransform>();
  110. RectTransform parentRT = null;
  111. if (go.transform.parent)
  112. parentRT = go.transform.parent.GetComponent<RectTransform>();
  113. if (!selectedRT || !parentRT) continue;
  114. Undo.RecordObject(selectedRT, "Fit anchors to parent");
  115. Rect parentRect = parentRT.rect;
  116. Vector2 lastOffMin = selectedRT.offsetMin;
  117. Vector2 lastOffMax = selectedRT.offsetMax;
  118. Vector2 lastAnchMin = selectedRT.anchorMin;
  119. Vector2 lastAnchMax = selectedRT.anchorMax;
  120. selectedRT.anchorMin = Vector2.zero;
  121. selectedRT.anchorMax = Vector2.one;
  122. selectedRT.offsetMin = new Vector2(lastOffMin.x + (lastAnchMin.x - selectedRT.anchorMin.x) * parentRect.width, lastOffMin.y + (lastAnchMin.y - selectedRT.anchorMin.y) * parentRect.height);
  123. selectedRT.offsetMax = new Vector2(lastOffMax.x + (lastAnchMax.x - selectedRT.anchorMax.x) * parentRect.width, lastOffMax.y + (lastAnchMax.y - selectedRT.anchorMax.y) * parentRect.height);
  124. }
  125. }
  126. [MenuItem("Tools/ZUI/UI Assistance/Fit Rect to Parent", false, 3)]
  127. static void FitRectToParent()
  128. {
  129. foreach (GameObject go in Selection.gameObjects)
  130. {
  131. RectTransform selectedRT = go.GetComponent<RectTransform>();
  132. RectTransform parentRT = null;
  133. if (go.transform.parent)
  134. parentRT = go.transform.parent.GetComponent<RectTransform>();
  135. if (!selectedRT || !parentRT) continue;
  136. Undo.RecordObject(selectedRT, "Fit anchors to parent");
  137. Rect parentRect = parentRT.rect;
  138. selectedRT.offsetMin = Vector2.zero;
  139. selectedRT.offsetMax = new Vector2(parentRect.width - (selectedRT.anchorMax.x * parentRect.width) + (selectedRT.anchorMin.x * parentRect.width), parentRect.height - (selectedRT.anchorMax.y * parentRect.height) + (selectedRT.anchorMin.y * parentRect.height));
  140. Vector2 lastPivot = selectedRT.pivot;
  141. selectedRT.pivot = parentRT.pivot;
  142. selectedRT.localPosition = Vector2.zero;
  143. Vector2 changeInPosition = new Vector3((lastPivot.x - parentRT.pivot.x) * parentRect.width, (lastPivot.y - parentRT.pivot.y) * parentRect.height);
  144. selectedRT.pivot = lastPivot;
  145. selectedRT.localPosition = changeInPosition;
  146. }
  147. }
  148. [MenuItem("Tools/ZUI/UI Assistance/Fit Anchors and Rect to Parent", false, 4)]
  149. static void FitAnchorAndRectToParent()
  150. {
  151. foreach (GameObject go in Selection.gameObjects)
  152. {
  153. RectTransform selectedRT = go.GetComponent<RectTransform>();
  154. RectTransform parentRT = null;
  155. if (go.transform.parent)
  156. parentRT = go.transform.parent.GetComponent<RectTransform>();
  157. if (!selectedRT || !parentRT) continue;
  158. Undo.RecordObject(selectedRT, "Fit to parent");
  159. selectedRT.anchorMin = Vector2.zero;
  160. selectedRT.anchorMax = Vector2.one;
  161. selectedRT.offsetMin = selectedRT.offsetMax = Vector2.zero;
  162. }
  163. }
  164. [MenuItem("Tools/ZUI/UI Assistance/Center Anchors", false, 15)]
  165. static void CenterAnchors()
  166. {
  167. foreach (GameObject go in Selection.gameObjects)
  168. {
  169. RectTransform selectedRT = go.GetComponent<RectTransform>();
  170. RectTransform parentRT = null;
  171. if (go.transform.parent)
  172. parentRT = go.transform.parent.GetComponent<RectTransform>();
  173. if (!selectedRT || !parentRT) continue;
  174. Undo.RecordObject(selectedRT, "Fit anchors to parent");
  175. Rect parentRect = parentRT.rect;
  176. Vector2 lastSize = new Vector2(selectedRT.rect.width, selectedRT.rect.height);
  177. Vector2 lastAnchMin = selectedRT.anchorMin;
  178. Vector2 lastAnchMax = selectedRT.anchorMax;
  179. float minX = ((parentRect.width * selectedRT.anchorMin.x) + selectedRT.offsetMin.x + (selectedRT.rect.width * selectedRT.pivot.x)) / parentRect.width;
  180. float minY = ((parentRect.height * selectedRT.anchorMin.y) + selectedRT.offsetMin.y + (selectedRT.rect.height * selectedRT.pivot.y)) / parentRect.height;
  181. selectedRT.anchorMin = new Vector2(minX, minY);
  182. selectedRT.anchorMax = selectedRT.anchorMin;
  183. selectedRT.sizeDelta = lastSize;
  184. selectedRT.anchoredPosition = Vector2.zero;
  185. }
  186. }
  187. }