自定义资源 新建插件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 (); 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; } 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 { return NSLOCTEXT ("AssetTypeActions" , "AssetTypeActions_StateMachine" , "StateMachine Blueprint" ); } FColor FAssetTypeActions_StateMachine::GetTypeColor () const { return FColor::Emerald; } UClass* FAssetTypeActions_StateMachine::GetSupportedClass () const { return UStateMachineAsset::StaticClass (); } 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 : 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 ; 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 #pragma once #include "AssetTypeCategories.h" #include "AssetToolsModule.h" #include "CoreMinimal.h" #include "Modules/ModuleManager.h" class FStateMachineEditorModule : public IModuleInterface{ public : 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; };
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 #include "StateMachineEditor.h" #include "AssetTypeActions_StateMachine.h" #include "ISettingsModule.h" #define LOCTEXT_NAMESPACE "FStateMachineEditorModule" void FStateMachineEditorModule::StartupModule () { IAssetTools& AssetToolsInstance = FModuleManager::LoadModuleChecked<FAssetToolsModule>("AssetTools" ).Get (); m_StateMachineCategoryType= AssetToolsInstance.RegisterAdvancedAssetCategory (TEXT ("StateMachine" ), LOCTEXT ("StateMachineAssetCategory" , "StateMachine" )); RegisterAssetType (AssetToolsInstance); RegisterSetting (); } void FStateMachineEditorModule::ShutdownModule () { 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
目录下右键点击就出现我们要的效果