Files

120 lines
3.0 KiB
C#

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<T>() where T : Component
{
return GameObject.HasComponent<T>();
}
public T GetComponent<T>() where T : Component
{
return GameObject.GetComponent<T>();
}
public T[] GetComponents<T>() where T : Component
{
return GameObject.GetComponents<T>();
}
public T GetComponentInChildren<T>() where T : Component
{
return GameObject.GetComponentInChildren<T>();
}
public T GetComponentInParent<T>() where T : Component
{
return GameObject.GetComponentInParent<T>();
}
public T AddComponent<T>() where T : Component
{
return GameObject.AddComponent<T>();
}
public bool TryGetComponent<T>(out T component) where T : Component
{
component = GetComponent<T>();
return component != null;
}
public bool CompareTag(string tag)
{
return GameObject.CompareTag(tag);
}
internal static T Create<T>(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;
}
}
}
}