Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Downloading multiple files (coroutine, yield)

Discussion in 'Scripting' started by zingfharn, Feb 19, 2016.

Thread Status:
Not open for further replies.
  1. zingfharn

    zingfharn

    Joined:
    Feb 19, 2016
    Posts:
    2
    Hello there! I'm new to Unity (picked it up a couple of days ago). I've programmed a lot in the past, so I've come on quite quickly, and c# is actually pretty nice!

    I've got a database of content with metadata (thumbs, title, etc) aand written a ghetto api to provide those via the web. I want to cache all the metadata, and pulldown the main files on the fly as the user selects them. I can call the APIs fine, but I'm wondering how I download stuff in serial. I don't fully grasp how coroutine and yield interact, and I'm loathe to set up blocking stuff before I understand more.

    Here's the pseudocode:

    GetIDsFromServer() (returns array of IDs)
    IterateThroughIDsAndCheckStatus
    - (returns int 0, 1, 2 - 0 is failed, 1 is locally cached, 2 is remotely requested).
    - if it's cached, wahoo, skip
    - if it's remote, pull down the data
    RenderThumbnailsInNiceScrollableList()


    I've got all the bits and pieces working in isolation but I'm now wondering exactly how I request the metadata in serial. Essentially, I want to a status bar with something like "Contacting Server.." then "Found 7 new Scenes.." then "Downloading 1/7", 2/7, etc.

    I discovered nested coroutines which seems to solve my problem, but I don't understand how to make IterateThroughIDsAndCheckStatus return an int. It seems if I want to nest coroutines, IterateThroughIDsAndCheckStatus needs to be an IENumerator and therefore can't return an int? Or did I miss something entirely obvious?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
  3. teaserss

    teaserss

    Joined:
    Feb 20, 2016
    Posts:
    1
  4. zingfharn

    zingfharn

    Joined:
    Feb 19, 2016
    Posts:
    2
    Thank you very much. In the end, I just set up some conditions in Update() (downloadActive, numberOfDownloads) that tracked the status and started the next download, displaying progress to the user. It's a dirty kludge, but it works.
     
    LiterallyJeff likes this.
  5. Garrik

    Garrik

    Joined:
    Dec 30, 2018
    Posts:
    1
    Thank you very much. Very valuable information!
     
    Last edited: Dec 30, 2018
  6. jpgordon00

    jpgordon00

    Joined:
    Jan 9, 2021
    Posts:
    25
    _fatalix3d_ likes this.
  7. samtcs

    samtcs

    Joined:
    Dec 14, 2022
    Posts:
    3
    Error :

    IndexOutOfRangeException: Index was outside the bounds of the array.
    UFD.UnityFileDownloader._Dispatch () (at Assets/UnityFileDownloader-main/UnityFileDownloader.cs:258)
    UFD.UnityFileDownloader.Download () (at Assets/UnityFileDownloader-main/UnityFileDownloader.cs:200)
    NewBehaviourScript.Download () (at Assets/NewBehaviourScript.cs:37)
    System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) (at <c9d3ffd4b98649ee9989e1908eaca019>:0)
    UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at <c399efc08928422d8828d74a2b19ffba>:0)
    UnityEngine.UnitySynchronizationContext.Exec () (at <c399efc08928422d8828d74a2b19ffba>:0)
    UnityEngine.UnitySynchronizationContext.ExecuteTasks () (at <c399efc08928422d8828d74a2b19ffba>:0)



    Hi i tried to use your code and used sample code to download but i am getting this error. Kindly Support
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    This error requires no support and requires no forum posting.

    You are the only person who can fix this error, and it is ALWAYS the same steps. ALWAYS.

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that
     
  9. samtcs

    samtcs

    Joined:
    Dec 14, 2022
    Posts:
    3
    And i have to give credentials to get access to the files. How to do?
     
  10. jpgordon00

    jpgordon00

    Joined:
    Jan 9, 2021
    Posts:
    25
    I actually fixed the issue @samtcs. It was an error relating to code that I released under my GitHub - so thanks for helping me improve it! :)
     
    samtcs likes this.
Thread Status:
Not open for further replies.