0

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

SHAI
  • 789
  • 3
  • 10
  • 43

1 Answers1

1

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");
A_Kz
  • 139
  • 1
  • 8
  • 2
    You should **never** simply use a string concatenation `+ "/"` for system paths though! Rather use `Path.Combine(Application.persistentDataPath, "some", "file")` instead which automatically inserts the correct path separators according to the OS the code is executed on. – derHugo Dec 28 '19 at 12:29
  • File.WriteAllBytes(Application.persistentDataPath + "/some/file"); is incorrect, as it includes two parameters, one is path another is byte[]. – Akhrorjon Jul 21 '21 at 07:42