F15.AssetLoad的几种方法
大约 1 分钟
前置
异步

//全局Handle
private:
TSharedPtr<FStreamableHandle> Handle;
void UDataDefinitionManager::AsyncCollectionDefinitions()
{
TArray<FPrimaryAssetId> AssetIds;
const TArray<FName> LoadBundles;
UKismetSystemLibrary::GetPrimaryAssetIdList(FPrimaryAssetType(CollectionDefinitionTypeName), AssetIds);
if (AssetIds.IsEmpty())
{
UE_LOG(LogTemp, Warning, TEXT("No assets found for type %s"), *CollectionDefinitionTypeName.ToString());
return;
}
UnloadAssets();
UAssetManager& Manager = UAssetManager::Get();
const FStreamableDelegate& NewDelegate = FStreamableDelegate::CreateUObject(this, &UDataDefinitionManager::CollectionCompleted);
Handle = Manager.LoadPrimaryAssets(AssetIds,LoadBundles,NewDelegate);
}
void UDataDefinitionManager::CollectionCompleted()
{
TArray<UObject*> Loaded;
const UAssetManager& Manager = UAssetManager::Get();
//也可以直接
Manager.GetPrimaryAssetObjectList(FPrimaryAssetType(CollectionDefinitionTypeName),Loaded);
if(!Handle.IsValid()) return;
Handle->GetLoadedAssets(Loaded);
if(Loaded.IsEmpty()) return;
CachedFragments.Empty();
for (UObject* LoadedObject : Loaded)
{
if (auto DataDefinition = Cast<UDataDefinition>(LoadedObject))
{
CachedFragments.Add(DataDefinition->Name,DataDefinition);
}
}
OnDefinitionsLoaded.Broadcast();
}

问题


2025/0322 补充
- 使用的类型反射宏UPROPERTY(Instanced) ,必须标记为
Instanced
- 对应的对象反射宏UCLASS(DefaultToInstanced, EditInlineNew, Abstract),必须标记为
EditInlineNew
- 注意类中的SoftPtr需要手动异步加载

同步

参考文档
https://www.cnblogs.com/shiroe/p/14710066.html
https://qiita.com/Zi_su/items/81dc5b5e29a96ad2cecc
https://jdelezenne.github.io/Codex/UE4/Assets%20Streaming.html