using System; using UnityEngine; using UnityObject = UnityEngine.Object; namespace Unity.VisualScripting { public static class ComponentHolderProtocol { public static bool IsComponentHolderType(Type type) { Ensure.That(nameof(type)).IsNotNull(type); return typeof(GameObject).IsAssignableFrom(type) || typeof(Component).IsAssignableFrom(type); } public static bool IsComponentHolder(this UnityObject uo) { return uo is GameObject || uo is Component; } public static GameObject GameObject(this UnityObject uo) { if (uo is GameObject) { return (GameObject)uo; } else if (uo is Component) { return ((Component)uo).gameObject; } else { return null; } } public static T AddComponent(this UnityObject uo) where T : Component { if (uo is GameObject) { return ((GameObject)uo).AddComponent(); } else if (uo is Component) { return ((Component)uo).gameObject.AddComponent(); } else { throw new NotSupportedException(); } } public static T GetOrAddComponent(this UnityObject uo) where T : Component { return uo.GetComponent() ?? uo.AddComponent(); } public static T GetComponent(this UnityObject uo) { if (uo is GameObject) { return ((GameObject)uo).GetComponent(); } else if (uo is Component) { return ((Component)uo).GetComponent(); } else { throw new NotSupportedException(); } } public static T GetComponentInChildren(this UnityObject uo) { if (uo is GameObject) { return ((GameObject)uo).GetComponentInChildren(); } else if (uo is Component) { return ((Component)uo).GetComponentInChildren(); } else { throw new NotSupportedException(); } } public static T GetComponentInParent(this UnityObject uo) { if (uo is GameObject) { return ((GameObject)uo).GetComponentInParent(); } else if (uo is Component) { return ((Component)uo).GetComponentInParent(); } else { throw new NotSupportedException(); } } public static T[] GetComponents(this UnityObject uo) { if (uo is GameObject) { return ((GameObject)uo).GetComponents(); } else if (uo is Component) { return ((Component)uo).GetComponents(); } else { throw new NotSupportedException(); } } public static T[] GetComponentsInChildren(this UnityObject uo) { if (uo is GameObject) { return ((GameObject)uo).GetComponentsInChildren(); } else if (uo is Component) { return ((Component)uo).GetComponentsInChildren(); } else { throw new NotSupportedException(); } } public static T[] GetComponentsInParent(this UnityObject uo) { if (uo is GameObject) { return ((GameObject)uo).GetComponentsInParent(); } else if (uo is Component) { return ((Component)uo).GetComponentsInParent(); } else { throw new NotSupportedException(); } } public static Component GetComponent(this UnityObject uo, Type type) { if (uo is GameObject) { return ((GameObject)uo).GetComponent(type); } else if (uo is Component) { return ((Component)uo).GetComponent(type); } else { throw new NotSupportedException(); } } public static Component GetComponentInChildren(this UnityObject uo, Type type) { if (uo is GameObject) { return ((GameObject)uo).GetComponentInChildren(type); } else if (uo is Component) { return ((Component)uo).GetComponentInChildren(type); } else { throw new NotSupportedException(); } } public static Component GetComponentInParent(this UnityObject uo, Type type) { if (uo is GameObject) { return ((GameObject)uo).GetComponentInParent(type); } else if (uo is Component) { return ((Component)uo).GetComponentInParent(type); } else { throw new NotSupportedException(); } } public static Component[] GetComponents(this UnityObject uo, Type type) { if (uo is GameObject) { return ((GameObject)uo).GetComponents(type); } else if (uo is Component) { return ((Component)uo).GetComponents(type); } else { throw new NotSupportedException(); } } public static Component[] GetComponentsInChildren(this UnityObject uo, Type type) { if (uo is GameObject) { return ((GameObject)uo).GetComponentsInChildren(type); } else if (uo is Component) { return ((Component)uo).GetComponentsInChildren(type); } else { throw new NotSupportedException(); } } public static Component[] GetComponentsInParent(this UnityObject uo, Type type) { if (uo is GameObject) { return ((GameObject)uo).GetComponentsInParent(type); } else if (uo is Component) { return ((Component)uo).GetComponentsInParent(type); } else { throw new NotSupportedException(); } } } }