Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Snake9 · Feb 24, 2017 at 12:54 PM · assetbundletexturesdownloadcache

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
5

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Snake9 · Feb 24, 2017 at 04:06 PM 1
Share

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 ?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

100 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Assets Bundle size increases after download 0 Answers

Assets Bundle size increases 20 times after download 0 Answers

How do I retreive the cached assetbundles??~!!! 0 Answers

Serialization depth limit exceeded at 'MaterialNode' error 0 Answers

Texture Issue on android device 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges