using UnityEngine; namespace SRF { public static class SRFGameObjectExtensions { public static T GetIComponent(this GameObject t) where T : class { return t.GetComponent(typeof(T)) as T; } public static T GetComponentOrAdd(this GameObject obj) where T : Component { T val = obj.GetComponent(); if (val == null) { val = obj.AddComponent(); } return val; } public static void RemoveComponentIfExists(this GameObject obj) where T : Component { T component = obj.GetComponent(); if (component != null) { Object.Destroy(component); } } public static void RemoveComponentsIfExists(this GameObject obj) where T : Component { T[] components = obj.GetComponents(); for (int i = 0; i < components.Length; i++) { Object.Destroy(components[i]); } } public static bool EnableComponentIfExists(this GameObject obj, bool enable = true) where T : MonoBehaviour { T component = obj.GetComponent(); if (component == null) { return false; } component.enabled = enable; return true; } public static void SetLayerRecursive(this GameObject o, int layer) { SetLayerInternal(o.transform, layer); } private static void SetLayerInternal(Transform t, int layer) { t.gameObject.layer = layer; foreach (Transform item in t) { SetLayerInternal(item, layer); } } } }