using System.Collections; using System.Collections.Generic; using UnityEngine; namespace BITKit { public static class GameObjectExtensions { public static T GetOrAddComponent(this GameObject gameObject) where T : Component { return gameObject.TryGetComponent(out var t) ? t : gameObject.AddComponent(); } public static T RemoveComponent(this GameObject gameObject) where T : Component { var t = gameObject.GetComponent(); if (t is null) return null; Object.Destroy(t); return t; } public static int RemoveComponentsInChildren(this GameObject gameObject) where T :Component { var count = 0; foreach (var x in gameObject.GetComponentsInChildren()) { if (!x) continue; Object.Destroy(x); count++; } return count; } } }