86 lines
2.3 KiB
C#
86 lines
2.3 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 bool HasComponent<T>() where T : Component
|
|
{
|
|
return GameObject.HasComponent<T>();
|
|
}
|
|
|
|
public T GetComponent<T>() where T : Component
|
|
{
|
|
return GameObject.GetComponent<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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|