From 03acac669a44d487f4322204ca91eb8f4041ed46 Mon Sep 17 00:00:00 2001 From: CoderGamester Date: Mon, 7 Apr 2025 22:46:08 +0300 Subject: [PATCH] **New**: - Added the *UnityObjectExtensions* to help add extra logic to Unity's *GameObject* type objects --- CHANGELOG.md | 5 ++ Runtime/UnityObjectsExtensions.cs | 70 ++++++++++++++++++++++++++ Runtime/UnityObjectsExtensions.cs.meta | 11 ++++ package.json | 2 +- 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 Runtime/UnityObjectsExtensions.cs create mode 100644 Runtime/UnityObjectsExtensions.cs.meta diff --git a/CHANGELOG.md b/CHANGELOG.md index 3912723..10b06e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this package will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [0.6.7] - 2025-04-07 + +**New**: +- Added the *UnityObjectExtensions* to help add extra logic to Unity's *GameObject* type objects + ## [0.6.6] - 2024-11-30 **Fix**: diff --git a/Runtime/UnityObjectsExtensions.cs b/Runtime/UnityObjectsExtensions.cs new file mode 100644 index 0000000..655be0b --- /dev/null +++ b/Runtime/UnityObjectsExtensions.cs @@ -0,0 +1,70 @@ +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.UI; + +#nullable enable +// ReSharper disable once CheckNamespace + +namespace GameLovers +{ + /// + /// Extension methods for Unity objects. + /// + public static class UnityObjectsExtensions + { + /// + /// Get the corners of the in the local space of its Transform. + /// + public static Vector3[] GetLocalCornersArray(this RectTransform transform) + { + var corners = new Vector3[4]; + var rect = transform.rect; + var x = rect.x; + var y = rect.y; + var xMax = rect.xMax; + var yMax = rect.yMax; + + corners[0] = new Vector3(x, y, 0f); + corners[1] = new Vector3(x, yMax, 0f); + corners[2] = new Vector3(xMax, yMax, 0f); + corners[3] = new Vector3(xMax, y, 0f); + + return corners; + } + + /// + /// Get the corners of the in world space of its Transform. + /// + public static Vector3[] GetWorldCornersArray(this RectTransform transform) + { + var corners = transform.GetLocalCornersArray(); + var matrix4x = transform.localToWorldMatrix; + for (int i = 0; i < 4; i++) + { + corners[i] = matrix4x.MultiplyPoint(corners[i]); + } + + return corners; + } + + /// + /// Extension method for GraphicRaycaster that performs a raycast at the specified screen point. + /// Returns true if any object was hit. + /// + public static bool RaycastPoint(this GraphicRaycaster raycaster, Vector2 screenPoint, out List results) + { + var eventData = new PointerEventData(EventSystem.current) + { + position = screenPoint, + displayIndex = 0 + }; + + results = new List(); + + raycaster.Raycast(eventData, results); + + return results.Count > 0; + } + } +} diff --git a/Runtime/UnityObjectsExtensions.cs.meta b/Runtime/UnityObjectsExtensions.cs.meta new file mode 100644 index 0000000..9d2fb43 --- /dev/null +++ b/Runtime/UnityObjectsExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a5618a86d50247c4cb5db3f21eb0140f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/package.json b/package.json index 8403bf3..13de709 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "com.gamelovers.dataextensions", "displayName": "Unity Data Type Extensions", "author": "Miguel Tomas", - "version": "0.6.6", + "version": "0.6.7", "unity": "2022.3", "license": "MIT", "description": "This package extends various sets of data types to be used in any type of data containers or persistent serializable data",