-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTransformUtils.cs
More file actions
33 lines (29 loc) · 1.05 KB
/
TransformUtils.cs
File metadata and controls
33 lines (29 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using UnityEngine;
/* Public Domain - 2025 Petar Petrov (PeterSvP)
* https://pi-dev.com * https://store.steampowered.com/pub/pidev
*
* ============= Description =============
* Converts a RectTransform to a screen-space Rect based on world position and scale.
* Useful for hit testing, UI interaction zones, or screen alignment calculations.
*
* ============= Usage =============
* Rect screenRect = Utils.RectTransformToScreenSpace(myRectTransform);
*/
namespace PiDev
{
public static partial class Utils
{
public static Rect RectTransformToScreenSpace(RectTransform transform)
{
Vector2 size = Vector2.Scale(transform.rect.size, transform.lossyScale);
return new Rect((Vector2)transform.position - (size * 0.5f), size);
}
public static Rect GetWorldSpaceRect(this RectTransform rt)
{
var r = rt.rect;
r.center = rt.TransformPoint(r.center);
r.size = rt.TransformVector(r.size);
return r;
}
}
}