插件的主要功能是
(1)在工具栏上的按钮点击 打开文件选择对话框
(2)选中文件,二进制方式读取文件内容

文件结构

主要实现代码在 showLogicGrid.cpp中,PluginButtonClicked这个上按钮点击的回调函数
(1)打开件选择对话框的代码
void* ParentWindowHandle = FSlateApplication::Get().GetActiveTopLevelWindow()->GetNativeWindow()->GetOSWindowHandle();
IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get();
if (DesktopPlatform)
{
//打开文件选择对话框
uint32 SelectionFlag = 0;
TArray<FString> OutFileNames;
FString DefaultPath = FPaths::ProjectContentDir();
DesktopPlatform->OpenFileDialog(ParentWindowHandle, "logicGrid", DefaultPath, FString(""), TEXT("(Image Files)|*.hmp;)"), SelectionFlag, OutFileNames);
}
注意要包含头文件
#include "Developer/DesktopPlatform/Public/IDesktopPlatform.h"
#include "Developer/DesktopPlatform/Public/DesktopPlatformModule.h"
(2)二进制读文件
TArray<uint8> kLogicData;
FFileHelper::LoadFileToArray(kLogicData, *fileName);
int iOffset = 0;
for (int iloop = 0; iloop < 4; iloop++)
{
m_iWidth += kLogicData[iloop] << iloop * 8;
}
以二进制形式读到kLogicData中,注意要包含头文件#include "Misc/FileHelper.h"
(3)根据读到的数据在游戏世界中创建一些对象
UWorld* kWorld = GCurrentLevelEditingViewportClient->GetWorld();这样获得World指针
要包含#include "LevelEditorViewport.h"

这篇博客介绍了如何在C++中通过插件实现文件选择对话框的功能,以及如何以二进制方式读取选中文件的内容。主要代码位于showLogicGrid.cpp中的PluginButtonClicked回调函数,利用IDesktopPlatform模块打开文件选择对话框,并使用FFileHelper加载文件到内存。此外,还展示了如何根据读取的数据在游戏世界中创建对象。
803

被折叠的 条评论
为什么被折叠?



