uGUITools.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /// Credit Senshi
  2. /// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/ (uGUITools link)
  3. using UnityEditor;
  4. namespace UnityEngine.UI.Extensions
  5. {
  6. public static class uGUITools
  7. {
  8. [MenuItem("uGUI/Anchors to Corners %[")]
  9. static void AnchorsToCorners()
  10. {
  11. if (Selection.transforms == null || Selection.transforms.Length == 0)
  12. {
  13. return;
  14. }
  15. Undo.IncrementCurrentGroup();
  16. Undo.SetCurrentGroupName("AnchorsToCorners");
  17. var undoGroup = Undo.GetCurrentGroup();
  18. foreach (Transform transform in Selection.transforms)
  19. {
  20. RectTransform t = transform as RectTransform;
  21. Undo.RecordObject( t, "AnchorsToCorners" );
  22. RectTransform pt = Selection.activeTransform.parent as RectTransform;
  23. if (t == null || pt == null) return;
  24. Vector2 newAnchorsMin = new Vector2(t.anchorMin.x + t.offsetMin.x / pt.rect.width,
  25. t.anchorMin.y + t.offsetMin.y / pt.rect.height);
  26. Vector2 newAnchorsMax = new Vector2(t.anchorMax.x + t.offsetMax.x / pt.rect.width,
  27. t.anchorMax.y + t.offsetMax.y / pt.rect.height);
  28. t.anchorMin = newAnchorsMin;
  29. t.anchorMax = newAnchorsMax;
  30. t.offsetMin = t.offsetMax = new Vector2(0, 0);
  31. }
  32. Undo.CollapseUndoOperations(undoGroup);
  33. }
  34. [MenuItem("uGUI/Corners to Anchors %]")]
  35. static void CornersToAnchors()
  36. {
  37. if (Selection.transforms == null || Selection.transforms.Length == 0)
  38. {
  39. return;
  40. }
  41. Undo.IncrementCurrentGroup();
  42. Undo.SetCurrentGroupName("CornersToAnchors");
  43. var undoGroup = Undo.GetCurrentGroup();
  44. foreach (Transform transform in Selection.transforms)
  45. {
  46. RectTransform t = transform as RectTransform;
  47. Undo.RecordObject( t, "CornersToAnchors" );
  48. if (t == null) return;
  49. t.offsetMin = t.offsetMax = new Vector2(0, 0);
  50. }
  51. Undo.CollapseUndoOperations(undoGroup);
  52. }
  53. [MenuItem("uGUI/Mirror Horizontally Around Anchors %;")]
  54. static void MirrorHorizontallyAnchors()
  55. {
  56. MirrorHorizontally(false);
  57. }
  58. [MenuItem("uGUI/Mirror Horizontally Around Parent Center %:")]
  59. static void MirrorHorizontallyParent()
  60. {
  61. MirrorHorizontally(true);
  62. }
  63. static void MirrorHorizontally(bool mirrorAnchors)
  64. {
  65. foreach (Transform transform in Selection.transforms)
  66. {
  67. RectTransform t = transform as RectTransform;
  68. RectTransform pt = Selection.activeTransform.parent as RectTransform;
  69. if (t == null || pt == null) return;
  70. if (mirrorAnchors)
  71. {
  72. Vector2 oldAnchorMin = t.anchorMin;
  73. t.anchorMin = new Vector2(1 - t.anchorMax.x, t.anchorMin.y);
  74. t.anchorMax = new Vector2(1 - oldAnchorMin.x, t.anchorMax.y);
  75. }
  76. Vector2 oldOffsetMin = t.offsetMin;
  77. t.offsetMin = new Vector2(-t.offsetMax.x, t.offsetMin.y);
  78. t.offsetMax = new Vector2(-oldOffsetMin.x, t.offsetMax.y);
  79. t.localScale = new Vector3(-t.localScale.x, t.localScale.y, t.localScale.z);
  80. }
  81. }
  82. [MenuItem("uGUI/Mirror Vertically Around Anchors %'")]
  83. static void MirrorVerticallyAnchors()
  84. {
  85. MirrorVertically(false);
  86. }
  87. [MenuItem("uGUI/Mirror Vertically Around Parent Center %\"")]
  88. static void MirrorVerticallyParent()
  89. {
  90. MirrorVertically(true);
  91. }
  92. static void MirrorVertically(bool mirrorAnchors)
  93. {
  94. foreach (Transform transform in Selection.transforms)
  95. {
  96. RectTransform t = transform as RectTransform;
  97. RectTransform pt = Selection.activeTransform.parent as RectTransform;
  98. if (t == null || pt == null) return;
  99. if (mirrorAnchors)
  100. {
  101. Vector2 oldAnchorMin = t.anchorMin;
  102. t.anchorMin = new Vector2(t.anchorMin.x, 1 - t.anchorMax.y);
  103. t.anchorMax = new Vector2(t.anchorMax.x, 1 - oldAnchorMin.y);
  104. }
  105. Vector2 oldOffsetMin = t.offsetMin;
  106. t.offsetMin = new Vector2(t.offsetMin.x, -t.offsetMax.y);
  107. t.offsetMax = new Vector2(t.offsetMax.x, -oldOffsetMin.y);
  108. t.localScale = new Vector3(t.localScale.x, -t.localScale.y, t.localScale.z);
  109. }
  110. }
  111. }
  112. }