【GAD翻译馆】创建自定义的编辑器资源
翻译:王成林(麦克斯韦的麦斯威尔 ) 审校:黄秀美(厚德载物)
在这篇文章中我们将创建一些自定义的编辑器资源。为了拓展UE4编辑器的功能并加入你自己的资源你需要两个类:
· 一个包含你的资源的不同属性的类
· 一个将以上类构建为一项编辑器资源(即.uasset文件)的类(名为XFactory,其中X代表资源的类名)
对于大多数的资源,它们的工厂类(factory class)位于编辑器的EditorFactories文件中。例如,当你在编辑器中创建一个新的纹理时,编辑器会显示Texture.h文件中定义的关于纹理的各种属性。然而,编辑器中构建该资源的类(名为UTexture2DFactoryNew)则位于EditorFactories中。
那么,我们来创建我们自定义的编辑器资源吧。
创建资源类
为了创建一个自定义类,我们添加一个新的C 类(我将其命名为OrfeasCustomAsset),它继承自Object类且包含以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include "CoreMinimal.h"#include "UObject/NoExportTypes.h"#include "OrfeasCustomAsset.generated.h"/** * */UCLASS()class CUSTOMASSET_API UOrfeasCustomAsset : public UObject{ GENERATED_BODY() protected: //Just some properties to display on the Editor UPROPERTY(EditAnywhere) FString Description; UPROPERTY(EditAnywhere) int32 BonusCoins; }; |
然后添加一个新的继承自工厂类的C 类:

然后在其头文件中添加以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | UCLASS()class CUSTOMASSET_API UOrfeasFactory : public UFactory{ GENERATED_BODY()public: UOrfeasFactory(); /* New assets that don't override this function are automatically placed into the "Miscellaneous" category in the editor */ virtual uint32 GetMenuCategories() const override; /* Creates the asset inside the UE4 Editor */ virtual UObject* FactoryCreateNew(UClass* InClass, UObject* InParent, FName InName, EObjectFlags Flags, UObject* Context, FFeedbackContext* Warn) override; }; |
在我们的源文件中为这些函数添加以下逻辑:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include "OrfeasFactory.h"//The asset header file that we wish to create#include "OrfeasCustomAsset.h"//The asset type categories will let us access the various asset categories inside the Editor#include "AssetTypeCategories.h"UOrfeasFactory::UOrfeasFactory(){ bCreateNew = true; bEditAfterNew = true; //Configure the class that this factory creates SupportedClass = UOrfeasCustomAsset::StaticClass();}uint32 UOrfeasFactory::GetMenuCategories() const{ //Let's place this asset in the Blueprints category in the Editor return EAssetTypeCategories::Blueprint;}UObject* UOrfeasFactory::FactoryCreateNew(UClass* InClass, UObject* InParent, FName InName, EObjectFlags Flags, UObject* Context, FFeedbackContext* Warn){ //Create the editor asset UOrfeasCustomAsset* OrfeasEditorAsset = NewObject return OrfeasEditorAsset;} |
为了使用AssetTypeCategories.h文件你需要在你的项目的公有依赖中添加 “AssetTools” :
1 | PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "AssetTools" }); |
好了,保存并编译你的代码。然后重启编辑器并检查蓝图的分类:


为你的资源添加一个自定义缩略图
你的新资源的默认缩略图就是该资源类的名字。如果你想要一个自定义的缩略图你需要创建一个新的Slate风格并将其和该资源类绑定。
理想情况下,你应将新资源放入一个新的模块或者插件中。为了这个例子的需要,我将使用一个插件的起始模块来为我的自定义资源创建一个新的slate风格。之前我已经在项目中添加了一个名为“OrfeasPlugin”的空白插件,现在我要使用它的默认图标作为一个新的缩略图。进入你的插件的头文件然后添加以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include "CoreMinimal.h"#include "ModuleManager.h"#include "SlateStyle.h"class FOrfeasPluginModule : public IModuleInterface{public: TSharedPtr /** IModuleInterface implementation */ virtual void StartupModule() override; virtual void ShutdownModule() override;}; |
然后在插件的源文件中添加以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include "IPluginManager.h"#include "SlateStyleRegistry.h"#define LOCTEXT_NAMESPACE "FOrfeasPluginModule"void FOrfeasPluginModule::StartupModule(){ // This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module StyleSet = MakeShareable(new FSlateStyleSet("OrfeasStyle")); //Content path of this plugin FString ContentDir = IPluginManager::Get().FindPlugin("OrfeasPlugin")->GetBaseDir(); //The image we wish to load is located inside the Resources folder inside the Base Directory //so let's set the content dir to the base dir and manually switch to the Resources folder: StyleSet->SetContentRoot(ContentDir); //Create a brush from the icon FSlateImageBrush* ThumbnailBrush = new FSlateImageBrush(StyleSet->RootToContentDir(TEXT("Resources/Icon128"), TEXT(".png")), FVector2D(128.f, 128.f)); if (ThumbnailBrush) { //In order to bind the thumbnail to our class we need to type ClassThumbnail.X where X is the name of the C class of the asset StyleSet->Set("ClassThumbnail.OrfeasCustomAsset", ThumbnailBrush); //Reguster the created style FSlateStyleRegistry::RegisterSlateStyle(*StyleSet); }}void FOrfeasPluginModule::ShutdownModule(){ // This function may be called during shutdown to clean up your module. For modules that support dynamic reloading, // we call this function before unloading the module. //Unregister the style FSlateStyleRegistry::UnRegisterSlateStyle(StyleSet->GetStyleSetName());}#undef LOCTEXT_NAMESPACE IMPLEMENT_MODULE(FOrfeasPluginModule, OrfeasPlugin) |
为了使用IPluginManager.h头文件你需要在你的插件的依赖中添加 “Projects” 依赖:
1 2 3 4 5 6 7 8 9 10 11 | PrivateDependencyModuleNames.AddRange( new string[] { "CoreUObject", "Engine", "Slate", "SlateCore", "Projects" // ... add private dependencies that you statically link with here ... } ); |
最后,确保将你的插件类型标记为“运行时”而不是“开发者”,这样你就可以成功地发行你的项目了。
保存并编译代码。然后重启编辑器。
这是最终结果:
【版权声明】
原文作者未做权利声明,视为共享知识产权进入公共领域,自动获得授权。
