feat(scripting): add script add-component api
This commit is contained in:
@@ -15,14 +15,20 @@ std::string ToStdString(const Containers::String& value) {
|
||||
|
||||
std::vector<std::string> SplitMaterialPaths(const std::string& value) {
|
||||
std::vector<std::string> paths;
|
||||
std::stringstream stream(value);
|
||||
std::string item;
|
||||
while (std::getline(stream, item, '|')) {
|
||||
paths.push_back(item);
|
||||
if (value.empty()) {
|
||||
return paths;
|
||||
}
|
||||
|
||||
if (value.empty()) {
|
||||
paths.clear();
|
||||
size_t start = 0;
|
||||
while (true) {
|
||||
const size_t separator = value.find('|', start);
|
||||
if (separator == std::string::npos) {
|
||||
paths.push_back(value.substr(start));
|
||||
break;
|
||||
}
|
||||
|
||||
paths.push_back(value.substr(start, separator - start));
|
||||
start = separator + 1;
|
||||
}
|
||||
|
||||
return paths;
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user