Search Unity

Is WWW.LoadFromCacheOrDownload() a synchronous function or asynchronous function ?

Discussion in 'Scripting' started by Usmith, Apr 7, 2013.

  1. Usmith

    Usmith

    Joined:
    Apr 7, 2013
    Posts:
    20
    Is WWW.LoadFromCacheOrDownload() a synchronous function or asynchronous function when it loading asset bundle from cache?

    I have a test code :
    Code (csharp):
    1.  
    2. www = WWW.LoadFromCacheOrDownload(path, DEFAULT_VERSION);//path with version is cached in advance.
    3. Debug.Log("www.isDone: " +www.isDone);//outputs: www.isDone:False
    4. AssetBundle ab = www.assetBundle;//But if I break the code here, www.isDone = Ture
    5.  
    6.  
    That drives me crazy!!
     
    Last edited: Apr 7, 2013
  2. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    Async. It instantiates the WWW, and when WWW is instantiated, it makes a call to the server. With the exception that using this method checks the cache and downloads from there if available.

    You should always treat your WWW calls asynchronously, by using either:

    1. coroutines (with yield return)
    2. checking www.isDone within your Update method
    3. using a lib like eDriven which enables writing your code in the callback (non-blocking) fashion

    The way you use the 3rd option is:

    Code (csharp):
    1. private readonly HttpConnector _httpConnector = new HttpConnector();
    2. // in your Start() method:
    3. _httpConnector.Send(
    4.     _yourUrl,
    5.     delegate (object data)
    6.     {
    7.         WWW w = (WWW)data;
    8.         /// w.isDone is true. Process data...
    9.     }
    10. );