• 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 /
avatar image
3
Question by Paulo-Henrique025 · Apr 12, 2013 at 06:13 AM · texturewwwdownload

Can I access previously downloaded textures?

Hi guys, didn't found anyt$$anonymous$$ng related. I'll be using www to get textures but once the player opens the game again I don't want to download the previously downloaded textures, is there a way to access these downloaded textures? Are they stored in some folder?

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
7
Best Answer

Answer by Dreamora · Apr 12, 2013 at 06:54 AM

No, t$$anonymous$$ngs you download through WWW are dropped unless you store them yourself (exception: www.LoadFromCacheOrDownload for assetbundles)

If you store the texture after a download then you can access it through WWW and the file:// protocol again (if its standalone or mobile) If it is the webplayer, just do the request again and the browser will provide it out of the cache.


Edit, re Dreamora's incredibly useful comment below, here's some tested code version of that! Hope it helps someone!

Call it with a gameObject w$$anonymous$$ch has a flat plane the size you want with an example image on it, and the URL in question of some jpg.

 private IEnumerator Paint(GameObject go, string url)
     {
     go.renderer.material.mainTexture =
         new Texture2D(512,512, TextureFormat.DXT1, false);
     
     var www = new WWW(url);
     yield return www;
     www.LoadImageIntoTexture(
         go.renderer.material.mainTexture as Texture2D);
     }
 
 private IEnumerator PaintCachewise(GameObject go, string url)
     {
     go.renderer.material.mainTexture =
         new Texture2D(512,512, TextureFormat.DXT1, false);
     
     Debug.Log("url "+url);
     
     string justFilename = System.IO.Path.GetFileName(url);
     Debug.Log("justFilename " +justFilename);
     
     string localName =
         Application.persistentDataPath + "/" + justFilename;
     Debug.Log("localName " + localName);
     
     if ( System.IO.File.Exists( localName ) )
         Debug.Log("            It Exists");
     else
         Debug.Log("            -- does not exist yet");
     
     if ( System.IO.File.Exists( localName ) )
         {
         // proudly load from cache with confidence
         
         var ccc = new WWW( "file://" + localName);
         yield return ccc;
         ccc.LoadImageIntoTexture(
                 go.renderer.material.mainTexture as Texture2D);
         
         Debug.Log("loaded from speedy local cache.");    
         }
     else
         {
         var www = new WWW(url);
         yield return www;
         www.LoadImageIntoTexture(
                 go.renderer.material.mainTexture as Texture2D);
     
         System.IO.File.WriteAllBytes( localName, www.bytes );
         
         Debug.Log("loaded from planetary cloud data system - "
                  +" and saved to device SSD");
         }
     
     // per Dreamora http://answers.unity3d.com/answers/436748 !!
     }
Comment
Add comment · Show 5 · 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 Paulo-Henrique025 · Apr 13, 2013 at 02:30 AM 0
Share

So yes? I can store and access them using WWW and file protocol?

avatar image jiteshvm · Apr 13, 2013 at 02:41 AM 1
Share

Yes. Another way could to store it permanently could be to write it to a file in the Resources folder and then check it before downloading it again. This is for the standalone build. I dont know if you can read and write files using the browser plugin.

avatar image Paulo-Henrique025 · Apr 13, 2013 at 02:44 AM 0
Share

No problem with the webplayer, I'm looking for a solution on iOs/Android :)

avatar image Dreamora · Apr 13, 2013 at 10:01 AM 2
Share

Its absolutely no problem on iOS and Android Use www and then store www.bytes onto the device using System.IO.File.WriteAllBytes(Application.persistentDataPath + "/someImageName.xxx",www.bytes); with xxx being the same filetype your requested file from the web has.

later on you can load that image again using new WWW("file://"+Application.persistentDataPath + "/someImageName.xxx")

avatar image Paulo-Henrique025 · Apr 13, 2013 at 02:18 PM 0
Share

