wo load asset by objectlibrary and streamablemanager . It’s always in memory.I can’t Unload it using various methods.
Loading process:/使用ObjectLibrary 获取指定路径下的资产信息 AssetData。 使用AssertRegistry 也能获取资源的AssetData
void AAssetPlayerController::TestObjectLibrary()
{
if (!ObjectLibrary)
{
ObjectLibrary = UObjectLibrary::CreateLibrary(UObject::StaticClass(), true, true);
ObjectLibrary->AddToRoot();
}
int AssetCount = ObjectLibrary->LoadBlueprintAssetDataFromPath("/Game/Test");
UE_LOG(LogTemp, Warning, TEXT("Asset Count: %d"), AssetCount);
}
//将AssetData信息转换为FSoftObjectPtr或者FSoftObjectPath,然后才能用StreamableManager进行资源的异步加载
void AAssetPlayerController::TestStreamableManagerAsyncLoad()
{
FStreamableManager* streamable = new FStreamableManager();
TArray assetdata;
ObjectLibrary->GetAssetDataList(assetdata);
TArray<FSoftObjectPath> ItemsToStream;
for (int i = 0; i < assetdata.Num(); i++)
{
ItemsToStream.AddUnique(assetdata[i].ToSoftObjectPath());
}
if (ItemsToStream.Num() > 0)
{
streamable->RequestAsyncLoad(ItemsToStream, FStreamableDelegate::CreateUObject(this, &AAssetPlayerController::KeepReference));
}
}
//将加载的资源保持引用,否则在这个函数调用完成之后就会销毁
void AAssetPlayerController::KeepReference()
{
TArray assetdata;
ObjectLibrary->GetAssetDataList(assetdata);
TArray<FSoftObjectPath> ItemsToStream;
for (int i = 0; i < assetdata.Num(); i++)
{
ItemsToStream.AddUnique(assetdata[i].ToSoftObjectPath());
}
myobject.Add(assetdata[0].GetAsset());
GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Cyan, "Async Load Object OK");
}
-------------------------------------------I can unload it --------------------------------------------------------------------------------------------
void AAssetPlayerController::IsContainInMemory()
{
TArray myAssetData;
if (!ObjectLibrary)
{
ObjectLibrary = UObjectLibrary::CreateLibrary(UObject::StaticClass(), true, GIsEditor);
ObjectLibrary->AddToRoot();
}
ObjectLibrary->LoadBlueprintAssetDataFromPath("/Game/Test");
ObjectLibrary->GetAssetDataList(myAssetData);
if (myAssetData[0].IsAssetLoaded())
{
GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Cyan, “Async Remain In Memory”);
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Cyan, "Async Don't In Memory");
}
}
void AAssetPlayerController::UnLoadAsset()
{
TArray myAssetData;
ObjectLibrary->GetAssetDataList(myAssetData);
FStreamableManager* streamable = new FStreamableManager();
myobject[0] = NULL;
streamable->Unload(myAssetData[0].ToSoftObjectPath());
FString Tip = "Start Asset Unload" + myAssetData[0].GetPrimaryAssetId().ToString();
GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Cyan,Tip);
}
Help me .Thanks vary much!!