Hi.
I’m trying to import assets dynamically at runtime. For this purpose i use FSoftObjectPath and FStreamableManager. But when i find the softpath to an asset and want to pass it to RequestAsyncLoad, it doesn’t work and it seems it doesn’t compile properly. Here are my exact questions and will be thankful if anyone answers.
1- how should i pass a function to RequestAsyncLoad as it’s second argument?
2- what does this callback function supposed to do? is it going to be called with every tick or it’s for unloading the loaded assets.
3- what should i do when my asset changes? should i call RequestAsyncLoad everyframe or it’s handled by itself?
4- when should i unload the assets exactly?
and if someone corrects me if i misunderstood something, i will be so grateful.
here is my code:
.cpp
TSubclassOf<UStaticMesh> ObjectClass;
UObjectLibrary* ObjectLibrary = UObjectLibrary::CreateLibrary(ObjectClass, false, GIsEditor);
if (ObjectLibrary != nullptr)
{
ObjectLibrary->AddToRoot();
// TODO : This only understands .uasset files. we have to convert obj to uasset.
int32 ret = ObjectLibrary->LoadAssetDataFromPath(TEXT("/Game/StarterContent/Shapes"));
ObjectLibrary->LoadAssetsFromAssetData();
TArray<FAssetData> AssetDatas;
ObjectLibrary->GetAssetDataList(AssetDatas);
UE_LOG(LogTemp, Warning, TEXT("Objects found: %d"), ret);
FSoftObjectPath AssetPath;
for (int32 i = 0; i < AssetDatas.Num(); ++i)
{
FAssetData& AssetData = AssetDatas[i];
if (AssetData.GetFullName().Contains("Cube"))
{
UE_LOG(LogClass, Warning, TEXT("LoadAsset data : %s"), *AssetData.GetFullName());
AssetPath = AssetData.ToSoftObjectPath();
break;
}
}
Streamable.RequestAsyncLoad(AssetPath,FStreamableDelegate::CreateUObject(this, &AMyActor::justForTest));
UObject* loadedAsset = AssetPath.ResolveObject();
Cylinder->SetStaticMesh(Cast<UStaticMesh>(loadedAsset));
Cylinder->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
Cylinder->SetWorldScale3D(FVector(1.f));
}
.h
void justForTest();
static FStreamableManager Streamable;