日常开发中有需求批量在大纲面板批量创建一些文件夹模板,此时若通过工具一键实现则非常方便:
创建文件夹部分需要借助C++编写工具函数实现,接下来就来讲解下具体操作。
1.编写Outliner相关蓝图函数
首先在项目中打开VS工程,引用Outliner相关编辑器模块:
build.cs
csharp
using UnrealBuildTool;
public class MyProject : ModuleRules
{
public MyProject(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput", /*此处增加*/"SceneOutliner" });
}
}
创建蓝图函数库,编写具体接口:
.h:
c++
UCLASS()
class MYPROJECT_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
UFUNCTION(BlueprintCallable, Category = "CreateOutlinerFolderTool")
static void CreateOutlinerFolderTemplate(FName FolderName);
};
.cpp:
c++
// Fill out your copyright notice in the Description page of Project Settings.
#include "ToolMenus.h"
#include "SSceneOutliner.h"
#include "LevelEditor.h"
#include "Engine/Selection.h"
#include "Engine/Level.h"
#include "MyBlueprintFunctionLibrary.h"
void UMyBlueprintFunctionLibrary::CreateOutlinerFolderTemplate(FName FolderName)
{
FModuleManager& ModuleManager = FModuleManager::Get();
if (ModuleManager.IsModuleLoaded(TEXT("UnrealEd")))
{
TWeakPtr<ILevelEditor> LevelEditor = FModuleManager::GetModuleChecked<FLevelEditorModule>(TEXT("LevelEditor")).GetLevelEditorInstance();
if (LevelEditor.IsValid())
{
TArray<TWeakPtr<ISceneOutliner>> SceneOutlinerPtrs = LevelEditor.Pin()->GetAllSceneOutliners();
for (TWeakPtr<ISceneOutliner> SceneOutlinerPtr : SceneOutlinerPtrs)
{
if (TSharedPtr<ISceneOutliner> SceneOutlinerPin = SceneOutlinerPtr.Pin())
{
SSceneOutliner* outliner = (SSceneOutliner*)SceneOutlinerPin.Get();
TArray<FName> folders;
folders.Add(FolderName);
outliner->PasteFoldersBegin(folders);
outliner->PasteFoldersEnd();
}
break;
}
}
}
}
这里之前试了下FActorFolders接口发现有问题,应该使用SSceneOutliner的接口进行创建。
2.创建Widget进行测试
编译好后,创建对应编辑器UI进行测试,调用该节点即可:
最终呼出该菜单,点击后即可在场景内创建对应文件夹模板。