-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFindUtils.cs
More file actions
188 lines (176 loc) · 7.43 KB
/
FindUtils.cs
File metadata and controls
188 lines (176 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
/* Copyright (c) 2025 Petar Petrov (PeterSvP)
* https://pi-dev.com * https://store.steampowered.com/pub/pidev
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* ============= Description =============
* Utility extensions for finding transforms, components, and objects based on proximity.
* Includes support for tags, component types, and interface implementations.
* Also provides tools to fetch grandchild transforms and all objects of a type.
*
* ============= Usage =============
* transform.FindGrandChild("ChildName");
* Utils.GetClosestPoint(origin, listOfPoints);
* Utils.GetClosestNumber(originValue, floatValues);
* Utils.GetClosestObject(origin, objects); // GameObjects or Transforms
* Utils.GetClosestObjectWithTag(origin, "Enemy");
* Utils.GetAllComponents<UnityEngine.UI.Image>();
* Utils.GetClosestObjectWithComponent<MyComponent>(origin);
* Utils.GetClosestComponent<MyComponent>(origin);
* Utils.GetClosestComponent(origin, allowedList);
* Utils.GetClosestObjectImplementingInterface<IMyInterface>(origin, 10f, excludeList);
*/
namespace PiDev
{
public static partial class Utils
{
static public Transform FindGrandChild(this Transform fromGameObject, string withName, bool includeInactive = true)
{
Transform[] ts = fromGameObject.GetComponentsInChildren<Transform>(includeInactive);
foreach (Transform t in ts) if (t.gameObject.name == withName) return t;
return null;
}
public static Vector3 GetClosestPoint(Vector3 origin, IEnumerable<Vector3> points)
{
Vector3 tMin = Vector3.negativeInfinity;
float minDist = float.PositiveInfinity;
foreach (var p in points)
{
float dist = Vector3.Distance(p, origin);
if (dist < minDist)
{
tMin = p;
minDist = dist;
}
}
return tMin;
}
public static float GetClosestNumber(float origin, IEnumerable<float> points)
{
float tMin = float.NegativeInfinity;
float minDist = float.PositiveInfinity;
foreach (var p in points)
{
float dist = Mathf.Abs(p - origin);
if (dist < minDist)
{
tMin = p;
minDist = dist;
}
}
return tMin;
}
public static Transform GetClosestObject(Vector3 origin, IEnumerable<GameObject> objects, float maxDist = float.PositiveInfinity)
{
Transform tMin = null;
float minDist = float.PositiveInfinity;
Vector3 currentPos = origin;
foreach (GameObject t in objects)
{
float dist = Vector3.Distance(t.transform.position, currentPos);
if (dist > maxDist) continue;
if (dist < minDist)
{
tMin = t.transform;
minDist = dist;
}
}
return tMin;
}
public static Transform GetClosestObject(Vector3 origin, IEnumerable<Transform> objects)
{
Transform tMin = null;
float minDist = float.PositiveInfinity;
Vector3 currentPos = origin;
foreach (Transform t in objects)
{
float dist = Vector3.Distance(t.position, currentPos);
if (dist < minDist)
{
tMin = t;
minDist = dist;
}
}
return tMin;
}
public static Transform GetClosestObjectWithTag(Vector3 origin, string tag)
{
return GetClosestObject(origin, GameObject.FindGameObjectsWithTag(tag));
}
public static T[] GetAllComponents<T>() where T : UnityEngine.Object
{
return Resources.FindObjectsOfTypeAll<T>();
}
public static Transform GetClosestObjectWithComponent<T>(Vector3 origin) where T : Component
{
var objs = UnityEngine.Object.FindObjectsOfType<T>();
List<GameObject> gameobjects = new List<GameObject>();
foreach (var c in objs)
{
gameobjects.Add(c.gameObject);
}
return GetClosestObject(origin, gameobjects.ToArray());
}
public static T GetClosestComponent<T>(Vector3 origin) where T : Component
{
var objs = UnityEngine.Object.FindObjectsOfType<T>();
List<GameObject> gameobjects = new List<GameObject>();
foreach (var c in objs)
{
gameobjects.Add(c.gameObject);
}
var co = GetClosestObject(origin, gameobjects.ToArray());
if (co != null) return co.GetComponent<T>();
else return null;
}
public static T GetClosestComponent<T>(Vector3 origin, IEnumerable<T> allowedComponents) where T : Component
{
T closestComponent = null;
float closestDistance = float.MaxValue;
foreach (var component in allowedComponents)
{
if (component == null) continue;
float distance = Vector3.Distance(origin, component.transform.position);
if (distance < closestDistance)
{
closestDistance = distance;
closestComponent = component;
}
}
return closestComponent;
}
public static T GetClosestObjectImplementingInterface<T>(Vector3 origin, float maxDist, List<GameObject> exclude = null) where T : class
{
var ss = UnityEngine.Object.FindObjectsOfType<MonoBehaviour>().OfType<T>().ToList();
var objects = new List<GameObject>();
foreach (var c in ss)
{
GameObject go = (c as MonoBehaviour).gameObject;
if (exclude == null) objects.Add(go);
else if (!exclude.Contains(go)) objects.Add(go);
}
return GetClosestObject(origin, objects, maxDist)?.GetComponent<T>();
}
}
}