Tanoda
uGUITools.cs
Go to the documentation of this file.
1
3
4using UnityEditor;
6{
7 public static class uGUITools
8 {
9 [MenuItem("uGUI/Anchors to Corners %[")]
10 static void AnchorsToCorners()
11 {
12 if (Selection.transforms == null || Selection.transforms.Length == 0)
13 {
14 return;
15 }
16 Undo.IncrementCurrentGroup();
17 Undo.SetCurrentGroupName("AnchorsToCorners");
18 var undoGroup = Undo.GetCurrentGroup();
19
20 foreach (Transform transform in Selection.transforms)
21 {
22 RectTransform t = transform as RectTransform;
23 Undo.RecordObject( t, "AnchorsToCorners" );
24 RectTransform pt = Selection.activeTransform.parent as RectTransform;
25
26 if (t == null || pt == null) return;
27
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);
32
33 t.anchorMin = newAnchorsMin;
34 t.anchorMax = newAnchorsMax;
35 t.offsetMin = t.offsetMax = new Vector2(0, 0);
36 }
37 Undo.CollapseUndoOperations(undoGroup);
38 }
39
40 [MenuItem("uGUI/Corners to Anchors %]")]
41 static void CornersToAnchors()
42 {
43 if (Selection.transforms == null || Selection.transforms.Length == 0)
44 {
45 return;
46 }
47 Undo.IncrementCurrentGroup();
48 Undo.SetCurrentGroupName("CornersToAnchors");
49 var undoGroup = Undo.GetCurrentGroup();
50
51 foreach (Transform transform in Selection.transforms)
52 {
53 RectTransform t = transform as RectTransform;
54 Undo.RecordObject( t, "CornersToAnchors" );
55
56 if (t == null) return;
57
58 t.offsetMin = t.offsetMax = new Vector2(0, 0);
59 }
60 Undo.CollapseUndoOperations(undoGroup);
61 }
62
63 [MenuItem("uGUI/Mirror Horizontally Around Anchors %;")]
64 static void MirrorHorizontallyAnchors()
65 {
66 MirrorHorizontally(false);
67 }
68
69 [MenuItem("uGUI/Mirror Horizontally Around Parent Center %:")]
70 static void MirrorHorizontallyParent()
71 {
72 MirrorHorizontally(true);
73 }
74
75 static void MirrorHorizontally(bool mirrorAnchors)
76 {
77 foreach (Transform transform in Selection.transforms)
78 {
79 RectTransform t = transform as RectTransform;
80 RectTransform pt = Selection.activeTransform.parent as RectTransform;
81
82 if (t == null || pt == null) return;
83
84 if (mirrorAnchors)
85 {
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);
89 }
90
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);
94
95 t.localScale = new Vector3(-t.localScale.x, t.localScale.y, t.localScale.z);
96 }
97 }
98
99 [MenuItem("uGUI/Mirror Vertically Around Anchors %'")]
100 static void MirrorVerticallyAnchors()
101 {
102 MirrorVertically(false);
103 }
104
105 [MenuItem("uGUI/Mirror Vertically Around Parent Center %\"")]
106 static void MirrorVerticallyParent()
107 {
108 MirrorVertically(true);
109 }
110
111 static void MirrorVertically(bool mirrorAnchors)
112 {
113 foreach (Transform transform in Selection.transforms)
114 {
115 RectTransform t = transform as RectTransform;
116 RectTransform pt = Selection.activeTransform.parent as RectTransform;
117
118 if (t == null || pt == null) return;
119
120 if (mirrorAnchors)
121 {
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);
125 }
126
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);
130
131 t.localScale = new Vector3(t.localScale.x, -t.localScale.y, t.localScale.z);
132 }
133 }
134 }
135}
Credit Erdener Gonenc - @PixelEnvision.