|
| 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 | +} |
0 commit comments