How to Unload Asset in memory loaded by StreamableManager

wo load asset by objectlibrary and streamablemanager . It’s always in memory.I can’t Unload it using various methods I want to know how to manually unload it ~~.
Loading by next methods:

 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<FAssetData> 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<FAssetData> 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<FAssetData> 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<FAssetData> 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!! I want to know how to manually unload it ~~

1 Like