自定义资源

自定义资源

新建插件StateMachine,然后手动拷贝一份StateMachineEditor用于编辑器模块 ,把里面的所有名称改为后缀为Editor版本

然后在Editor版本的Build.cs 的Private部分中加入如下模块 "UnrealEd", "AssetTools",Public部分加入 "StateMachine"模块

资源类

创建随意UObject 资源,可以保持默认,本例中为UStateMachineAsset

行为类

创建FAssetTypeActions_StateMachine类继承自FAssetTypeActions_Base,此类可以接管资源双击打开编辑器的动作,这个类需要在模块启动时注册,稍微会讲

此类建议创建在Editor模块

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once

#include "Developer/AssetTools/Public/AssetTypeActions_Base.h"

class FAssetTypeActions_StateMachine: public FAssetTypeActions_Base
{
public:
FAssetTypeActions_StateMachine(EAssetTypeCategories::Type AsstCategory);
~FAssetTypeActions_StateMachine();

// IAssetTypeActions Implementation

virtual FText GetName()const override;
virtual FColor GetTypeColor()const override;;
virtual UClass* GetSupportedClass()const override;
virtual void OpenAssetEditor(const TArray<UObject*>& InObjects, TSharedPtr<class IToolkitHost> EditWithInLevelEditor = TSharedPtr<IToolkitHost>())override;

virtual UThumbnailInfo* GetThumbnailInfo(UObject* Asset)const override;
virtual uint32 GetCategories()override { return m_AssetCategory; }//自定义的标签,可以用系统自带的EAssetTypeCategories::Type

private:
EAssetTypeCategories::Type m_AssetCategory;
};
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
44
45
46
47
48
49
50
51
52

#include "AssetTypeActions_StateMachine.h"
#include "BlueprintEditorUtils.h"
#include "AssetRegistryModule.h"

#include "StateMachineAsset.h"

#include "ThumbnailRendering/SceneThumbnailInfo.h"

#define LOCTEXT_NAMESPACE "AssetTypeActions"

FAssetTypeActions_StateMachine::FAssetTypeActions_StateMachine(EAssetTypeCategories::Type AssetCategory)
:m_AssetCategory(AssetCategory)
{

}

FAssetTypeActions_StateMachine::~FAssetTypeActions_StateMachine()
{

}

FText FAssetTypeActions_StateMachine::GetName()const
{
// Content里右键菜单创建的显示的可创建类型的名字
return NSLOCTEXT("AssetTypeActions", "AssetTypeActions_StateMachine", "StateMachine Blueprint");
}

FColor FAssetTypeActions_StateMachine::GetTypeColor()const
{

return FColor::Emerald;
}

UClass* FAssetTypeActions_StateMachine::GetSupportedClass()const
{
return UStateMachineAsset::StaticClass();//创建的资源,即之前定义的UObject
}

//打开资源行为,通过这个方法可以打开其他视图
void FAssetTypeActions_StateMachine::OpenAssetEditor(const TArray<UObject*>& InObjects, TSharedPtr<class IToolkitHost> EditWithInLevelEditor)
{
UE_LOG(LogTemp, Log, TEXT("FAssetTypeActions_StateMachine::OpenAssetEditor"));
//内容暂无
}

UThumbnailInfo* FAssetTypeActions_StateMachine::GetThumbnailInfo(UObject* Asset)const
{
return NULL;
}

#undef LOCTEXT_NAMESPACE
工厂类

此类最好创建在Editor模块

此类是让引擎认识我们创建的这个资源和自定义标签,似乎定义了以后引擎就自动会识别,即上述两个类的功能

不创建的话连对应的自定义的标签也无法显示

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
#pragma once

#include "StateMachineAsset.h"
#include "Engine/Blueprint.h"
#include "Factories/Factory.h"
#include "UObject/Object.h"
#include "UObject/ObjectMacros.h"
#include "UObject/ScriptMacros.h"

#include "StateMachineFactory.generated.h"

