Try to load a given asset:
TSubobject<UStaticMeshComponent> mesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("Mesh0"));
const ConstructorHelpers::FObjectFinder<UStaticMesh> MeshObj(TEXT("/Game/MyMesh"));
mesh->SetStaticMesh(MeshObj.Object);
There are some nice tutorials on how to load object dynamically such as this one from Rama or the documentation about Asynchronous Asset Loading.
To get a list of files what you need is to repeat the above process for each one. You can use the FileManager to find files within a directory. More about the file manager from its documentation page and another tutorial. To find all files in a directoy just use the FPlatformFileManager::Get().GetPlatformFile().FindFiles()
function with a wildcard
Yet another way would be loading a set of assets from a given path. It might fail if you try it from a packaged game and you are not adding the paths to be cooked. Add all paths into the Additional Asset Directories
to Cook section of your project.
TArray<UObject*> MeshAssets;
EngineUtils::FindOrLoadAssetsByPath(TEXT("/Game/Assets/Meshes/"), MeshAssets, EngineUtils::ATL_Regular);
Take into account that the first approach using the FObjectFinder
will work on a packaged game while EngineUtils::FindOrLoadAssetsByPath
requires the editor to be present. The following code will load an asset from a given path using an instance of FStreamableManager
. I usually just have one in my Game Singleton Class (more info on how to set one up can be found in this wiki post).
FStreamableManager& StreamableManager = [Get the singleton instance from where you declared it];
UObject* myObject = StreamableManager.SynchronousLoad(AssetPath);
In a packaged game you would normally use a PAK file with the content you would like to load and request the list of files that is contained in it. To add stuff to a pak file just the UnrealPak.exe for it. Here is a shorthand example, have a look at the FAssetStreamer::StreamPackage()
function which does the whole process (the function is not complete but outlines the required steps).
Just in case you would like to play with pak files this is how you add assets to them:
UnrealPak.exe OutPak.pak myasset.uasset anotherasset.uasset
As you can see there are lots of stuff that you can use. If you just want to be able to load all assets from a given path in runtime you should investigate further EngineUtils::FindOrLoadAssetsByPath
or you ensamble the list in the editor and then you use the FStreamableManager
or the FObjectFinder
to load them. In all cases you must ensure that your assets are taken into account when you cook your game.