At some point in your project, you might need to create a new custom Asset class, for example, an Asset to store conversation data in an RPG.
In order to properly integrate these with Content Browser, you'll need to create a new Asset type.
UObject
:#pragma once #include "Object.h" #include "MyCustomAsset.generated.h" /** * */ UCLASS() class UE4COOKBOOK_API UMyCustomAsset : public UObject { GENERATED_BODY() public: UPROPERTY(EditAnywhere, Category = "Custom Asset") FString Name; };
UCustomAssetFactory
based on UFactory
, overriding FactoryCreateNew
:#pragma once #include "Factories/Factory.h" #include "CustomAssetFactory.generated.h" /** * */ UCLASS() class UE4COOKBOOK_API UCustomAssetFactory : public UFactory { GENERATED_BODY() public: UCustomAssetFactory(); virtual UObject* FactoryCreateNew(UClass* InClass, UObject* InParent, FName InName, EObjectFlags Flags, UObject* Context, FFeedbackContext* Warn, FName CallingContext) override; };
#include "UE4Cookbook.h" #include "MyCustomAsset.h" #include "CustomAssetFactory.h" UCustomAssetFactory::UCustomAssetFactory() :Super() { bCreateNew = true; bEditAfterNew = true; SupportedClass = UMyCustomAsset::StaticClass(); } UObject* UCustomAssetFactory::FactoryCreateNew(UClass* InClass, UObject* InParent, FName InName, EObjectFlags Flags, UObject* Context, FFeedbackContext* Warn, FName CallingContext) { auto NewObjectAsset = NewObject<UMyCustomAsset>(InParent, InClass, InName, Flags); return NewObjectAsset; }
FString
property to contain a name.UPROPERTY
so that it remains in memory, and additionally marked as EditAnywhere
so that it is editable on both, the default object and on instances of it.Factory
. Unreal uses the Factory
design pattern to create instances of assets.Factory
that uses virtual methods to declare the interface of object creation, and then Factory
subclasses are responsible for creating the actual object in question.UFactory
as our base class, we include the appropriate header.bCreateNew
signifies that the factory is currently able to create a new instance of the object in question from scratch.bEditAfterNew
indicates that we would like to edit the newly created object immediately after creation.SupportedClass
variable is an instance of UClass
containing reflection information about the type of object the factory will create.UFactory
subclass is the actual factory method—FactoryCreateNew
.FactoryCreateNew
is responsible for determining the type of object that should be created, and using NewObject
to construct an instance of that type. It passes the following parameters through to the NewObject
call.InClass
is the class of object that will be constructed.InParent
is the object that should be containing the new object that will be created. If this isn't specified, the object is assumed to go into the transient package, which means that it won't be automatically saved.Name
is the name of the object to be created.Flags
is a bitmask of creation flags that control things such as making the object visible outside of the package it is contained in.FactoryCreateNew
, decisions can be made regarding which subclass should be instantiated. Other initialization can also be performed; for example, if there are sub-objects that require manual instantiation or initialization, they can be added here.UObject* UCameraAnimFactory::FactoryCreateNew(UClass* Class,UObject* InParent,FName Name,EObjectFlags Flags,UObject* Context,FFeedbackContext* Warn) { UCameraAnim* NewCamAnim = NewObject<UCameraAnim>(InParent, Class, Name, Flags); NewCamAnim->CameraInterpGroup = NewObject<UInterpGroupCamera>(NewCamAnim); NewCamAnim->CameraInterpGroup->GroupName = Name; return NewCamAnim; }
NewObject
to populate the CameraInterpGroup
member of the NewCamAnim
instance.3.128.197.164