Skip to content

Commit 03acac6

Browse files
committed
**New**:
- Added the *UnityObjectExtensions* to help add extra logic to Unity's *GameObject* type objects
1 parent eabc82b commit 03acac6

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this package will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.6.7] - 2025-04-07
8+
9+
**New**:
10+
- Added the *UnityObjectExtensions* to help add extra logic to Unity's *GameObject* type objects
11+
712
## [0.6.6] - 2024-11-30
813

914
**Fix**:

Runtime/UnityObjectsExtensions.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
using UnityEngine.EventSystems;
4+
using UnityEngine.UI;
5+
6+
#nullable enable
7+
// ReSharper disable once CheckNamespace
8+
9+
namespace GameLovers
10+
{
11+
/// <summary>
12+
/// Extension methods for Unity objects.
13+
/// </summary>
14+
public static class UnityObjectsExtensions
15+
{
16+
/// <summary>
17+
/// Get the corners of the <see cref="RectTransform"/> in the local space of its Transform.
18+
/// </summary>
19+
public static Vector3[] GetLocalCornersArray(this RectTransform transform)
20+
{
21+
var corners = new Vector3[4];
22+
var rect = transform.rect;
23+
var x = rect.x;
24+
var y = rect.y;
25+
var xMax = rect.xMax;
26+
var yMax = rect.yMax;
27+
28+
corners[0] = new Vector3(x, y, 0f);
29+
corners[1] = new Vector3(x, yMax, 0f);
30+
corners[2] = new Vector3(xMax, yMax, 0f);
31+
corners[3] = new Vector3(xMax, y, 0f);
32+
33+
return corners;
34+
}
35+
36+
/// <summary>
37+
/// Get the corners of the <see cref="RectTransform"/> in world space of its Transform.
38+
/// </summary>
39+
public static Vector3[] GetWorldCornersArray(this RectTransform transform)
40+
{
41+
var corners = transform.GetLocalCornersArray();
42+
var matrix4x = transform.localToWorldMatrix;
43+
for (int i = 0; i < 4; i++)
44+
{
45+
corners[i] = matrix4x.MultiplyPoint(corners[i]);
46+
}
47+
48+
return corners;
49+
}
50+
51+
/// <summary>
52+
/// Extension method for GraphicRaycaster that performs a raycast at the specified screen point.
53+
/// Returns true if any object was hit.
54+
/// </summary>
55+
public static bool RaycastPoint(this GraphicRaycaster raycaster, Vector2 screenPoint, out List<RaycastResult> results)
56+
{
57+
var eventData = new PointerEventData(EventSystem.current)
58+
{
59+
position = screenPoint,
60+
displayIndex = 0
61+
};
62+
63+
results = new List<RaycastResult>();
64+
65+
raycaster.Raycast(eventData, results);
66+
67+
return results.Count > 0;
68+
}
69+
}
70+
}

Runtime/UnityObjectsExtensions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "com.gamelovers.dataextensions",
33
"displayName": "Unity Data Type Extensions",
44
"author": "Miguel Tomas",
5-
"version": "0.6.6",
5+
"version": "0.6.7",
66
"unity": "2022.3",
77
"license": "MIT",
88
"description": "This package extends various sets of data types to be used in any type of data containers or persistent serializable data",

0 commit comments

Comments
 (0)