UE4_C++笔记
目录
C++
获取当前世界指针
UWorld* World = GWorld;
或者
UWorld* World = GEgine->GetWorldContexts()[0].World();
创建文件夹拾取窗口
- Module:DesktopPlatform
- Header:IDesktopPlatform.h
IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get();
if(DesktopPlatform)
{
TSharedPtr<SWindow> ParentWindow = FSlateApplication::Get().FindWidgetWindow(AsShared());
void* ParentWindowHandle = (ParentWindow.IsValid() && ParentWindow->GetNativeWindow().IsValid()) ? ParentWindow->GetNativeWindow()->GetOSWindowHandle() : nullptr;
FString FolderName;
const FString Title = "Choose a repository location";
const bool bFolderSelected = DesktopPlatform->OpenDirectoryDialog(ParentWindowHandle, Title, "", FolderName);
if(bFolderSelected)
{
this->ExportSettingWidgetsPtr->DirectoryText->SetText(FText::FromString(FolderName));
}
}
获取Actor所在地图名称
FString Dir,LevelName;
Actor->GetLevel->GetPackage()->GetFName().ToString().Split("/",&Dir,&LevelName,ESearchCase::IgnoreCase,ESearchDir::FromEnd);
选择世界里所有指定类型的Actor
- Module: Engine
- Header: EngineUtils.h
UWorld* World = GWorld;
TArray<AActor*> NeedSelectActors;
for (TActorIterator<AActor> It(World,AStaticMeshActor::StaticClass());It;++It)
{
NeedSelectActors.Add(*It);
}
获取当前世界所有已加载关卡
UWorld* = GWorld;
TArray<ULevel*> Levels;
Levels = World->GetLevels();
获取当前选择的已加载关卡
- Module: Engine
- Header: LevelUtils.h
UWorld* = GWorld;
TArray<ULevel*> SelectedLevels;
for (ULevel* level : World->GetSelectedLevels())
{
// 判断该地图是否加载了
if (FLevelUtils::IsLevelLoaded(level))
{
SelectedLevels.Add(level);
}
}
获取关卡名称
ULevel* CurrentLevel;
FString L, LevelName;
CurrentLevel->GetPackage()->GetFName().ToString().Split("/", &L, &LevelName, ESearchCase::IgnoreCase, ESearchDir::FromEnd);
判断文件/路径是否存在
- Module: Core
- Header: Misc/Paths.h
FString filePath, directory;
FPaths::FileExists(filePath);
FPaths::DirectoryExists(directory);
通过文件路径获取资产
- Module: Core
- Header: Modules/ModuleManager.h
FString AssetPath;
FAssetData Asset = FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry").Get().GetAssetByObjectPath(*AssetPath);
添加撤回操作
const FScopedTransaction Transaction(FText::FromString("Operator name"))
obj->Modify();// 记录该obj编辑前状态
obj->progress();// 修改操作
添加任务进度条
GWarn->BeginSlowTask(FText::FromString("Message"), true);
/*
Progress...
*/
GWarn->UpdateProgress(i, total);
GWarn->EndSlowTask();
数据用JSON格式导出
- Module: Json
- Header:
- Dom/JsonObject.h
- Misc/FileHelper.h
- Serialization/JsonSerializer.h
FString JsonStr;
TSharedPtr<FJsonObject> Obj = MakeShareable(new FJsonObject());
TSharedRef<TJsonWriter<TCHAR>> jsonWriter = TJsonWriterFactory<TCHAR>::Create(&JsonStr);
// key,value成对写入到FString
FString key, value;
Obj->SetStringField(key, value);
// 写到文件
FString filePath;
FJsonSerializer::Serialize(Obj.ToSharedRef(), jsonWriter);
FFileHelper::SaveStringToFile(JsonStr, filePath);
/*
Out file:
{
key1: value1,
key2: value2,
...
}
*/
从外部JSON文件读取数据
- Module: Json
- Header:
- Dom/JsonObject.h
- Misc/FileHelper.h
- Serialization/JsonSerializer.h
FString filePath, jsonStr;
FFileHelper::LoadFileToString(jsonStr, *filePath);
TSharedPtr<FJsonObject> Obj = MakeShareable(new FJsonObject());
TSharedRef<TJsonReader<>> JsonReader = TJsonReaderFactory<>::Create(jsonStr);
if (FJsonSerializer::Deserialize(JsonReader,Obj))
{
const FString value = Obj->GetStringField("key1");// return value1
}
Slate
编辑器式样
- Module: EditorStyle
- Header: EditorStyleSet.h
- File path: Source\Editor\EditorStyle\Private\SlateEditorStyle.cpp
SNew(SButton)
.ButtonStyle(FEditorStyle::Get(), "HoverHintOnly")// 按钮式样
[
SNew(SImage)
.Image(FEditorStyle::GetBrush("PropertyWindow.Button_Delete"))// 图标
]