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.
Answer by mamad_m2 · May 20, 2020 at 09:10 AM
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!
Answer by Denko86 · Feb 24, 2017 at 02:40 PM
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.
Thank for your answer.
Actually I don't have big textures but several little textures that I have to download simultaneously, and we want that the user can use the app even if its connection is not fast.
The textures are used to make the interface and converted to Sprite at runtime in order to be set in Image component. We externalized them to reduce the size of the app on the store, and because it is possible that we'll change these images later so we'll can update it without build again and publish an update on stores.
Currently, when the connection is bad, the user has to wait several seconds before the textures are downloaded and the interface can be shown, that is why we want to cache the textures.
What I don't like with the 2nd solution (save images somewhere in persistantDataPath for example after the first download) is that it looks like I have to develop a cache system similar to the one used by the assetbundles :
1) check if textures is stored in persistantDataPath
2) if not, download it from server. if yes, load it with ReadAllBytes.
3) if downloaded from server, store it in persistantDataPath And maybe add a system which check the version number to download it again if it has not the same version number as the one in the cache...
That is why I would prefer to use the assetbundle cache system, because it already does all of this.
$$anonymous$$oreover, I figured out that my textures are smaller when I put it in an assetbundle. mytexture.jpg = 193 $$anonymous$$o mytexture in an assetbundle = 62 $$anonymous$$o
I think it is caused by the compression performed by Unity. The asset bundle contains only the texture file put in Unity (no gameobject or other component) and I get the texture with bundle.GetAsset() and cast it into Texture2D.
But I don't know what is the fastest between download assetbundle then use GetAsset() in one hand, and download the png or jpg file and write it to file or load it directly from file if it already exists, in the other hand.
Here the advantages of using textures into assetbundles :
file size smaller than when it is jpg or png, so download will be faster
cache system
And the drawbacks :
create an assetbundle for each texture (it is boring if I cannot do it automatically, I mean for each texture I would have to go in the inspector and assign a new bundle name in the "AssetBundle" section below)
need to use Unity to make new asset bundles each time we want to update these images
create assetbundles for each platform (android, iOS, editor)
Do you think all the stuff to create a texture cache system (check if it is stored on device and then load its bytes into a texture, else download it and write its bytes in a file) will be executed more quickly than using UnityWebRequest.GetAssetBundle() and then bundle.GetAsset("myTexture") as Texture2D ?