feat(scripting): add script add-component api

This commit is contained in:
2026-03-27 15:32:37 +08:00
parent 9c94adb4a2
commit f0d6d4f41c
10 changed files with 401 additions and 6 deletions

View File

@@ -221,6 +221,37 @@ bool HasNativeComponent(Components::GameObject* gameObject, ManagedComponentKind
return false;
}
Components::Component* AddOrGetNativeComponent(Components::GameObject* gameObject, ManagedComponentKind componentKind) {
if (!gameObject) {
return nullptr;
}
switch (componentKind) {
case ManagedComponentKind::Transform:
return gameObject->GetTransform();
case ManagedComponentKind::Camera:
return gameObject->GetComponent<Components::CameraComponent>()
? static_cast<Components::Component*>(gameObject->GetComponent<Components::CameraComponent>())
: static_cast<Components::Component*>(gameObject->AddComponent<Components::CameraComponent>());
case ManagedComponentKind::Light:
return gameObject->GetComponent<Components::LightComponent>()
? static_cast<Components::Component*>(gameObject->GetComponent<Components::LightComponent>())
: static_cast<Components::Component*>(gameObject->AddComponent<Components::LightComponent>());
case ManagedComponentKind::MeshFilter:
return gameObject->GetComponent<Components::MeshFilterComponent>()
? static_cast<Components::Component*>(gameObject->GetComponent<Components::MeshFilterComponent>())
: static_cast<Components::Component*>(gameObject->AddComponent<Components::MeshFilterComponent>());
case ManagedComponentKind::MeshRenderer:
return gameObject->GetComponent<Components::MeshRendererComponent>()
? static_cast<Components::Component*>(gameObject->GetComponent<Components::MeshRendererComponent>())
: static_cast<Components::Component*>(gameObject->AddComponent<Components::MeshRendererComponent>());
case ManagedComponentKind::Unknown:
return nullptr;
}
return nullptr;
}
Components::CameraComponent* FindCameraComponent(uint64_t gameObjectUUID) {
Components::GameObject* gameObject = FindGameObjectByUUID(gameObjectUUID);
return gameObject ? gameObject->GetComponent<Components::CameraComponent>() : nullptr;
@@ -318,6 +349,11 @@ uint64_t InternalCall_GameObject_GetComponent(uint64_t gameObjectUUID, MonoRefle
return gameObjectUUID;
}
uint64_t InternalCall_GameObject_AddComponent(uint64_t gameObjectUUID, MonoReflectionType* componentType) {
Components::GameObject* gameObject = FindGameObjectByUUID(gameObjectUUID);
return AddOrGetNativeComponent(gameObject, ResolveManagedComponentKind(componentType)) ? gameObjectUUID : 0;
}
mono_bool InternalCall_Behaviour_GetEnabled(uint64_t scriptComponentUUID) {
ScriptComponent* component = FindScriptComponentByUUID(scriptComponentUUID);
return (component && component->IsEnabled()) ? 1 : 0;
@@ -955,6 +991,7 @@ void RegisterInternalCalls() {
mono_add_internal_call("XCEngine.InternalCalls::GameObject_SetActive", reinterpret_cast<const void*>(&InternalCall_GameObject_SetActive));
mono_add_internal_call("XCEngine.InternalCalls::GameObject_HasComponent", reinterpret_cast<const void*>(&InternalCall_GameObject_HasComponent));
mono_add_internal_call("XCEngine.InternalCalls::GameObject_GetComponent", reinterpret_cast<const void*>(&InternalCall_GameObject_GetComponent));
mono_add_internal_call("XCEngine.InternalCalls::GameObject_AddComponent", reinterpret_cast<const void*>(&InternalCall_GameObject_AddComponent));
mono_add_internal_call("XCEngine.InternalCalls::Behaviour_GetEnabled", reinterpret_cast<const void*>(&InternalCall_Behaviour_GetEnabled));
mono_add_internal_call("XCEngine.InternalCalls::Behaviour_SetEnabled", reinterpret_cast<const void*>(&InternalCall_Behaviour_SetEnabled));
mono_add_internal_call("XCEngine.InternalCalls::Transform_GetLocalPosition", reinterpret_cast<const void*>(&InternalCall_Transform_GetLocalPosition));