UCLASS(HideCategories = Object,MinimalAPI)
class UStateMachineFactory : public UFactory
{
GENERATED_BODY()

public:
UStateMachineFactory(const FObjectInitializer& ObjectInitializer);
virtual ~UStateMachineFactory();

public:
UPROPERTY(EditAnywhere, Category = "StateMachineFactory")
TEnumAsByte<EBlueprintType> mBlueprintType;

UPROPERTY(EditAnywhere, Category = "StateMachineFactory")
TSubclassOf<UStateMachineAsset> mSupportedClass;

public:
// override UFactory function
virtual bool ConfigureProperties()override;
virtual UObject* FactoryCreateNew(UClass* InClass, UObject* InParent, FName InName, EObjectFlags InFlags, UObject* InContext, FFeedbackContext* InWarn,FName InCallingContext)override;
virtual UObject* FactoryCreateNew(UClass* InClass, UObject* InParent, FName InName, EObjectFlags InFlags, UObject* InContext, FFeedbackContext* InWarn);
};
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

#include "StateMachineFactory.h"
#include "StateMachineAsset.h"

UStateMachineFactory::UStateMachineFactory(const FObjectInitializer& ObjectInitializer)
:Super(ObjectInitializer), mSupportedClass(UStateMachineAsset::StaticClass())
{
bCreateNew = true;
bEditAfterNew = true;

// 这个Factory托管的类型
SupportedClass = mSupportedClass;
}

UStateMachineFactory::~UStateMachineFactory()
{

}

bool UStateMachineFactory::ConfigureProperties()
{
return true;
}

UObject* UStateMachineFactory::FactoryCreateNew(UClass* InClass, UObject* InParent, FName InName, EObjectFlags InFlags, UObject* InContext, FFeedbackContext* InWarn,FName InCallingContext)
{
return FactoryCreateNew(InClass, InParent, InName, InFlags, InContext, InWarn);
}

UObject* UStateMachineFactory::FactoryCreateNew(UClass* InClass,UObject* InParent,FName InName,EObjectFlags InFlags,UObject* InContext,FFeedbackContext* InWarn)
{
UStateMachineAsset* NewStateMachineAssetItem = NewObject<UStateMachineAsset>(InParent, InClass, InName, InFlags);
return NewStateMachineAssetItem;
}
模块

用于注册自定义标签以及我们之前创建的FAssetTypeActions_StateMachine

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
// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.

#pragma once
#include "AssetTypeCategories.h"
#include "AssetToolsModule.h"

#include "CoreMinimal.h"
#include "Modules/ModuleManager.h"

class FStateMachineEditorModule : public IModuleInterface
{
public:

/** IModuleInterface implementation */
virtual void StartupModule() override;
virtual void ShutdownModule() override;


void RegisterAssetType(IAssetTools& AssetTools);
void RegisterSetting();
void UnRegisterSetting();


private:
EAssetTypeCategories::Type m_StateMachineCategoryType;//自定义标签
TArray<TSharedPtr<IAssetTypeActions>> m_CreatedAssetTypeActions;//Action数组
};

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
44
45
46
47
48
// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.

#include "StateMachineEditor.h"
#include "AssetTypeActions_StateMachine.h"
#include "ISettingsModule.h"

#define LOCTEXT_NAMESPACE "FStateMachineEditorModule"

void FStateMachineEditorModule::StartupModule()
{
// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
IAssetTools& AssetToolsInstance = FModuleManager::LoadModuleChecked<FAssetToolsModule>("AssetTools").Get();
//创建自定义标签,可以本地化翻译
m_StateMachineCategoryType= AssetToolsInstance.RegisterAdvancedAssetCategory(TEXT("StateMachine"), LOCTEXT("StateMachineAssetCategory", "StateMachine"));
RegisterAssetType(AssetToolsInstance);
RegisterSetting();

}

void FStateMachineEditorModule::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.

UnRegisterSetting();
}

void FStateMachineEditorModule::RegisterAssetType(IAssetTools& AssetTools)
{
TSharedRef<IAssetTypeActions> StateMachineBlueprint = MakeShareable(new FAssetTypeActions_StateMachine(m_StateMachineCategoryType));
//注册资源行为类
AssetTools.RegisterAssetTypeActions(StateMachineBlueprint);
m_CreatedAssetTypeActions.Add(StateMachineBlueprint);
}

void FStateMachineEditorModule::RegisterSetting()
{
//暂时为空
}

void FStateMachineEditorModule::UnRegisterSetting()
{
//暂时为空
}

#undef LOCTEXT_NAMESPACE

IMPLEMENT_MODULE(FStateMachineEditorModule, StateMachineEditor)

然后我们在引擎Content目录下右键点击就出现我们要的效果

image-20200710105049753