Files
trail-into-darkness/Packages/com.jovian.utilities/Runtime/GameObjectUtilities.cs
Sebastian Bularca 36d3f112ef added zlinq
2026-04-02 07:43:33 +02:00

60 lines
2.0 KiB
C#

using System;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Jovian.Utilities {
public static class GameObjectUtilities {
public static void DestroyGameObjectsOfType<T>() where T : MonoBehaviour {
var objects = Object.FindObjectsByType<T>(FindObjectsInactive.Include, FindObjectsSortMode.None);
foreach(var t in objects) {
Object.Destroy(t.gameObject);
}
}
public static bool IsGameObjectAChildOfGameObject(this GameObject targetGameObject, GameObject potentialParentGameObject) {
if(!targetGameObject) {
return false;
}
var potentialParentTransform = potentialParentGameObject.transform;
var checkTransform = targetGameObject.transform;
do {
if(checkTransform == potentialParentTransform) {
return true;
}
checkTransform = checkTransform.parent;
} while(checkTransform);
return false;
}
public static bool TryFindChildByName(this GameObject gameObject,
string name,
out GameObject childGameObject,
bool includeInactive = false,
bool allowPartialMatch = false,
StringComparison stringComparison = StringComparison.Ordinal) {
childGameObject = null;
if(!gameObject) {
return false;
}
var children = gameObject.GetComponentsInChildren<Transform>(includeInactive);
foreach(var child in children) {
if(allowPartialMatch && child.name.Contains(name, stringComparison)) {
childGameObject = child.gameObject;
return true;
}
if(!allowPartialMatch && child.name.Equals(name, stringComparison)) {
childGameObject = child.gameObject;
return true;
}
}
return false;
}
}
}