feat(scripting): add mono csharp runtime foundation
This commit is contained in:
70
managed/XCEngine.ScriptCore/Component.cs
Normal file
70
managed/XCEngine.ScriptCore/Component.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace XCEngine
|
||||
{
|
||||
public abstract class Component
|
||||
{
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user