Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 3d_Game_Ready · Aug 07, 2014 at 05:48 PM · performancewwwdownloading

Background downloading causing performance jitters

Hi Everyone

Apologies if this has been answered, but I've looked everywhere in the last week and could not find an answer to my exact problem.

I am using www to download levels that are stored in assetbundles (WWW.LoadFromCacheOrDownload). When the game starts the first level is downloaded using a GUI and a progress bar. While the player is busy playing this level I start downloading the next level so that it might be ready for him by the time he completes the first level. It works great, except for jitter during the download. (Not when the asset file is being loaded at the end of the download). If I set my local apache server SendBufferSize to something small like 512 the download jitter disappears. If I increase the SendBufferSize the jitter increases accordingly. This make me think that Unity downloads a chunk of data into memory and does some processing at the end of each buffered chunk. (Write it to disk?) The bigger the chunk of data the more the game play will experience hiccups.

Now while adjusting SendBufferSize sounds like a solution, it does not work when the assetbundle is hosted on a public server. If I adjust the SendBufferSize it makes no difference. I assume there are caching servers between my device and the server and I am unable to adjust SendBufferSize on those.

I have tried modifying the www thread priority as well as backgroundLoadingPriority but it caused only a slight improvement.

Platform: iOS Xcode 5.1.1 Unity 4.5.2p1

Any ideas?

Comment
Add comment · Show 4
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 meat5000 ♦ · Aug 07, 2014 at 02:15 PM 0
Share

www calls tend to halt things until they are complete. I'm not sure what best practise is but maybe it can be handled with a coroutine?

avatar image 3d_Game_Ready · Aug 07, 2014 at 02:34 PM 0
Share

There is indeed a coroutine using a while(!download.isDone) loop.

avatar image npatch 3d_Game_Ready · Oct 24, 2015 at 10:43 AM 0
Share

Indeed, you can put the www handling inside a coroutine and yield while the download is not done. We use it where I work for downloading images on app startup and it ends up downloading like 40 images without fixed res and it handles it pretty well. On the other hand , seeing you rolled your own, might be better. Not really a fan of www class. Seems like patchwork.

avatar image Jakhongir npatch · Oct 24, 2015 at 04:10 PM 0
Share

Hi, if I put www in coroutine, downloading still causes jittering and freezes(each asset bundle about 3mb), I think it is because courutine works in main thread.

1 Reply

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

Answer by 3d_Game_Ready · Dec 08, 2014 at 10:33 AM

I had to implement a custom downloader since the Unity WWW jitter could not be resolved.

Comment
Add comment · Show 6 · 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 Jakhongir · Oct 24, 2015 at 08:17 AM 0
Share

Hello, could you share you implementation of custom downloader?

avatar image 3d_Game_Ready · Oct 26, 2015 at 08:59 AM 1
Share

I'm trying to post it but it tells me I can only post 3000 characters max.

avatar image 3d_Game_Ready · Oct 26, 2015 at 09:11 AM 1
Share

using System.Text; using UnityEngine;

public class Downloader { uint contentLength; int bytesDownloaded = 0; int read = 0; public float progress = 0.0f;

 NetworkStream networkStream; 
 static FileStream fileStream;
 Socket client;
 public string error = null;
 
 public Downloader (string uRL, string filename) 
 {
     Uri myUri = new Uri(uRL);   
     string host = myUri.Host;
     //UnityEngine.Debug.Log ("host:" + host);
     string uri = uRL.Substring (uRL.IndexOf(host)+host.Length);
     //UnityEngine.Debug.Log ("uri:" + uri);
     string query = "GET " + uri.Replace(" ", "%20") + " HTTP/1.1\r\n" +
         "Host: " + host + "\r\n" +
             "User-Agent: undefined\r\n" +
             "Connection: close\r\n"+
             "\r\n";
     
     
     //UnityEngine.Debug.Log (query);
     
     client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
     try{
         client.Connect(host, 80);   
         
         networkStream = new NetworkStream(client);
         
         var bytes = Encoding.Default.GetBytes(query);
         networkStream.Write(bytes, 0, bytes.Length);
     }
     catch(Exception e){
         UnityEngine.Debug.LogError ("Connection Error:" + e.$$anonymous$$essage);
         error = "Are you connected to the internet?";
         return;
     }
     
     var bReader = new BinaryReader(networkStream, Encoding.Default);
     string response = "";
     string line;
     char c;
avatar image 3d_Game_Ready · Oct 26, 2015 at 09:11 AM 0
Share
     do 
     {
         line = "";
         c = '\u0000';
         while (true) 
         {
             c = bReader.ReadChar();
             if (c == '\r')
                 break;
             line += c;
         }
         c = bReader.ReadChar();
         response += line + "\r\n";
     } 
     while (line.Length > 0);  
     
     //UnityEngine.Debug.Log ( response );
     
     Regex reContentLength = new Regex(@"(?<=Content-Length:\s)\d+", RegexOptions.IgnoreCase);
     contentLength = uint.Parse(reContentLength.$$anonymous$$atch(response).Value);
     try{
         string fileName = UnityEngine.Application.persistentDataPath + "/" + filename;
         if(System.IO.File.Exists(fileName)){
             System.IO.File.Delete(fileName);
         }
         fileStream = new FileStream( fileName, File$$anonymous$$ode.Create);
         iPhone.SetNoBackupFlag (fileName);
     }
     catch(Exception e){
         UnityEngine.Debug.LogError ("fileStream exception:" + e.$$anonymous$$essage);
         error = e.$$anonymous$$essage;
         if(fileStream != null){
             fileStream.Flush();
             fileStream.Close();
         }
     }
 }
 
 
 public bool GetNextBuffer(int kilobytes)
 {
     //try{
     byte[] buffer = new byte[kilobytes * 1024];
     
     if (bytesDownloaded < contentLength) 
     {
         if (networkStream.DataAvailable) 
         {
             read = networkStream.Read(buffer, 0, buffer.Length);
             bytesDownloaded += read;
             try{
                 fileStream.Write(buffer, 0, read);
             }                
             catch(Exception e){
                 UnityEngine.Debug.Log("Exception while writing file:" + e.$$anonymous$$essage);
                 error = e.$$anonymous$$essage;
                 if(fileStream!=null){
                     fileStream.Flush();
                     fileStream.Close();
                 }
                 
                 client.Close();
                 return true;
             }
         }
         progress = (float)((float)bytesDownloaded/(float)contentLength);
         //UnityEngine.Debug.Log ( "Downloaded: " + bytesDownloaded + " of " + contentLength + " bytes ..." );
         return false;
     }
     else
     {
         fileStream.Flush();
         fileStream.Close();
         
         client.Close();
         return true;
     }
     return true;
     //}
 }
 

}

avatar image 3d_Game_Ready · Oct 26, 2015 at 09:12 AM 0
Share

Ok, was able to add it in two code segments. This was adapted from code I found online. The buffer size is adjustable.

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

6 People are following this question.

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

Related Questions

Transfer closed with bytes remaining to read 1 Answer

Download and saving .txt from server to Android 1 Answer

Downloaded file consumes a large amount of memory iOS 1 Answer

Downloading images using www get pixelated 0 Answers

Minimize the memory uses while Downloading 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