Search code examples
c#unity-game-engineassetsassetbundle

How to save AssetBundle on device?


I have a game app, once my user gets to level 25- I want to download new pics (wallpapers&characters) that weren't included in the installation, I can do it with assetBundle . But I want to download the assetBundle only once and save it (permanently) on the device. My code so far-

AssetBundle myLoadedAssetBundle = AssetBundle.LoadFromFile(@"C:\ab\level25"); *path will be a server
Sprite newSprite = myLoadedAssetBundle.LoadAsset<Sprite>("Assets/pic1.jpg");
....

I read a bit about LoadFromCacheOrDownload but it seems to be obsolete, also not sure if it removes the assetbundle object after a restart. I basically want to save "myLoadedAssetBundle" object on my device, and extract from it my pics when I need, this way my user won't have to connect to the internet and redownload the files again


Solution

  • You can store the asset bundle itself in the PersistentDataPath, or open it and store the extracted content in managed folders.

    To store content:

    File.WriteAllBytes(Application.persistentDataPath + "/some/file");
    

    To read assets:

    var data = File.ReadAllBytes(Application.persistentDataPath + "/some/file");
    

    To read stored assetBundle:

    AssetBundle bundleLoadRequest = AssetBundle.LoadFromFile(Application.persistentDataPath + "/some/bundle");