using System; using UnityEngine; using Object = UnityEngine.Object; namespace Jovian.Utilities { public static class GameObjectUtilities { public static void DestroyGameObjectsOfType() where T : MonoBehaviour { var objects = Object.FindObjectsByType(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(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; } } }