First commit
This commit is contained in:
59
Packages/com.jovian.utilities/Runtime/GameObjectUtilities.cs
Normal file
59
Packages/com.jovian.utilities/Runtime/GameObjectUtilities.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user