@@ -40,20 +40,27 @@ private static GameObject CreateUIElementRoot(string name, Vector2 size)
40
40
GameObject root ;
41
41
42
42
#if UNITY_EDITOR
43
- root = ObjectFactory . CreateGameObject ( name ) ;
43
+ root = ObjectFactory . CreateGameObject ( name , typeof ( RectTransform ) ) ;
44
+ var rt = root . GetComponent < RectTransform > ( ) ;
45
+ rt . sizeDelta = size ;
44
46
#else
45
47
root = new GameObject ( name ) ;
46
- #endif
47
-
48
48
RectTransform rectTransform = root . AddComponent < RectTransform > ( ) ;
49
49
rectTransform . sizeDelta = size ;
50
+ #endif
51
+
50
52
return root ;
51
53
}
52
54
53
55
static GameObject CreateUIObject ( string name , GameObject parent )
54
56
{
55
- GameObject go = new GameObject ( name ) ;
57
+ GameObject go ;
58
+ #if UNITY_EDITOR
59
+ go = ObjectFactory . CreateGameObject ( name , typeof ( RectTransform ) ) ;
60
+ #else
61
+ go = new GameObject ( name ) ;
56
62
go . AddComponent < RectTransform > ( ) ;
63
+ #endif
57
64
SetParentAndAlign ( go , parent ) ;
58
65
return go ;
59
66
}
@@ -102,12 +109,12 @@ public static GameObject CreateScrollbar(Resources resources)
102
109
GameObject sliderArea = CreateUIObject ( "Sliding Area" , scrollbarRoot ) ;
103
110
GameObject handle = CreateUIObject ( "Handle" , sliderArea ) ;
104
111
105
- Image bgImage = scrollbarRoot . AddComponent < Image > ( ) ;
112
+ Image bgImage = AddComponent < Image > ( scrollbarRoot ) ;
106
113
bgImage . sprite = resources . background ;
107
114
bgImage . type = Image . Type . Sliced ;
108
115
bgImage . color = s_DefaultSelectableColor ;
109
116
110
- Image handleImage = handle . AddComponent < Image > ( ) ;
117
+ Image handleImage = AddComponent < Image > ( handle ) ;
111
118
handleImage . sprite = resources . standard ;
112
119
handleImage . type = Image . Type . Sliced ;
113
120
handleImage . color = s_DefaultSelectableColor ;
@@ -120,7 +127,7 @@ public static GameObject CreateScrollbar(Resources resources)
120
127
RectTransform handleRect = handle . GetComponent < RectTransform > ( ) ;
121
128
handleRect . sizeDelta = new Vector2 ( 20 , 20 ) ;
122
129
123
- Scrollbar scrollbar = scrollbarRoot . AddComponent < Scrollbar > ( ) ;
130
+ Scrollbar scrollbar = AddComponent < Scrollbar > ( scrollbarRoot ) ;
124
131
scrollbar . handleRect = handleRect ;
125
132
scrollbar . targetGraphic = handleImage ;
126
133
SetDefaultColorTransitionValues ( scrollbar ) ;
@@ -132,19 +139,25 @@ public static GameObject CreateButton(Resources resources)
132
139
{
133
140
GameObject buttonRoot = CreateUIElementRoot ( "Button" , s_ThickElementSize ) ;
134
141
135
- GameObject childText = new GameObject ( "Text (TMP)" ) ;
142
+ GameObject childText ;
143
+ #if UNITY_EDITOR
144
+ childText = ObjectFactory . CreateGameObject ( "Text (TMP)" , typeof ( RectTransform ) ) ;
145
+ #else
146
+ childText = new GameObject ( "Text (TMP)" ) ;
136
147
childText . AddComponent < RectTransform > ( ) ;
148
+ #endif
149
+
137
150
SetParentAndAlign ( childText , buttonRoot ) ;
138
151
139
- Image image = buttonRoot . AddComponent < Image > ( ) ;
152
+ Image image = AddComponent < Image > ( buttonRoot ) ;
140
153
image . sprite = resources . standard ;
141
154
image . type = Image . Type . Sliced ;
142
155
image . color = s_DefaultSelectableColor ;
143
156
144
- Button bt = buttonRoot . AddComponent < Button > ( ) ;
157
+ Button bt = AddComponent < Button > ( buttonRoot ) ;
145
158
SetDefaultColorTransitionValues ( bt ) ;
146
159
147
- TextMeshProUGUI text = childText . AddComponent < TextMeshProUGUI > ( ) ;
160
+ TextMeshProUGUI text = AddComponent < TextMeshProUGUI > ( childText ) ;
148
161
text . text = "Button" ;
149
162
text . alignment = TextAlignmentOptions . Center ;
150
163
SetDefaultTextValues ( text ) ;
@@ -162,8 +175,7 @@ public static GameObject CreateText(Resources resources)
162
175
GameObject go = null ;
163
176
164
177
#if UNITY_EDITOR
165
- go = ObjectFactory . CreateGameObject ( "Text (TMP)" ) ;
166
- ObjectFactory . AddComponent < TextMeshProUGUI > ( go ) ;
178
+ go = ObjectFactory . CreateGameObject ( "Text (TMP)" , typeof ( TextMeshProUGUI ) ) ;
167
179
#else
168
180
go = CreateUIElementRoot ( "Text (TMP)" , s_TextElementSize ) ;
169
181
go . AddComponent < TextMeshProUGUI > ( ) ;
@@ -172,7 +184,6 @@ public static GameObject CreateText(Resources resources)
172
184
return go ;
173
185
}
174
186
175
-
176
187
public static GameObject CreateInputField ( Resources resources )
177
188
{
178
189
GameObject root = CreateUIElementRoot ( "InputField (TMP)" , s_ThickElementSize ) ;
@@ -181,15 +192,15 @@ public static GameObject CreateInputField(Resources resources)
181
192
GameObject childPlaceholder = CreateUIObject ( "Placeholder" , textArea ) ;
182
193
GameObject childText = CreateUIObject ( "Text" , textArea ) ;
183
194
184
- Image image = root . AddComponent < Image > ( ) ;
195
+ Image image = AddComponent < Image > ( root ) ;
185
196
image . sprite = resources . inputField ;
186
197
image . type = Image . Type . Sliced ;
187
198
image . color = s_DefaultSelectableColor ;
188
199
189
- TMP_InputField inputField = root . AddComponent < TMP_InputField > ( ) ;
200
+ TMP_InputField inputField = AddComponent < TMP_InputField > ( root ) ;
190
201
SetDefaultColorTransitionValues ( inputField ) ;
191
202
192
- RectMask2D rectMask = textArea . AddComponent < RectMask2D > ( ) ;
203
+ RectMask2D rectMask = AddComponent < RectMask2D > ( textArea ) ;
193
204
rectMask . padding = new Vector4 ( - 8 , - 5 , - 8 , - 5 ) ;
194
205
195
206
RectTransform textAreaRectTransform = textArea . GetComponent < RectTransform > ( ) ;
@@ -200,14 +211,14 @@ public static GameObject CreateInputField(Resources resources)
200
211
textAreaRectTransform . offsetMax = new Vector2 ( - 10 , - 7 ) ;
201
212
202
213
203
- TextMeshProUGUI text = childText . AddComponent < TextMeshProUGUI > ( ) ;
214
+ TextMeshProUGUI text = AddComponent < TextMeshProUGUI > ( childText ) ;
204
215
text . text = "" ;
205
216
text . textWrappingMode = TextWrappingModes . NoWrap ;
206
217
text . extraPadding = true ;
207
218
text . richText = true ;
208
219
SetDefaultTextValues ( text ) ;
209
220
210
- TextMeshProUGUI placeholder = childPlaceholder . AddComponent < TextMeshProUGUI > ( ) ;
221
+ TextMeshProUGUI placeholder = AddComponent < TextMeshProUGUI > ( childPlaceholder ) ;
211
222
placeholder . text = "Enter text..." ;
212
223
placeholder . fontSize = 14 ;
213
224
placeholder . fontStyle = FontStyles . Italic ;
@@ -220,7 +231,7 @@ public static GameObject CreateInputField(Resources resources)
220
231
placeholder . color = placeholderColor ;
221
232
222
233
// Add Layout component to placeholder.
223
- placeholder . gameObject . AddComponent < LayoutElement > ( ) . ignoreLayout = true ;
234
+ AddComponent < LayoutElement > ( placeholder . gameObject ) . ignoreLayout = true ;
224
235
225
236
RectTransform textRectTransform = childText . GetComponent < RectTransform > ( ) ;
226
237
textRectTransform . anchorMin = Vector2 . zero ;
@@ -275,28 +286,28 @@ public static GameObject CreateDropdown(Resources resources)
275
286
276
287
// Setup item UI components.
277
288
278
- TextMeshProUGUI itemLabelText = itemLabel . AddComponent < TextMeshProUGUI > ( ) ;
289
+ TextMeshProUGUI itemLabelText = AddComponent < TextMeshProUGUI > ( itemLabel ) ;
279
290
SetDefaultTextValues ( itemLabelText ) ;
280
291
itemLabelText . alignment = TextAlignmentOptions . Left ;
281
292
282
- Image itemBackgroundImage = itemBackground . AddComponent < Image > ( ) ;
293
+ Image itemBackgroundImage = AddComponent < Image > ( itemBackground ) ;
283
294
itemBackgroundImage . color = new Color32 ( 245 , 245 , 245 , 255 ) ;
284
295
285
- Image itemCheckmarkImage = itemCheckmark . AddComponent < Image > ( ) ;
296
+ Image itemCheckmarkImage = AddComponent < Image > ( itemCheckmark ) ;
286
297
itemCheckmarkImage . sprite = resources . checkmark ;
287
298
288
- Toggle itemToggle = item . AddComponent < Toggle > ( ) ;
299
+ Toggle itemToggle = AddComponent < Toggle > ( item ) ;
289
300
itemToggle . targetGraphic = itemBackgroundImage ;
290
301
itemToggle . graphic = itemCheckmarkImage ;
291
302
itemToggle . isOn = true ;
292
303
293
304
// Setup template UI components.
294
305
295
- Image templateImage = template . AddComponent < Image > ( ) ;
306
+ Image templateImage = AddComponent < Image > ( template ) ;
296
307
templateImage . sprite = resources . standard ;
297
308
templateImage . type = Image . Type . Sliced ;
298
309
299
- ScrollRect templateScrollRect = template . AddComponent < ScrollRect > ( ) ;
310
+ ScrollRect templateScrollRect = AddComponent < ScrollRect > ( template ) ;
300
311
templateScrollRect . content = ( RectTransform ) content . transform ;
301
312
templateScrollRect . viewport = ( RectTransform ) viewport . transform ;
302
313
templateScrollRect . horizontal = false ;
@@ -305,28 +316,28 @@ public static GameObject CreateDropdown(Resources resources)
305
316
templateScrollRect . verticalScrollbarVisibility = ScrollRect . ScrollbarVisibility . AutoHideAndExpandViewport ;
306
317
templateScrollRect . verticalScrollbarSpacing = - 3 ;
307
318
308
- Mask scrollRectMask = viewport . AddComponent < Mask > ( ) ;
319
+ Mask scrollRectMask = AddComponent < Mask > ( viewport ) ;
309
320
scrollRectMask . showMaskGraphic = false ;
310
321
311
- Image viewportImage = viewport . AddComponent < Image > ( ) ;
322
+ Image viewportImage = AddComponent < Image > ( viewport ) ;
312
323
viewportImage . sprite = resources . mask ;
313
324
viewportImage . type = Image . Type . Sliced ;
314
325
315
326
// Setup dropdown UI components.
316
327
317
- TextMeshProUGUI labelText = label . AddComponent < TextMeshProUGUI > ( ) ;
328
+ TextMeshProUGUI labelText = AddComponent < TextMeshProUGUI > ( label ) ;
318
329
SetDefaultTextValues ( labelText ) ;
319
330
labelText . alignment = TextAlignmentOptions . Left ;
320
331
321
- Image arrowImage = arrow . AddComponent < Image > ( ) ;
332
+ Image arrowImage = AddComponent < Image > ( arrow ) ;
322
333
arrowImage . sprite = resources . dropdown ;
323
334
324
- Image backgroundImage = root . AddComponent < Image > ( ) ;
335
+ Image backgroundImage = AddComponent < Image > ( root ) ;
325
336
backgroundImage . sprite = resources . standard ;
326
337
backgroundImage . color = s_DefaultSelectableColor ;
327
338
backgroundImage . type = Image . Type . Sliced ;
328
339
329
- TMP_Dropdown dropdown = root . AddComponent < TMP_Dropdown > ( ) ;
340
+ TMP_Dropdown dropdown = AddComponent < TMP_Dropdown > ( root ) ;
330
341
dropdown . targetGraphic = backgroundImage ;
331
342
SetDefaultColorTransitionValues ( dropdown ) ;
332
343
dropdown . template = template . GetComponent < RectTransform > ( ) ;
@@ -400,5 +411,14 @@ public static GameObject CreateDropdown(Resources resources)
400
411
401
412
return root ;
402
413
}
414
+
415
+ private static T AddComponent < T > ( GameObject go ) where T : Component
416
+ {
417
+ #if UNITY_EDITOR
418
+ return ObjectFactory . AddComponent < T > ( go ) ;
419
+ #else
420
+ return go . AddComponent < T > ( ) ;
421
+ #endif
422
+ }
403
423
}
404
424
}
0 commit comments