Now I have a pak file, which contains some .uasset files. I want to load the .uasset files at runtime but the code doesn’t work right. The code is as below:
IPlatformFile &PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
FPakPlatformFile *PakPlatfromFile = new FPakPlatformFile;
PakPlatfromFile->Initialize(&PlatformFile, TEXT(""));
FPakFile PakFile(PakFileName, false);
UE_LOG(LogDynamicStaticMeshActor, Log, TEXT("Init - IsPakFileValid:%d"), PakFile.IsValid());
FString MountPoint(FPaths::EngineDir());
int32 PakOrder = 0;
if (!PakPlatfromFile->Mount(PakFileName, PakOrder, *MountPoint)) {
UE_LOG(LogDynamicStaticMeshActor, Fatal, TEXT("Mount error"));
}
UObjectLibrary *ObjectLibrary = ConstructObject<UObjectLibrary>(UObjectLibrary::StaticClass());
ObjectLibrary->UseWeakReferences(true);
ObjectLibrary->AddToRoot();
UE_LOG(LogDynamicStaticMeshActor, Log, TEXT("Before load asset"));
ObjectLibrary->LoadAssetDataFromPath(MountPoint);
if (ObjectLibrary->IsLibraryFullyLoaded()) {
UE_LOG(LogDynamicStaticMeshActor, Log, TEXT("Fully loaded"));
ObjectLibrary->LoadAssetsFromAssetData();
}
TArray<FAssetData> AssetDataList;
int32 count = ObjectLibrary->GetAssetDataCount();
UE_LOG(LogDynamicStaticMeshActor, Log, TEXT("count:%d"), count);
It seems the mount step is successful, but when it runs to ObjectLibrary->LoadAssetDataFromPath(MountPoint), it is not fully loaded belong to the log, and ObjectLibrary->GetAssetDataCount() returns zero.
Also I’ve tried other ways:
void ADynamicStaticMeshActor::Init(const TCHAR* PakFileName) {
IPlatformFile &PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
FPakPlatformFile PakPlatfromFile;
PakPlatfromFile.Initialize(&PlatformFile, TEXT(""));
FPakFile PakFile(PakFileName, false);
UE_LOG(LogDynamicStaticMeshActor, Log, TEXT("Init - IsPakFileValid:%d"), PakFile.IsValid());
FString MountPoint(TEXT("/Engine/Content/DLC/"));
int32 PakOrder = 0;
if (!PakPlatfromFile.Mount(PakFileName, PakOrder, *MountPoint)) {
UE_LOG(LogDynamicStaticMeshActor, Fatal, TEXT("Mount error"));
}
TArray<FString> AssetFileNames;
PakFile.FindFilesAtPath(AssetFileNames, *MountPoint);
for (const FString &AssetName : AssetFileNames) {
UE_LOG(LogDynamicStaticMeshActor, Log, TEXT("AssetName:%s"), *AssetName);
}
FString MaterialFilePath = MountPoint + TEXT("BR_DongChenMingHan_6.uasset");
FString MaterialFileContent;
FFileHelper::LoadFileToString(MaterialFileContent, *MaterialFilePath);
UE_LOG(LogDynamicStaticMeshActor, Log, TEXT("Path:%s Content:%s"), *MaterialFilePath, *MaterialFileContent);
UStaticMeshComponent *StaticMeshComponent = NewObject<UStaticMeshComponent>(this, TEXT("staticMeshCom"));
FStreamableManager StreamableManager;
FString AssetName(MaterialFilePath);
FStringAssetReference AssetRef(AssetName);
Cast<UMaterial>(StaticLoadObject(UMaterial::StaticClass(), nullptr, *AssetName));
UObject *Object = StreamableManager.SynchronousLoad(AssetRef);
UMaterial *Material = Cast<UMaterial>(Object);
}
it could not load the assset file BR_DongChenMingHan_6.uasset to string. So I’m not sure whether the mount step is successful? And when loading the asset using StaticLoadObject, It logs “can’t find object” and I am really confused about this log for the file path seems correct.
The related log is as below:
LogDynamicStaticMeshActor: Init - IsPakFileValid:1
LogDynamicStaticMeshActor: Path:/Engine/Content/DLC/BR_DongChenMingHan_6.uasset Content:
LogLinker:Warning: Can't find file '/Engine/Content/DLC/BR_DongChenMingHan_6'
LogLinker:Warning: Can't find file '/Engine/Content/DLC/BR_DongChenMingHan_6'
LogUObjectGlobals:Warning: Failed to find object 'Material /Engine/Content/DLC/BR_DongChenMingHan_6.uasset'
LogDynamicStaticMeshActor: Before load Object
LogLinker:Warning: Can't find file '/Engine/Content/DLC/BR_DongChenMingHan_6'
LogUObjectGlobals:Warning: Failed to find object 'Object /Engine/Content/DLC/BR_DongChenMingHan_6.uasset'
LogStreamableManager: Failed attempt to load /Engine/Content/DLC/BR_DongChenMingHan_6.uasset
It’s very kind of u if anyone teach me the right resolution, for I’ve spent several days on this problem.