在Unreal Engine中实现按下快捷键记录当前LevelViewport的坐标并保存为JSON,可以通过以下步骤进行:\n\n1. 创建一个编辑器蓝图类,用于处理编辑器相关的功能,例如捕获按键事件和保存JSON文件。确保该蓝图类继承自"EditorUtilityWidget",这样它就可以在编辑器中使用。\n\n2. 在蓝图类中创建一个按键事件,用于触发记录坐标的功能。在事件图表中,将按键事件节点(例如"InputKey"节点)连接到一个自定义的事件。\n\n3. 在自定义事件中,使用"GetActiveViewportClient"函数获取当前的"FEditorViewportClient"对象,该对象包含了当前LevelViewport的相关信息。\n\n4. 从"FEditorViewportClient"对象中获取需要的坐标信息,例如"ViewLocation"和"ViewRotation"。\n\n5. 创建一个JSON对象,并将坐标信息存储为键值对。\n\n6. 使用"FJsonObjectConverter"类将JSON对象转换为字符串。\n\n7. 使用"FFileHelper"类将字符串保存为JSON文件。\n\n下面是一个示例蓝图类的伪代码,用于实现上述功能:\n\ncpp\nclass UEditorFunctionality : public UEditorUtilityWidget\n{\n GENERATED_BODY()\n\npublic:\n UFUNCTION(BlueprintCallable, Category = \"Editor\")\n void RecordViewportCoordinate();\n\nprivate:\n void SaveCoordinateAsJson(FVector Location, FRotator Rotation);\n};\n\nvoid UEditorFunctionality::RecordViewportCoordinate()\n{\n FEditorViewportClient* ViewportClient = GetActiveViewportClient();\n if (ViewportClient != nullptr)\n {\n FVector Location = ViewportClient->GetViewLocation();\n FRotator Rotation = ViewportClient->GetViewRotation();\n\n SaveCoordinateAsJson(Location, Rotation);\n }\n}\n\nvoid UEditorFunctionality::SaveCoordinateAsJson(FVector Location, FRotator Rotation)\n{\n TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject);\n JsonObject->SetNumberField(\"X\", Location.X);\n JsonObject->SetNumberField(\"Y\", Location.Y);\n JsonObject->SetNumberField(\"Z\", Location.Z);\n JsonObject->SetNumberField(\"Pitch\", Rotation.Pitch);\n JsonObject->SetNumberField(\"Yaw\", Rotation.Yaw);\n JsonObject->SetNumberField(\"Roll\", Rotation.Roll);\n\n FString JsonString;\n TSharedRef<TJsonWriter<>> JsonWriter = TJsonWriterFactory<>::Create(&JsonString);\n FJsonSerializer::Serialize(JsonObject.ToSharedRef(), JsonWriter);\n\n FString SavePath = FPaths::ProjectDir() + \"Saved/ViewportCoordinate.json\";\n FFileHelper::SaveStringToFile(JsonString, *SavePath);\n}\n\n\n在编辑器中,将该蓝图类作为窗口小部件添加到所需的UI布局中。然后,将按键事件绑定到记录坐标的函数。当按下快捷键时,将触发该事件,记录并保存当前LevelViewport的坐标信息为JSON文件。


原文地址: https://www.cveoy.top/t/topic/pvZp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录