Already did it, thanks again @Dreammora :)

avatar image
2

Answer by lassiiwa · Dec 17, 2015 at 02:40 PM

here's a little more generic cached WWW utility function. you will need to point the hash function to somet$$anonymous$$ng that you have and the StartCoroutine come from some monobehaviour. just use t$$anonymous$$s instead of your normal WWW constructor.

 static public WWW getCachedWWW(string url)
     {
         string filePath = Application.persistentDataPath;
         filePath += "/" + GetInt64HashCode(url);
         string loadFilepath = filePath;
         bool web = false;
         WWW www;
         bool useCached = false;
         useCached = System.IO.File.Exists(filePath);
         if (useCached)
         {
             //check how old
             System.DateTime written = File.GetLastWriteTimeUtc(filePath);
             System.DateTime now = System.DateTime.UtcNow;
             double totalHours = now.Subtract(written).TotalHours;
             if (totalHours > 300)
                 useCached = false;
         }
         if (useCached)
         {
             string pathforwww = "file://" + loadFilepath;
             Debug.Log("TRYING FROM CACHE " + url + "  file " + pathforwww);
             www = new WWW(pathforwww);
         }
         else
         {
             web = true;
             www = new WWW(url);
         }
         yoursomesclass.instance.StartCoroutine(doLoad(www, filePath, web));
         return www;
     }
 
     static IEnumerator doLoad(WWW www, string filePath, bool web)
     {
         yield return www;
 
         if (www.error == null)
         {
             if (web)
             {
                 //System.IO.Directory.GetFiles
                 Debug.Log("SAVING DOWNLOAD  " + www.url + " to " + filePath);
                // string fullPath = filePath;
                 File.WriteAllBytes(filePath, www.bytes);
                 Debug.Log("SAVING DONE  " + www.url + " to " + filePath);
                 //Debug.Log("FILE ATTRIBUTES  " + File.GetAttributes(filePath));
                 //if (File.Exists(fullPath))
                // {
                 //    Debug.Log("File.Exists " + fullPath);
                // }
            }
             else
             {
                 Debug.Log("SUCCESS CACHE LOAD OF " + www.url);
             }
         }
         else
         {
             if (!web)
             {
                 File.Delete(filePath);
             }
             Debug.Log("WWW ERROR " + www.error);
         }
     }
Comment
Add comment · Show 3 · 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 antx · Jan 05, 2016 at 01:53 PM 1
Share

I think you have a bug in there. Should line 19 not be: if (useCached) instead of if (System.IO.File.Exists(filePath)) ?

And loadFilepath is actually not needed since you can use filePath instead, right?

avatar image lassiiwa antx · Jan 12, 2016 at 04:44 AM 0
Share

yes, good catch. had fixed it on my local version but not here. was mainly just posting as inspiration, that a simple disk based cache that will do the job in a pinch is not too complex to make and that you don't necessarily need to rewrite all the parts of your app that use www to use it, since www will load from disk quite easily(and at least for android and win32 apps you don't need to write any special cases for the path either.)

also what I'm doing in my actual app is caching some of the the www's in memory.

it's too bad unity doesn't do this itself though, since it would really be a rather simple addition to unity and very useful. also www doesn't support retries and such, so mostly you will end up writing your own wrapper for www.

avatar image Alex-Simson · Mar 11, 2016 at 02:59 PM 1
Share

Thanks alot for your solution!

I had problem with function GetInt64HashCode so I changed

 filePath += "/" + GetInt64HashCode(url);

to

 var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(url);
 filePath += "/" + System.Convert.ToBase64String(plainTextBytes);
   

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

WWW class downloading image doesn't work in Web Player? 1 Answer

How to load interlaced streamed image into a texture? 0 Answers

WWW 16 bits textureformat download possible? 2 Answers

www.texture textureType to GUI 0 Answers

Ios www.texture returns question mark 1 Answer


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