using System; using System.Reflection; namespace XCEngine { public abstract class Component : Object { internal ulong m_gameObjectUUID; protected Component() { } protected Component(ulong gameObjectUUID) { m_gameObjectUUID = gameObjectUUID; } public ulong GameObjectUUID => m_gameObjectUUID; public GameObject GameObject => new GameObject(m_gameObjectUUID); public GameObject gameObject => GameObject; public Transform Transform => GameObject.Transform; public Transform transform => Transform; public string Tag { get => GameObject.tag; set => GameObject.tag = value; } public string tag { get => Tag; set => Tag = value; } public int Layer { get => GameObject.layer; set => GameObject.layer = value; } public int layer { get => Layer; set => Layer = value; } public bool HasComponent() where T : Component { return GameObject.HasComponent(); } public T GetComponent() where T : Component { return GameObject.GetComponent(); } public T[] GetComponents() where T : Component { return GameObject.GetComponents(); } public T GetComponentInChildren() where T : Component { return GameObject.GetComponentInChildren(); } public T GetComponentInParent() where T : Component { return GameObject.GetComponentInParent(); } public T AddComponent() where T : Component { return GameObject.AddComponent(); } public bool TryGetComponent(out T component) where T : Component { component = GetComponent(); return component != null; } public bool CompareTag(string tag) { return GameObject.CompareTag(tag); } internal static T Create(ulong gameObjectUUID) where T : Component { return Create(typeof(T), gameObjectUUID) as T; } internal static Component Create(Type componentType, ulong gameObjectUUID) { if (componentType == null || gameObjectUUID == 0 || !typeof(Component).IsAssignableFrom(componentType)) { return null; } try { return Activator.CreateInstance( componentType, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, binder: null, args: new object[] { gameObjectUUID }, culture: null) as Component; } catch { return null; } } } }