Search code examples
iosmemory-managementunity-game-engineresourcesassetbundle

How to organize resources loading and unloading in Unity3D?


I have 340 sprites and I want to show them like a comic book. The comic book has 39 chapters and each chapter has around 6-10 pictures.

I noticed that Unity does not allow to loading resources from Resources folder if the file resources.assets is bigger than 2Gb, so I split all resources in two sets: first set of images are left in Resources folder, and the second set is combined into AssetBundle.

So now if I want to watch the first chapter it loads pictures from the first chapter, and if I go ahead and select the second chapter, it unloads images from the first chapter and loads images from the second chapter. But if I want to see the first chapter again then there are two situations.

If images from first chapter were loading from Resources folder - all right, it just loads them again.

If images were loading from AssetBundle - it can't just load these images again because when I was switching from first chapter to second, it unloaded resources from first chapter and they have been deleted now.

So how do I fix the second situation with AssetBundle and deleted resources? I just can't hold all unused resources in memorythe whole time.


Solution

  • Just load the asset bundle again using the WWW class exactly as you did before. You will have to let your app wait until the loading is done. You should show a loading popup while it is loading.

    Something like this in a coroutine:

    WWW www = WWW.LoadFromCacheOrDownload (url, 1);
    yield return www;
    AssetBundle bundle = www.assetBundle;
    AssetBundleRequest request = bundle.LoadAssetAsync ("img1", typeof(Texture2D));
    yield return request;
    Texture2D img = request.asset as Texture2D ;
    

    http://docs.unity3d.com/Manual/LoadingAssetBundles.html