Search Unity

Can anyone tell me HOW?

Discussion in 'iOS and tvOS' started by Falcondor, Feb 5, 2012.

  1. Falcondor

    Falcondor

    Joined:
    Oct 31, 2011
    Posts:
    14
    I tried to download and write a new file after deleting older one. This script downloads and writes perfectly on standalone but it doesnt download new file but it logs instantly old file's size "Debug.Log(www1.size.ToString());" on ipad2 and iphone4.

    I tried it with C# too, same result.
    with Application.temporaryCachePath, same result.
    .Net 2.0 is full, isnt subset.

    Code (csharp):
    1.  
    2. import System;
    3.  
    4. import System.IO;
    5.  
    6. var url1 = "http://www.myweb.net/myfile.mp4";    (.mp3 , .dat , .png ... tested file formats);
    7.  
    8. var www1 : WWW;
    9.  
    10. function Download1 () {
    11.  
    12.     if(www1!=null){www1=null; Debug.Log("NULL");}
    13.  
    14.     www1 = new WWW (url1);
    15.     yield www1;
    16.  
    17.     if(File.Exists(Application.persistentDataPath+"/myfile.mp4"))
    18.         {
    19.           File.Delete(Application.persistentDataPath+"/myfile.mp4");
    20.           if(!File.Exists(Application.persistentDataPath+"/myfile.mp4"))
    21.           {Debug.Log("DELETED");}
    22.         }
    23.     File.WriteAllBytes(Application.persistentDataPath+"/myfile.mp4", www1.bytes);
    24.  
    25.     Debug.Log(www1.size.ToString());
    26.  
    27. }
    28. function OnGUI () {
    29.  
    30.     if (GUI.Button (Rect (10,10,150,100), "Download1")) {
    31.  
    32.         Download1 ();
    33.  
    34.     }
    35.  
    36. }
    37.  


    Where is the problem and how can i fix that?

    Thanks.
     
    Last edited: Feb 5, 2012
  2. marjan

    marjan

    Joined:
    Jun 6, 2009
    Posts:
    563
    just a guess, i had this when saving "screenshots". There the capturing and writing was asynchronous. It took about 1 second to write the file.

    Thus, you could try someting with a delay or coroutine to check when the file actually got written, if at all.
     
  3. Falcondor

    Falcondor

    Joined:
    Oct 31, 2011
    Posts:
    14
    I tried download 2 files with coroutine. This works for bigger sized file now but lower sized file again old one.

    Code (csharp):
    1.  
    2.  
    3. void Start()
    4.     {
    5.         string myurl = "http://www.myweb.net/";
    6.         string myfile = "myFile";
    7.         StartCoroutine(Download(myurl, myfile + ".xml"));
    8.         StartCoroutine(Download(myurl, myfile + ".mp4"));
    9.     }
    10.    
    11.     IEnumerator Download(string url, string filename)
    12.     {
    13.         string remoteFile = url + "/" + filename;
    14.         string localFile = Application.persistentDataPath + "/" + filename;
    15.  
    16.         if (System.IO.File.Exists(localFile))
    17.         {
    18.             System.IO.File.Delete(localFile);
    19.         }
    20.  
    21.         WWW wwwfile = new WWW(remoteFile);
    22.         yield return wwwfile;
    23.  
    24.         System.IO.File.WriteAllBytes(localFile, wwwfile.bytes);
    25.  
    26.         if (System.IO.File.Exists(localFile))
    27.         {
    28.             Debug.Log(" file does exist");
    29.         }
    30.         else
    31.         {
    32.             Debug.Log(" file does not exist");
    33.         }
    34.        
    35.         Debug.Log(wwwfile.size.ToString());
    36.     }
     
    Last edited: Feb 5, 2012
  4. marjan

    marjan

    Joined:
    Jun 6, 2009
    Posts:
    563
    That's not what I meant. You should have delay after download and after write.
    For simplicity sake make something like waitforseconds(5) after you start writing.

    When I did my screenshot stuff, I used only one file name "screenshot.png". After saving I wanted it to show up in an email composer immediately. But after 1. Save there was of course always such a file. And in the mail composer the last shot was loaded, not the one I just took. So in the end I did this:

    1. Check if there is a file. If there is, delete it.
    2. Make the new shot and save. This is asynchrounous.
    3. Start a coroutine which checks for the file every 0.5 seconds.
    4. At some time the new image should be written, coroutine will find it now, and start the mail composer things.