Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Downloading files and save in local with StartCoroutine/delegate callback

Discussion in 'Scripting' started by kamihiro74, Oct 20, 2014.

  1. kamihiro74

    kamihiro74

    Joined:
    May 15, 2013
    Posts:
    20
    Hi, I have a questions about the startCoroutine, with the script I made, it works fine with iOS,
    but I am not sure it is a right way to use startcoroutine/ delegate. if you could give me comment about the followiing code it would be great.
    Thank you.


    Code (CSharp):
    1.  
    2. public delegate void OnComplete();
    3.  
    4. void Start(){
    5.  
    6. StartCoroutine(load(Callback));
    7. }
    8.  
    9. private IEnumerator load(OnComplete Callback) {
    10. WWWwww = newWWW(wwwURL);
    11. while(!www.isDone)
    12. {
    13.      yield return null;
    14. }
    15. if (www.error != null)
    16. throw newException("WWW download:" + www.error);
    17.  
    18. string path =createPathFromFilename();  //function to create local path
    19. System.IO.File.WriteAllBytes(path, www.bytes);
    20. iPhone.SetNoBackupFlag (path);
    21.  
    22. bool succeeded = isFileExists ();  //function to check the file exists
    23. if(!succeeded)
    24. throw newException("no file exit");
    25.  
    26.  
    27. www.Dispose();
    28. Callback();
    29.  
    30. }