Skip to content

Commit 7f757b7

Browse files
committed
feat(Util): Add ui get bounds
1 parent 556f2ea commit 7f757b7

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Assets/JCSUnity/Scripts/Util/JCS_UIUtil.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,36 @@ public static bool IsUnityDefinedUI(Component comp)
5252
comp.GetComponent<TMP_InputField>());
5353
}
5454

55+
/// <summary>
56+
/// Return the bounds of the UI element.
57+
/// </summary>
58+
public static Bounds GetBounds(Transform transform)
59+
{
60+
var min = Vector3.positiveInfinity;
61+
var max = Vector3.negativeInfinity;
62+
63+
RectTransform[] rts = transform.GetComponentsInChildren<RectTransform>();
64+
65+
foreach (RectTransform rt in rts)
66+
{
67+
// Get the 4 corners in world coordinates
68+
var v = new Vector3[4];
69+
rt.GetWorldCorners(v);
70+
71+
// update min and max
72+
foreach (var vector3 in v)
73+
{
74+
min = Vector3.Min(min, vector3);
75+
max = Vector3.Max(max, vector3);
76+
}
77+
}
78+
79+
var bounds = new Bounds();
80+
bounds.SetMinMax(min, max);
81+
82+
return bounds;
83+
}
84+
5585
#region EVENT
5686

5787
/// <summary>

docs/ScriptReference/Util/JCS_UIUtil.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ User interface related utilities functions.
77
| Name | Description |
88
|:---------------------------------|:--------------------------------------------------------------------------|
99
| IsUnityDefinedUI | Return true if GameObject contains built-in UI components. |
10+
| GetBounds | Return the bounds of the UI element. |
1011
| AddEventTriggerEvent | Add an event to EventTrigger component from Unity API. |
1112
| SetLangText | Set text by system language and language data. |
1213
| SetText | Set the text with data. |

0 commit comments

Comments
 (0)