Skip to content

Release 0.6.7 #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**:
Expand Down
70 changes: 70 additions & 0 deletions Runtime/UnityObjectsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

#nullable enable
// ReSharper disable once CheckNamespace

namespace GameLovers
{
/// <summary>
/// Extension methods for Unity objects.
/// </summary>
public static class UnityObjectsExtensions
{
/// <summary>
/// Get the corners of the <see cref="RectTransform"/> in the local space of its Transform.
/// </summary>
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;
}
Comment on lines +19 to +34
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add null parameter validation

The extension method should validate the transform parameter to avoid NullReferenceException.

public static Vector3[] GetLocalCornersArray(this RectTransform transform)
{
+   if (transform == null)
+   {
+       throw new System.ArgumentNullException(nameof(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;
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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;
}
public static Vector3[] GetLocalCornersArray(this RectTransform transform)
{
if (transform == null)
{
throw new System.ArgumentNullException(nameof(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;
}


/// <summary>
/// Get the corners of the <see cref="RectTransform"/> in world space of its Transform.
/// </summary>
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;
}
Comment on lines +39 to +49
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve variable naming and add null validation

The variable matrix4x could have a more descriptive name, and null validation should be added.

public static Vector3[] GetWorldCornersArray(this RectTransform transform)
{
+   if (transform == null)
+   {
+       throw new System.ArgumentNullException(nameof(transform));
+   }
    
    var corners = transform.GetLocalCornersArray();
-   var matrix4x = transform.localToWorldMatrix;
+   var worldMatrix = transform.localToWorldMatrix;
    for (int i = 0; i < 4; i++)
    {
-       corners[i] = matrix4x.MultiplyPoint(corners[i]);
+       corners[i] = worldMatrix.MultiplyPoint(corners[i]);
    }

    return corners;
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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;
}
public static Vector3[] GetWorldCornersArray(this RectTransform transform)
{
if (transform == null)
{
throw new System.ArgumentNullException(nameof(transform));
}
var corners = transform.GetLocalCornersArray();
var worldMatrix = transform.localToWorldMatrix;
for (int i = 0; i < 4; i++)
{
corners[i] = worldMatrix.MultiplyPoint(corners[i]);
}
return corners;
}


/// <summary>
/// Extension method for GraphicRaycaster that performs a raycast at the specified screen point.
/// Returns true if any object was hit.
/// </summary>
public static bool RaycastPoint(this GraphicRaycaster raycaster, Vector2 screenPoint, out List<RaycastResult> results)
{
var eventData = new PointerEventData(EventSystem.current)
{
position = screenPoint,
displayIndex = 0
};

results = new List<RaycastResult>();

raycaster.Raycast(eventData, results);

return results.Count > 0;
}
Comment on lines +55 to +68
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Check for null EventSystem.current

The method assumes EventSystem.current is not null, which might cause a NullReferenceException if no event system exists in the scene.

public static bool RaycastPoint(this GraphicRaycaster raycaster, Vector2 screenPoint, out List<RaycastResult> results)
{
+   if (raycaster == null)
+   {
+       throw new System.ArgumentNullException(nameof(raycaster));
+   }
+   
+   if (EventSystem.current == null)
+   {
+       results = new List<RaycastResult>();
+       return false;
+   }
    
    var eventData = new PointerEventData(EventSystem.current)
    {
        position = screenPoint,
        displayIndex = 0
    };

    results = new List<RaycastResult>();

    raycaster.Raycast(eventData, results);

    return results.Count > 0;
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public static bool RaycastPoint(this GraphicRaycaster raycaster, Vector2 screenPoint, out List<RaycastResult> results)
{
var eventData = new PointerEventData(EventSystem.current)
{
position = screenPoint,
displayIndex = 0
};
results = new List<RaycastResult>();
raycaster.Raycast(eventData, results);
return results.Count > 0;
}
public static bool RaycastPoint(this GraphicRaycaster raycaster, Vector2 screenPoint, out List<RaycastResult> results)
{
if (raycaster == null)
{
throw new System.ArgumentNullException(nameof(raycaster));
}
if (EventSystem.current == null)
{
results = new List<RaycastResult>();
return false;
}
var eventData = new PointerEventData(EventSystem.current)
{
position = screenPoint,
displayIndex = 0
};
results = new List<RaycastResult>();
raycaster.Raycast(eventData, results);
return results.Count > 0;
}

}
}
11 changes: 11 additions & 0 deletions Runtime/UnityObjectsExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading