Tanoda
pb_GUIUtility.cs
Go to the documentation of this file.
1using UnityEngine;
2using UnityEngine.UI;
3
4namespace GILES.Interface
5{
9 public static class pb_GUIUtility
10 {
12 public static readonly Color PANEL_COLOR = new Color(.27f, .27f, .27f, .5f);
13
15 public static readonly Color ITEM_BACKGROUND_COLOR = new Color(.15f, .15f, .15f, 1f);
16
18 public const int PADDING = 4;
19
22 public static readonly RectOffset PADDING_RECT_OFFSET = new RectOffset(PADDING, PADDING, PADDING, PADDING);
23
26 private static Font _defaultFont;
27
31 public static Vector2 ScreenToGUIPoint(Vector2 v)
32 {
33 v.y = Screen.height - v.y;
34 return v;
35 }
36
40 public static Font DefaultFont()
41 {
42 if(_defaultFont == null)
43 _defaultFont = (Font) Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");
44
45 return _defaultFont;
46 }
47
51 public static Font GetFont(string fontName)
52 {
53 return Resources.Load<Font>("Required/Font/" + fontName);
54 }
55
59 public static GameObject CreateLabeledVerticalPanel(string label)
60 {
61 GameObject go = new GameObject();
62 go.name = label;
63#if UNITY_WEBGL
64 go.AddComponent<Image>().color = PANEL_COLOR;
65#endif
66 AddVerticalLayoutGroup(go);
67
68 CreateLabel( label ).transform.SetParent(go.transform);
69
70 return go;
71 }
72
76 public static GameObject CreateHorizontalGroup()
77 {
78 GameObject go = new GameObject();
79
80 HorizontalLayoutGroup group = go.AddComponent<HorizontalLayoutGroup>();
81 group.padding = new RectOffset(2,2,2,2);
82 group.childForceExpandWidth = true;
83 group.childForceExpandHeight = false;
84
85 return go;
86 }
87
91 public static GameObject CreateLabel(string text)
92 {
93 GameObject go = new GameObject();
94 go.name = "Label Field";
95 Text field = go.AddComponent<Text>();
96 string temp = text.Replace("UnityEngine.","");
97 field.text = temp; //This removes the UnityEngine. Prefix from the Inspector.
98 //field.text = text;
99 field.font = pb_GUIUtility.DefaultFont();
100 go.AddComponent<LayoutElement>().minHeight = 24;
101 field.alignment = TextAnchor.MiddleLeft;
102 return go;
103 }
104
108 public static void AddVerticalLayoutGroup(GameObject go)
109 {
110 AddVerticalLayoutGroup(
111 go,
112 pb_GUIUtility.PADDING_RECT_OFFSET,
113 pb_GUIUtility.PADDING,
114 true,
115 false);
116 }
117
121 public static void AddVerticalLayoutGroup(GameObject go, RectOffset padding, int spacing, bool childForceExpandWidth, bool childForceExpandHeight)
122 {
123 VerticalLayoutGroup group = go.AddComponent<VerticalLayoutGroup>();
124 group.padding = padding;
125 group.spacing = spacing;
126 group.childForceExpandWidth = childForceExpandWidth;
127 group.childForceExpandHeight = childForceExpandHeight;
128 }
129
133 public static Selectable GetNextSelectable(Selectable current)
134 {
135 if(current == null)
136 return null;
137
138 Selectable next = current.FindSelectableOnRight();
139
140 if(next != null)
141 {
142 return next;
143 }
144 else
145 {
146 next = current.FindSelectableOnDown();
147
148 if(next == null)
149 return null;
150
151 Selectable left = next;
152
153 while(next != null)
154 {
155 left = left.FindSelectableOnLeft();
156
157 if(left == null)
158 return next;
159
160 next = left;
161 }
162 }
163
164 return null;
165 }
166
170 public static Rect GetScreenRect(this RectTransform rectTransform)
171 {
172 Vector3[] world = new Vector3[4];
173 rectTransform.GetWorldCorners(world);
174 Vector2 min = Vector3.Min(Vector3.Min(Vector3.Min(world[0], world[1]), world[2]), world[3]);
175 Vector2 max = Vector3.Max(Vector3.Max(Vector3.Max(world[0], world[1]), world[2]), world[3]);
176 return new Rect(min.x, max.y, max.x - min.x, max.y - min.y);
177 }
178 }
179}
System.Drawing.Image Image
Definition: TestScript.cs:37
UnityEngine.Color Color
Definition: TestScript.cs:32