Files
trail-into-darkness/Packages/com.jovian.utilties/Runtime/GameObjectUtilities.cs

59 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 {
T[] objects = Object.FindObjectsOfType<T>();
for (int i = 0; i < objects.Length; ++i) {
Object.Destroy(objects[i].gameObject);
}
}
public static bool IsGameObjectAChildOfGameObject(this GameObject targetGameObject, GameObject potentialParentGameObject) {
if (targetGameObject == null) {
return false;
}
Transform potentialParentTransform = potentialParentGameObject.transform;
Transform checkTransform = targetGameObject.transform;
do {
if (checkTransform == potentialParentTransform) {
return true;
}
checkTransform = checkTransform.parent;
} while (checkTransform != null);
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 == null) {
return false;
}
Transform[] children = gameObject.GetComponentsInChildren<Transform>(includeInactive);
foreach (Transform 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;
}
}
}