Skip to content

Commit c2fabb5

Browse files
committed
GameObject Extension Methods
1 parent 60eba3d commit c2fabb5

File tree

4 files changed

+94
-2
lines changed

4 files changed

+94
-2
lines changed

Colver/Colver.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
</Reference>
4545
</ItemGroup>
4646
<ItemGroup>
47+
<Compile Include="Main\GameObjectExtensions.cs" />
4748
<Compile Include="Main\Rigidbody2DExtensions.cs" />
4849
<Compile Include="Main\SpriteRendererExtension.cs" />
4950
<Compile Include="Main\Transform2DExtensions.cs" />
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
4+
namespace Colver.Main
5+
{
6+
public static class GameObjectExtensions
7+
{
8+
/// <summary>
9+
/// Finde the closest gameobject of the current one based on it's tag
10+
/// </summary>
11+
/// <typeparam name="T">Type of object to find</typeparam>
12+
/// <param name="obj">Object wich is searching</param>
13+
/// <param name="tag">Tag of the searched object</param>
14+
/// <param name="maxDistance">Max distance that will be searched</param>
15+
/// <returns>Returns a single GameObject or null</returns>
16+
public static T FindNearestByTag<T>(this GameObject obj, string tag, float maxDistance = float.PositiveInfinity) where T : MonoBehaviour
17+
{
18+
var objects = GameObject.FindGameObjectsWithTag(tag);
19+
20+
if (objects != null)
21+
{
22+
List<T> selectedObjects = new List<T>();
23+
foreach (var item in objects)
24+
{
25+
if (item.TryGetComponent<T>(out var component))
26+
selectedObjects.Add(component);
27+
}
28+
return FindNearests<T>(obj,selectedObjects, maxDistance);
29+
}
30+
return null;
31+
}
32+
33+
/// <summary>
34+
/// Finde the closest gameobject of the current one based on it's Type
35+
/// </summary>
36+
/// <typeparam name="T">Type of object to find</typeparam>
37+
/// <param name="obj">Object wich is searching</param>
38+
/// <param name="maxDistance">Max distance that will be searched</param>
39+
/// <returns>Returns a single GameObject or null</returns>
40+
public static T FindNearestByType<T>(this GameObject obj,float maxDistance = float.PositiveInfinity) where T : MonoBehaviour
41+
{
42+
var objects = GameObject.FindObjectsOfType<T>();
43+
if (objects != null)
44+
{
45+
List<T> selectedObjects = new List<T>();
46+
foreach (var item in objects)
47+
{
48+
if (item.TryGetComponent<T>(out var component))
49+
selectedObjects.Add(component);
50+
}
51+
return FindNearests<T>(obj, selectedObjects, maxDistance);
52+
}
53+
return null;
54+
}
55+
56+
/// <summary>
57+
/// Searchs on a list of GameObjects wich one it's closest to <see cref="obj"/>
58+
/// </summary>
59+
/// <typeparam name="T">Type of object to find</typeparam>
60+
/// <param name="obj">Object wich is searching</param>
61+
/// <param name="objects">Object list to be filtered</param>
62+
/// <param name="maxDistance">Max distance that will be searched</param>
63+
/// <returns>Returns the closests GameObject of obj or null</returns>
64+
public static T FindNearests<T>(this GameObject obj, List<T> objects, float maxDistance = float.PositiveInfinity) where T : MonoBehaviour
65+
{
66+
if (objects.Count == 0)
67+
return null;
68+
69+
T nearestObject = null;
70+
foreach (T item in objects)
71+
{
72+
var dist = Vector2.Distance(obj.transform.position, item.transform.position);
73+
74+
if (dist <= maxDistance)
75+
{
76+
if (nearestObject == null)
77+
{
78+
nearestObject = item;
79+
}
80+
else
81+
{
82+
var dist2 = Vector2.Distance(obj.transform.position, nearestObject.transform.position);
83+
nearestObject = dist < dist2 ? item : nearestObject;
84+
}
85+
}
86+
}
87+
return nearestObject;
88+
}
89+
}
90+
}

Colver/Main/Transform2DExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public static class Transform2DExtension
99
/// </summary>
1010
/// <param name="trans">The object wich will be rotated</param>
1111
/// <param name="target">The place where the object will be rotated</param>
12+
[System.Obsolete("May not work in some situations")]
1213
public static void LookToPosition(this Transform trans,Vector3 target)
1314
{
1415
trans.rotation = Quaternion.LookRotation(trans.position - target, Vector3.forward);

Colver/Main/Vector2Extensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ public static Vector2 ToAbsolute(this Vector2 vector2)
2727
/// <param name="vector2">The first Vector 2</param>
2828
/// <param name="obj">The second Vector 2</param>
2929
/// <returns>Returns true if the x and the y of the first and the second one are equals</returns>
30-
public static bool Equals(this Vector2 vector2, Vector2 obj) => vector2.ToAbsolute().x == obj.ToAbsolute().x && vector2.ToAbsolute().y == obj.ToAbsolute().y;
30+
public static bool AbsEquals(this Vector2 vector2, Vector2 obj) => vector2.ToAbsolute().x == obj.ToAbsolute().x && vector2.ToAbsolute().y == obj.ToAbsolute().y;
3131

3232
/// <summary>
3333
/// Verify if the first Vector is Greater than the second one
3434
/// </summary>
3535
/// <param name="vector2">First Vector2</param>
3636
/// <param name="obj">Second Vector2</param>
3737
/// <returns>Returns true if the x or the y of the first are grat than x and y of second one</returns>
38-
public static bool GreaterThan(this Vector2 vector2, Vector2 obj) => vector2.ToAbsolute().x > obj.ToAbsolute().x || vector2.ToAbsolute().y > obj.ToAbsolute().y;
38+
public static bool AbsGreaterThan(this Vector2 vector2, Vector2 obj) => vector2.ToAbsolute().x > obj.ToAbsolute().x || vector2.ToAbsolute().y > obj.ToAbsolute().y;
3939
}
4040

4141
}

0 commit comments

Comments
 (0)