Search Unity

Read/Write Cache IOS, Iphone AssetBundle Problem

Discussion in 'iOS and tvOS' started by tigershan, May 2, 2011.

  1. tigershan

    tigershan

    Joined:
    Jul 29, 2010
    Posts:
    73
    Hi, I can write to the cache and download the assetbundle just fine, but whenever I tried to read cache from ios document it doesn't seem to work(and I checked the organizer, so the file is actually been saved into the documents folder), however second time I click on the download asset button again, it goes and download from internet again....



    Here is the code:

    Code (csharp):
    1.  
    2.  
    3. void OnGUI() {
    4.         GUILayout.Space(0.5f);
    5.         GUILayout.BeginHorizontal();
    6.        
    7.        
    8.        
    9.        
    10.        
    11.         if (GUILayout.Button("Download Asset"))
    12.             StartDownload();   
    13.        
    14.         if (internetDownload != null  !Downloaded)
    15.         {
    16.             if (internetDownload.error == null)
    17.             {
    18.                 int progress = (int)(internetDownload.progress * 100);
    19.                 GUILayout.Label(progress + "%");   
    20.                
    21.                
    22.                 if (internetDownload.isDone  GUILayout.Button("Save Resource")  )
    23.                 {
    24.                     //GUILayout.BeginHorizontal();
    25.                     Debug.Log("Saving resource to cache");
    26.                    
    27.                    
    28.                     //GUILayout.EndHorizontal();
    29.                    
    30.                     FileStream cache = new System.IO.FileStream(GetiPhoneDocumentsPath() + PackageName, System.IO.FileMode.Create);
    31.                     cache.Write(internetDownload.bytes, 0, internetDownload.bytes.Length);
    32.                     cache.Close();
    33.                     Debug.Log("Cache saved: " + GetiPhoneDocumentsPath()+ PackageName);
    34.                        
    35.                 }
    36.             }
    37.             else
    38.             {
    39.                 GUILayout.Label(internetDownload.error);       
    40.             }  
    41.         }
    42.         GUILayout.EndHorizontal();
    43.  
    44. }
    45.  
    46.  
    47. private void StartDownload() {
    48.        
    49.        
    50.        
    51.         cachedDownload = new WWW ("file://" + GetiPhoneDocumentsPath() + PackageName); // I tried both file:/ and file://
    52.        
    53.        
    54.         StartCoroutine(WaitLoading(cachedDownload));
    55.        
    56.        
    57.         assetBundle = cachedDownload.assetBundle;
    58.        
    59.        
    60.         if(assetBundle == null)   // keep saying i have nothing....
    61.         {
    62.            Debug.Log("reads nothing from cache");  
    63.            
    64.         }
    65.        
    66.         Debug.Log("file://" + GetiPhoneDocumentsPath() + PackageName);
    67.        
    68.        
    69.         if(assetBundle != nullß) // current file exsit on cache
    70.         {
    71.             Debug.Log("current file exist on cache");
    72.             Downloaded = true;
    73.            
    74.            
    75.            
    76.             if (assetBundle != null)
    77.             {
    78.             // Alternatively you can also load an asset by name (assetBundle.Load("my asset name"))
    79.        
    80.                 Object go = assetBundle.mainAsset;
    81.            
    82.                 if (go != null)
    83.                 {
    84.                     instanced = Instantiate(go, someObject.transform.position, someObject.transform.rotation);
    85.                 }
    86.                 else
    87.                 {
    88.                     Debug.Log("Couldnt not instantiate object");                   
    89.                 }  
    90.             }
    91.             else
    92.             {
    93.                 Debug.Log("Couldnt load resource");
    94.             }
    95.  
    96.            
    97.            
    98.            
    99.         }
    100.         else
    101.         {
    102.             Debug.Log("downloading it from internet");
    103.             Downloaded = false;
    104.                
    105.             internetDownload = new WWW (urlString + PackageName);
    106.            
    107.             StartCoroutine(WaitLoading(internetDownload));
    108.            
    109.             assetBundle = internetDownload.assetBundle;
    110.         }
    111.  
    112.  
    113.    
    114.     }
    115.  
    116.  
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    well your code is wrong, you lack the yield return cachedDownload to wait that its done, so it will never be != null (www is always async even against the current file system and it will never return immediately with the downloaded data)
     
  3. tigershan

    tigershan

    Joined:
    Jul 29, 2010
    Posts:
    73
    StartCoroutine(WaitLoading(cachedDownload));


    the coroutine has the yield, i forgot to put it in there...


    public IEnumerator WaitLoading(WWW download) {
    yield return download;
    }
     
  4. Mantas-Puida

    Mantas-Puida

    Joined:
    Nov 13, 2008
    Posts:
    1,864
    Lines going after "StartCoroutine" will be executed immediately, because "StartCoroutine" is supposed to launch "pseudo parallel" routine and if this "parallel routine| waits for something it doesn't suspend execution of the parent routine.