7 public static class uGUITools
9 [MenuItem(
"uGUI/Anchors to Corners %[")]
10 static void AnchorsToCorners()
12 if (Selection.transforms ==
null || Selection.transforms.Length == 0)
16 Undo.IncrementCurrentGroup();
17 Undo.SetCurrentGroupName(
"AnchorsToCorners");
18 var undoGroup = Undo.GetCurrentGroup();
20 foreach (Transform transform
in Selection.transforms)
22 RectTransform t = transform as RectTransform;
23 Undo.RecordObject( t,
"AnchorsToCorners" );
24 RectTransform pt = Selection.activeTransform.parent as RectTransform;
26 if (t ==
null || pt ==
null)
return;
28 Vector2 newAnchorsMin =
new Vector2(t.anchorMin.x + t.offsetMin.x / pt.rect.width,
29 t.anchorMin.y + t.offsetMin.y / pt.rect.height);
30 Vector2 newAnchorsMax =
new Vector2(t.anchorMax.x + t.offsetMax.x / pt.rect.width,
31 t.anchorMax.y + t.offsetMax.y / pt.rect.height);
33 t.anchorMin = newAnchorsMin;
34 t.anchorMax = newAnchorsMax;
35 t.offsetMin = t.offsetMax =
new Vector2(0, 0);
37 Undo.CollapseUndoOperations(undoGroup);
40 [MenuItem(
"uGUI/Corners to Anchors %]")]
41 static void CornersToAnchors()
43 if (Selection.transforms ==
null || Selection.transforms.Length == 0)
47 Undo.IncrementCurrentGroup();
48 Undo.SetCurrentGroupName(
"CornersToAnchors");
49 var undoGroup = Undo.GetCurrentGroup();
51 foreach (Transform transform
in Selection.transforms)
53 RectTransform t = transform as RectTransform;
54 Undo.RecordObject( t,
"CornersToAnchors" );
56 if (t ==
null)
return;
58 t.offsetMin = t.offsetMax =
new Vector2(0, 0);
60 Undo.CollapseUndoOperations(undoGroup);
63 [MenuItem(
"uGUI/Mirror Horizontally Around Anchors %;")]
64 static void MirrorHorizontallyAnchors()
66 MirrorHorizontally(
false);
69 [MenuItem(
"uGUI/Mirror Horizontally Around Parent Center %:")]
70 static void MirrorHorizontallyParent()
72 MirrorHorizontally(
true);
75 static void MirrorHorizontally(
bool mirrorAnchors)
77 foreach (Transform transform
in Selection.transforms)
79 RectTransform t = transform as RectTransform;
80 RectTransform pt = Selection.activeTransform.parent as RectTransform;
82 if (t ==
null || pt ==
null)
return;
86 Vector2 oldAnchorMin = t.anchorMin;
87 t.anchorMin =
new Vector2(1 - t.anchorMax.x, t.anchorMin.y);
88 t.anchorMax =
new Vector2(1 - oldAnchorMin.x, t.anchorMax.y);
91 Vector2 oldOffsetMin = t.offsetMin;
92 t.offsetMin =
new Vector2(-t.offsetMax.x, t.offsetMin.y);
93 t.offsetMax =
new Vector2(-oldOffsetMin.x, t.offsetMax.y);
95 t.localScale =
new Vector3(-t.localScale.x, t.localScale.y, t.localScale.z);
99 [MenuItem(
"uGUI/Mirror Vertically Around Anchors %'")]
100 static void MirrorVerticallyAnchors()
102 MirrorVertically(
false);
105 [MenuItem(
"uGUI/Mirror Vertically Around Parent Center %\"")]
106 static void MirrorVerticallyParent()
108 MirrorVertically(
true);
111 static void MirrorVertically(
bool mirrorAnchors)
113 foreach (Transform transform
in Selection.transforms)
115 RectTransform t = transform as RectTransform;
116 RectTransform pt = Selection.activeTransform.parent as RectTransform;
118 if (t ==
null || pt ==
null)
return;
122 Vector2 oldAnchorMin = t.anchorMin;
123 t.anchorMin =
new Vector2(t.anchorMin.x, 1 - t.anchorMax.y);
124 t.anchorMax =
new Vector2(t.anchorMax.x, 1 - oldAnchorMin.y);
127 Vector2 oldOffsetMin = t.offsetMin;
128 t.offsetMin =
new Vector2(t.offsetMin.x, -t.offsetMax.y);
129 t.offsetMax =
new Vector2(t.offsetMax.x, -oldOffsetMin.y);
131 t.localScale =
new Vector3(t.localScale.x, -t.localScale.y, t.localScale.z);
Credit Erdener Gonenc - @PixelEnvision.