1- using System . Reflection ;
1+ using System . ComponentModel ;
2+ using System . Reflection ;
23using UnityEngine . UI ;
4+ using Component = UnityEngine . Component ;
35
46namespace JFUtils ;
57
@@ -13,51 +15,17 @@ public static string GetPrefabName(this GameObject gameObject)
1315 return prefabName ;
1416 }
1517
16- public static string GetPrefabName < T > ( this T gameObject ) where T : MonoBehaviour
18+ public static string GetPrefabName < T > ( this T gameObject ) where T : Component
1719 {
1820 var prefabName = Utils . GetPrefabName ( gameObject . gameObject ) ;
1921 for ( var i = 0 ; i < 80 ; i ++ ) prefabName = prefabName . Replace ( $ " ({ i } )", "" ) ;
2022
2123 return prefabName ;
2224 }
2325
24- /// <summary>
25- /// Extends GameObject with a shortcut for the Unity bool operator override.
26- /// </summary>
27- /// <summary>
28- /// Facilitates use of null propagation operator for unity GameObjects by respecting op_equality.
29- /// </summary>
30- /// <param name="this"> this </param>
31- /// <returns>Returns null when GameObject.op_equality returns false.</returns>
32- public static GameObject OrNull ( this GameObject @this ) { return @this ? @this : null ; }
26+ public static T GetOrAddComponent < T > ( this GameObject gameObject ) where T : Component =>
27+ gameObject . GetComponent < T > ( ) ?? gameObject . AddComponent < T > ( ) ;
3328
34- /// <summary>
35- /// Facilitates use of null propagation operator for unity MonBehaviours by respecting op_equality.
36- /// </summary>
37- /// <typeparam name="T">Any type that inherits MonoBehaviour</typeparam>
38- /// <param name="this">this</param>
39- /// <returns>Returns null when MonoBehaviours.op_equality returns false.</returns>
40- public static T OrNull < T > ( this T @this ) where T : Object { return @this ? @this : null ; }
41-
42- /// <summary>
43- /// Returns the component of Type type. If one doesn't already exist on the GameObject it will be added.
44- /// </summary>
45- /// <remarks>Source: https://wiki.unity3d.com/index.php/GetOrAddComponent</remarks>
46- /// <typeparam name="T">The type of Component to return.</typeparam>
47- /// <param name="gameObject">The GameObject this Component is attached to.</param>
48- /// <returns>Component</returns>
49- public static T GetOrAddComponent < T > ( this GameObject gameObject ) where T : Component
50- {
51- return gameObject . GetComponent < T > ( ) ?? gameObject . AddComponent < T > ( ) ;
52- }
53-
54- /// <summary>
55- /// Adds a new copy of the provided component to a gameObject
56- /// </summary>
57- /// <param name="gameObject"></param>
58- /// <param name="duplicate"></param>
59- /// <typeparam name="T"></typeparam>
60- /// <returns></returns>
6129 public static Component AddComponentCopy < T > ( this GameObject gameObject , T duplicate ) where T : Component
6230 {
6331 var target = gameObject . AddComponent ( duplicate . GetType ( ) ) ;
@@ -80,143 +48,4 @@ public static Component AddComponentCopy<T>(this GameObject gameObject, T duplic
8048
8149 return target ;
8250 }
83-
84- internal static GameObject SetToTextHeight ( this GameObject go )
85- {
86- go . GetComponent < RectTransform > ( ) . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical ,
87- go . GetComponentInChildren < Text > ( ) . preferredHeight + 3f ) ;
88- return go ;
89- }
90-
91- internal static GameObject SetUpperLeft ( this GameObject go )
92- {
93- var rect = go . GetComponent < RectTransform > ( ) ;
94- rect . anchorMax = new Vector2 ( 0 , 1 ) ;
95- rect . anchorMin = new Vector2 ( 0 , 1 ) ;
96- rect . pivot = new Vector2 ( 0 , 1 ) ;
97- rect . anchoredPosition = new Vector2 ( 0 , 0 ) ;
98- return go ;
99- }
100-
101- internal static GameObject SetMiddleLeft ( this GameObject go )
102- {
103- var rect = go . GetComponent < RectTransform > ( ) ;
104- rect . anchorMax = new Vector2 ( 0 , 0.5f ) ;
105- rect . anchorMin = new Vector2 ( 0 , 0.5f ) ;
106- rect . pivot = new Vector2 ( 0 , 0.5f ) ;
107- rect . anchoredPosition = new Vector2 ( 0 , 0f ) ;
108- return go ;
109- }
110-
111- internal static GameObject SetBottomLeft ( this GameObject go )
112- {
113- var rect = go . GetComponent < RectTransform > ( ) ;
114- rect . anchorMax = new Vector2 ( 0 , 0 ) ;
115- rect . anchorMin = new Vector2 ( 0 , 0 ) ;
116- rect . pivot = new Vector2 ( 0 , 0 ) ;
117- rect . anchoredPosition = new Vector2 ( 0 , 0f ) ;
118- return go ;
119- }
120-
121- internal static GameObject SetUpperRight ( this GameObject go )
122- {
123- var rect = go . GetComponent < RectTransform > ( ) ;
124- rect . anchorMax = new Vector2 ( 1 , 1 ) ;
125- rect . anchorMin = new Vector2 ( 1 , 1 ) ;
126- rect . pivot = new Vector2 ( 1 , 1 ) ;
127- rect . anchoredPosition = new Vector2 ( 0 , 0 ) ;
128- return go ;
129- }
130-
131- internal static GameObject SetMiddleRight ( this GameObject go )
132- {
133- var rect = go . GetComponent < RectTransform > ( ) ;
134- rect . anchorMax = new Vector2 ( 1 , 0.5f ) ;
135- rect . anchorMin = new Vector2 ( 1 , 0.5f ) ;
136- rect . pivot = new Vector2 ( 1 , 0.5f ) ;
137- rect . anchoredPosition = new Vector2 ( 0 , 0f ) ;
138- return go ;
139- }
140-
141- internal static GameObject SetBottomRight ( this GameObject go )
142- {
143- var rect = go . GetComponent < RectTransform > ( ) ;
144- rect . anchorMax = new Vector2 ( 1 , 0 ) ;
145- rect . anchorMin = new Vector2 ( 1 , 0 ) ;
146- rect . pivot = new Vector2 ( 1f , 0f ) ;
147- rect . anchoredPosition = new Vector2 ( 0 , 0 ) ;
148- return go ;
149- }
150-
151- internal static GameObject SetUpperCenter ( this GameObject go )
152- {
153- var rect = go . GetComponent < RectTransform > ( ) ;
154- rect . anchorMax = new Vector2 ( 0.5f , 1f ) ;
155- rect . anchorMin = new Vector2 ( 0.5f , 1f ) ;
156- rect . pivot = new Vector2 ( 0.5f , 1f ) ;
157- rect . anchoredPosition = new Vector2 ( 0 , 0 ) ;
158- return go ;
159- }
160-
161- internal static GameObject SetMiddleCenter ( this GameObject go )
162- {
163- var rect = go . GetComponent < RectTransform > ( ) ;
164- rect . anchorMax = new Vector2 ( 0.5f , 0.5f ) ;
165- rect . anchorMin = new Vector2 ( 0.5f , 0.5f ) ;
166- rect . pivot = new Vector2 ( 0.5f , 0.5f ) ;
167- rect . anchoredPosition = new Vector2 ( 0 , 0 ) ;
168- return go ;
169- }
170-
171- internal static GameObject SetBottomCenter ( this GameObject go )
172- {
173- var rect = go . GetComponent < RectTransform > ( ) ;
174- rect . anchorMax = new Vector2 ( 0.5f , 0 ) ;
175- rect . anchorMin = new Vector2 ( 0.5f , 0 ) ;
176- rect . pivot = new Vector2 ( 0.5f , 0f ) ;
177- rect . anchoredPosition = new Vector2 ( 0 , 0 ) ;
178- return go ;
179- }
180-
181- internal static GameObject SetSize ( this GameObject go , float width , float height )
182- {
183- var rect = go . GetComponent < RectTransform > ( ) ;
184- rect . SetSizeWithCurrentAnchors ( RectTransform . Axis . Horizontal , width ) ;
185- rect . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical , height ) ;
186- return go ;
187- }
188-
189- internal static GameObject SetWidth ( this GameObject go , float width )
190- {
191- var rect = go . GetComponent < RectTransform > ( ) ;
192- rect . SetSizeWithCurrentAnchors ( RectTransform . Axis . Horizontal , width ) ;
193- return go ;
194- }
195-
196- internal static GameObject SetHeight ( this GameObject go , float height )
197- {
198- var rect = go . GetComponent < RectTransform > ( ) ;
199- rect . SetSizeWithCurrentAnchors ( RectTransform . Axis . Vertical , height ) ;
200- return go ;
201- }
202-
203- internal static float GetWidth ( this GameObject go )
204- {
205- var rect = go . GetComponent < RectTransform > ( ) ;
206- return rect . rect . width ;
207- }
208-
209- internal static float GetHeight ( this GameObject go )
210- {
211- var rect = go . GetComponent < RectTransform > ( ) ;
212- return rect . rect . height ;
213- }
214-
215- internal static float GetTextHeight ( this GameObject go ) { return go . GetComponent < Text > ( ) . preferredHeight ; }
216-
217- internal static GameObject SetText ( this GameObject go , string text )
218- {
219- go . GetComponent < Text > ( ) . text = text ;
220- return go ;
221- }
22251}
0 commit comments