UBlueprintFunctionLibrary
is a very usefull Class
when there's a need to e.g. work with data and files right in editor (for their preparation and simpliefied development), without need to switch to runtime.
Creating assets with C++ & UBlueprintFunctionLibrary
#include "Kismet/BlueprintFunctionLibrary.h" // Engine
UCLASS()
class UExampleClass : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
/**
* Editor Only - Will not work in packaged build.
*
* Create an asset.
*
* @param AssetPath The path of where to create the asset: "/Game/Folder/MyAsset"
* @param AssetClass The class of the asset to create
* @param AssetFactory Optional. The factory to use to create the asset
* @param bOutSuccess If the action was a success or not
* @param OutInfoMsg More information about the action's result
*
* @return The created asset
*/
UFUNCTION(BlueprintCallable, Category = " ... ")
static UObject* CreateAsset(FString AssetPath, UClass* AssetClass, class UFactory* AssetFactory, bool& bOutSuccess, FString& OutInfoMsg);
}
.cpp file - CreateAsset
function
#include "AssetToolsModule.h" // AssetTools (Editor Only)*
UObject* UExampleClass::CreateAsset(FString AssetPath, UClass* AssetClass, UFactory* AssetFactory, bool& bOutSuccess, FString& OutInfoMsg)
{
// Get the asset tools module
IAssetTools& AssetTools = FModuleManager::GetModuleChecked<FAssetToolsModule>("AssetTools").Get();
// Find right factory
UFactory* Factory = AssetFactory;
if (Factory == nullptr)
{
for (UFactory* Fac : AssetTools.GetNewAssetFactories())
{
if (Fac->SupportedClass == AssetClass)
{
Factory = Fac;
break;
}
}
if (Factory == nullptr)
{
bOutSuccess = false;
OutInfoMsg = FString::Printf(TEXT("Create Asset Failed - Was not able to find factory for AssetClass. '%s'"), *AssetPath);
return nullptr;
}
}
// Can factory create the desired asset?
if (Factory->SupportedClass != AssetClass)
{
bOutSuccess = false;
OutInfoMsg = FString::Printf(TEXT("Create Asset Failed - Factory cannot create desired AssetClass. '%s'"), *AssetPath);
return nullptr;
}
// Create asset
UObject* Asset = AssetTools.CreateAsset(FPaths::GetBaseFilename(AssetPath), FPaths::GetPath(AssetPath), AssetClass, Factory);
// Make sure it worked
if (Asset == nullptr)
{
bOutSuccess = false;
OutInfoMsg = FString::Printf(TEXT("Create Asset Failed - Either the path is invalid or the asset already exists. '%s'"), *AssetPath);
return nullptr;
}
// Return the asset
bOutSuccess = true;
OutInfoMsg = FString::Printf(TEXT("Create Asset Succeeded - '%s'"), *AssetPath);
return Asset;
}
* As of AssetToolsModule.h
is AssetTools
type of dependency, that is not attached to the project by default, it must be added to .Build.cs
file manually.
PrivateDependencyModuleNames.AddRange
(
new string[]
{
// Default Modules
"Core",
"CoreUObject",
"Engine",
// New Modules - Editor Only
"AssetTools",
}
);
Creating various kind of assets
World asset
#include "Engine/World.h" // Engine #include "Factories/WorldFactory.h" // "UnrealEd" (Editor Only) - dependency must be added to .Build.cs file /** * Editor Only - Will not work in packaged build. * * Create a World asset. * * @param AssetPath The path of where to create the asset: "/Game/Folder/MyAsset" * @param bOutSuccess If the action was a success or not * @param OutInfoMsg More information about the action's result * * @return The created asset */ UWorld* UExampleClass::CreateWorldAsset(FString AssetPath, bool& bOutSuccess, FString& OutInfoMsg) { // Factory UWorldFactory* Factory = NewObject<UWorldFactory>(); Factory->bInformEngineOfWorld = false; Factory->bCreateWorldPartition = false; Factory->FeatureLevel = ERHIFeatureLevel::ES3_1; // Create and return asset UObject* Asset = CreateAsset(AssetPath, UWorld::StaticClass(), Factory, bOutSuccess, OutInfoMsg); return Cast<UWorld>(Asset); }
Material asset
#include "Materials/Material.h" // Engine #include "Factories/MaterialFactoryNew.h" //"UnrealEd" (Editor Only) - dependency must be added to .Build.cs file /** * Editor Only - Will not work in packaged build. * * Create a Material asset. * * @param AssetPath The path of where to create the asset: "/Game/Folder/MyAsset" * @param bOutSuccess If the action was a success or not * @param OutInfoMsg More information about the action's result * * @return The created asset */ UMaterial* UExampleClass::CreateMaterialAsset(FString AssetPath, bool& bOutSuccess, FString& OutInfoMsg) { // Factory UMaterialFactoryNew* Factory = NewObject<UMaterialFactoryNew>(); Factory->InitialTexture = nullptr; // Create and return asset UObject* Asset = CreateAsset(AssetPath, UMaterial::StaticClass(), Factory, bOutSuccess, OutInfoMsg); return Cast<UMaterial>(Asset); }
LevelSequence asset
#include "LevelSequence.h" // "LevelSequence" - dependency must be added to .Build.cs file /** * Editor Only - Will not work in packaged build. * * Create a LevelSequence asset. * * @param AssetPath The path of where to create the asset: "/Game/Folder/MyAsset" * @param bOutSuccess If the action was a success or not * @param OutInfoMsg More information about the action's result * * @return The created asset */ ULevelSequence* UExampleClass::CreateLevelSequenceAsset(FString AssetPath, bool& bOutSuccess, FString& OutInfoMsg) { UObject* Asset = CreateAsset(AssetPath, ULevelSequence::StaticClass(), nullptr, bOutSuccess, OutInfoMsg); return Cast<ULevelSequence>(Asset); }
-
NiagaraSystem asset
#include "NiagaraSystem.h" // "Niagara" - dependency must be added to .Build.cs file #include "BehaviorTree/BlackboardData.h" /** * Editor Only - Will not work in packaged build. * * Create a NiagaraSystem asset. * * @param AssetPath The path of where to create the asset: "/Game/Folder/MyAsset" * @param bOutSuccess If the action was a success or not * @param OutInfoMsg More information about the action's result * * @return The created asset */ UNiagaraSystem* UExampleClass::CreateNiagaraSystemAsset(FString AssetPath, bool& bOutSuccess, FString& OutInfoMsg) { UObject* Asset = CreateAsset(AssetPath, UBlackboardData::StaticClass(), nullptr, bOutSuccess, OutInfoMsg); return Cast<UNiagaraSystem>(Asset); }
-
SoundCue asset
#include "Sound/SoundCue.h" // Engine /** * Editor Only - Will not work in packaged build. * * Create a SoundCue asset. * * @param AssetPath The path of where to create the asset: "/Game/Folder/MyAsset" * @param bOutSuccess If the action was a success or not * @param OutInfoMsg More information about the action's result * * @return The created asset */ USoundCue* UExampleClass::CreateSoundCueAsset(FString AssetPath, bool& bOutSuccess, FString& OutInfoMsg) { UObject* Asset = CreateAsset(AssetPath, USoundCue::StaticClass(), nullptr, bOutSuccess, OutInfoMsg); return Cast<USoundCue>(Asset); }
-
Enum asset
#include "Engine/UserDefinedEnum.h" // Engine /** * Editor Only - Will not work in packaged build. * * Create a Enum asset. * * @param AssetPath The path of where to create the asset: "/Game/Folder/MyAsset" * @param bOutSuccess If the action was a success or not * @param OutInfoMsg More information about the action's result * * @return The created asset */ UUserDefinedEnum* UExampleClass::CreateEnumAsset(FString AssetPath, bool& bOutSuccess, FString& OutInfoMsg) { UObject* Asset = CreateAsset(AssetPath, UUserDefinedEnum::StaticClass(), nullptr, bOutSuccess, OutInfoMsg); return Cast<UUserDefinedEnum>(Asset); }
-
Struct asset
#include "Engine/UserDefinedStruct.h" // Engine /** * Editor Only - Will not work in packaged build. * * Create a Struct asset. * * @param AssetPath The path of where to create the asset: "/Game/Folder/MyAsset" * @param bOutSuccess If the action was a success or not * @param OutInfoMsg More information about the action's result * * @return The created asset */ UUserDefinedStruct* UExampleClass::CreateStructAsset(FString AssetPath, bool& bOutSuccess, FString& OutInfoMsg) { UObject* Asset = CreateAsset(AssetPath, UUserDefinedStruct::StaticClass(), nullptr, bOutSuccess, OutInfoMsg); return Cast<UUserDefinedStruct>(Asset); }
- Importing Data
- Data Tables
- Importing meshes