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 /
avatar image
0
Question by im012 · Oct 13, 2017 at 12:46 PM · imagewwwdownloadsystem.iobytes

Downloading image from the server crash the game in Android Device ?

The code is working perfect in editor but some time it show the high CPU usage in stats windows...that's why when build .apk and run in android device it causes crash.

Any Solution for this..

Here is my code..

 void Start()
 {
     imgNum = 1;
     StartCoroutine (Load ());
 }

 // Use this for initialization
 IEnumerator Load () 
 {
     if (File.Exists (Application.persistentDataPath + ""+imgNum+".jpg"))
     {
         imgNum += 1;
         if (imgNum > 19) 
         {
             Panel.SetActive (false);
             this.enabled = false;
         }  
         else
         {
             StartCoroutine (Load ());
         }
     }  
     else 
     {
         WWW www = new WWW("URL"+imgNum+".jpg");
         yield return www;
         Texture2D texture = www.texture;
         byte[] bytes = texture.EncodeToJPG ();
         File.WriteAllBytes (Application.persistentDataPath + ""+imgNum+".jpg", bytes);
         imgNum += 1;
                     if (imgNum > 19)
         {
             Panel.SetActive (false);
             this.enabled = false;
         }  
         else
         {
             StartCoroutine (Load ());
         }
     }
     
 }
Comment
Add comment · Show 2
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 im012 · Oct 14, 2017 at 04:20 AM 0
Share

Any $$anonymous$$gestion for this...!!

avatar image DeveshPandey · Jun 22, 2018 at 10:43 AM 0
Share

If you will download any big large file or multiple files then it will cause a memory leak if you are using WWW class. So you can use WebClient class of c# to download the files. There is a plugin (http://u3d.as/RDf?aid=1101|43jr) to download any big file from web without memory leak. I hope this will help you. Ask me if you have any other questions.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by FlyingHighUp · Oct 14, 2017 at 05:08 AM

I'm unsure, what does the adb log say?

Personally, my first suspicion is RAM. If your image is too large, (~40MB+) Android will kill your app for using too much memory. http://answers.unity3d.com/questions/547900/how-to-prevent-android-to-kill-my-unity-app-.html .

Your current code downloads 19 images in succession without disposing them, and each image is tripled because you generate a new Texture2D, then encoded it to a JPG. So you end up potentially having a whopping 57 images in memory lol

Since the data you downloaded is already in JPG format, you could just save www.bytes instead of re-encoding the image. Then right after saving it, go www.Dispose() to clear it from RAM asap (otherwise, it'll just linger).

If your images are really large, you might have to look into streaming them into files directly.

Comment
Add comment · Show 8 · 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 im012 · Oct 14, 2017 at 06:01 AM 0
Share

Thank u sir...this means now i have to store images a bytes and after download all images i have to call www.Dispose()..m i Right ?

avatar image FlyingHighUp im012 · Oct 14, 2017 at 06:06 AM 0
Share

Almost, more like:

 WWW www = new WWW("URL"+imgNum+".jpg");
 yield return www;
 File.WriteAllBytes (Application.persistentDataPath + ""+imgNum+".jpg", www.bytes);
 www.Dispose();

for each image. Hopefully that will solve your memory woes. Note you have to dispose each www after you're done with it.

avatar image im012 FlyingHighUp · Oct 14, 2017 at 06:43 AM 0
Share

The game stop crashing when i download image..

This time Another Error occurs when i trying to access this image it will again crash the game...and images download take much time to download...

here is my code for Fetching Image..

byte[] byteArray = File.ReadAllBytes (Application.persistentDataPath + "" + imgNum+ ".jpg");

Texture2D texture = new Texture2D (8, 8);

texture.LoadImage (byteArray);

Show more comments

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

75 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

Related Questions

few questions about using www for getting bunch of images... 0 Answers

Download and save files on iphone? 1 Answer

Loading images from web to editor window 3 Answers

www download strange...help 0 Answers

downloading file, saving on android 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