- Home /
What is the proper method to use the AssetBundle API to unload all AssetBundles?
In a Unity 2019.2 AR App, it is designed so that in the active scene, the user can select various models to place in the AR Scene. At the time the item is selected, the associated AssetBundle is loaded (not from www, but held in the app).
The scripts which perform this action insure that the AssetBundle is never loaded more than once to provide better memory management.
At some point the user may want to start over, so i have created a "reset" button, which is intended to remove the active scene (there is never more than one scene), which does work. But that action does not remove from memory any of the loaded AssetBundles. In my reading of the API, there are methods for first identifying all the loaded assets, "AssetBundle.GetAllLoadedAssetBundles()" and then pass that information to the "AssetBundle.UnloadAllAssetBundles()".
But as a relative beginner, i am unclear how to use the "Get" method and pass that information to the "Unload" method when we don't know which AssetBundles have been loaded by the User.
Here is my UnloadScene Script thus far, with the portion in question in the commented area.
After reviewing the Unity API reference, i can see that the "Get" is listed as an "IEnumerable/IEnumerator". But i am relatively new, and do not really understand how to utilize this method to collect the list of AssetBundles to pass to the Unload method. I am struggling in understanding how to use these two methods.
Unity API Documentation for GetAllLoadedAssetBundles
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
public class unloadScene : MonoBehaviour
{
public void Update()
{
int y = SceneManager.GetActiveScene().buildIndex;
SceneManager.UnloadSceneAsync(y);
}
/* public static void Clear()
{
AssetBundle.GetAllLoadedAssetBundles();
AssetBundle.UnloadAllAssetBundles(true);
}
*/
}
After spending some time scouring the web for articles which explained the concept of IEnumerable, IEnumerator and Interfaces, i have attempted to start to structure a means to accomplish this unloading of all loaded asset bundles.
The Unity documentation shows the following reference:
public static IEnumerable<AssetBundle> GetAllLoadedAssetBundles();
Following that form, i started to re-write the first step, that of getting a list of all loaded asset bundles using this example:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class unloadAssetBundles : MonoBehaviour
{
public static IEnumerable<AssetBundle>
AssetBundle.GetAllLoadedAssetBundles();
}
The next step was to reference the logs in Unity to see what kinds of errors are generated, since i am sadly, very confident i don't understand this quite yet. Here are the errors received.
Assets/Scripts/unloadAssetBundles.cs(9,5): error CS0538: 'AssetBundle' in explicit interface declaration is not an interface
Assets/Scripts/unloadAssetBundles.cs(9,17): error CS0501: 'unloadAssetBundles.GetAllLoadedAssetBundles()' must declare a body because it is not marked abstract, extern, or partial
Both of which are referencing line 9 of code
AssetBundle.GetAllLoadedAssetBundles();
At this point, though it is clear Unity has specific functions to accomplish this task, i am simply not understanding the syntax of using IEnumerables, IEnumerators to collect the list of loaded bundles and then unload them.
Though i have posted this question on Stackoverflow on this is fundamental knowledge, but without any interest in the question, i am hitting a wall in how to accomplish this.
Any guidance from the Unity community is very, very welcomed.
After doing more research online, i was able to write a script that would compile. But unfortunately, it doesn't do anything. I do not believe i understand how to structure this so that the list of loaded asset bundles is handed off to the method that unloads all asset bundles. Any guidance here is very much appreciated.
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class unloadAssetBundles : MonoBehaviour
{
public IEnumerator GetTheBundles()
{
yield return new WaitForSeconds(2.0f);
AssetBundle.GetAllLoadedAssetBundles();
AssetBundle.UnloadAllAssetBundles(true);
}
}