What is the best way to cache downloaded textures on Android/iOS ?

Hi,

I have some textures to download in my app (.jpg and .png), I am using UnityWebRequest.GetTextures and it works well but the textures are downloaded each time I need them.

What is the best way to cache the textures and not download them twice, like the caching system for assetbundles ?

I see 2 possibilities :

  1. Make an assetbundle for each texture and use UnityWebRequest.GetAssetBundle to get the texture, instead of using GetTextures. Is it a good way ? And if so, do I have to create a gameobject with a script containing only the reference to my texture, and then make a bundle from this gameobject, download it, get the script component and then get the reference of the texture ? Or can I just make a bundle with only one texture, without any gameobject or script containing ref ?

  2. Keep using GetTextures and save files in the device to simulate a cache ? And when I have to download it again, verify if it is store on the device or not.

Any other solution ? What is the best ?

Thanks in advance.

For everyone else, to download and display images in Unity you can easily use Davinci. This open-source library has a simple usage and supports Unity UI.Image and 3D model textures and lots of other cool features like download progress, placeholders, caching and etc.

Davinci.get().load(imageUrl).into(image).start();

Hope this helps!

You can write the texture to file using EncodeToPNG() then System.File.IO.WriteAllBytes()

You can read in by using ReadAllBytes() and then

Texture2D tex = new Texture2D(2,2);

tex.Load(System.File.IO.ReadAllBytes(path));

If the textures are big, then I suggest you save it while in a coroutine pausing every so often after writing X number of bytes.