Search Unity

Question Scene Download Folder

Discussion in 'Addressables' started by dfarjoun, Jun 5, 2020.

  1. dfarjoun

    dfarjoun

    Joined:
    Aug 6, 2017
    Posts:
    42
    Hi,

    I'm starting with addressables and started with the code from Unity's Course.

    I than build everything and downloaded the scenes.
    I could see the new folder com.myappname... in the project's main folder.

    I deleted the first time.

    I did more testings, generated new build, uploaded to my server the bundles, but after download, I couldn't find the directory.

    After this, I decided to implement the download progress indicator with this code:


    Code (CSharp):
    1. private void SceneLoadCompleted(AsyncOperationHandle<SceneInstance> obj)
    2.         {
    3.         //When the load is still in progress, output the Text and progress bar
    4.         while (!obj.IsDone)
    5.         {
    6.             //Output the current progress
    7.             responseText.text = "Loading progress: " + (obj.PercentComplete * 100) + "%";
    8.         }
    9.  
    10.  
    11.             if (obj.Status == AsyncOperationStatus.Succeeded)
    12.              {
    13.             //set our reference to the AsyncOperationHandle
    14.  
    15.             Debug.Log(obj.Result.Scene.name + " sucessfully loaded.");
    16.             //optional - do more stuff
    17.  
    18.             //in your delegate, assign the reference to point to the AsyncOperatoinHandle. in the previous section it was named obj.
    19.             //add the following line in place of or after "do more stuff".
    20.             handle = obj;
    21.              }
    22.  
    23.         }

    But now I can't see the progress indicator.
    Or because the code is wrong.
    Or because I've already previously downloaded the files (and I don't know where to delete them).

    I'm assuming it's the second option because it's loading the scenes as they were locally based, even if I checked the option PLAY MODE SCRIPT: Use existing Build.

    Is there something I can do here to see if the progress indicator will work?
    Or just deleting everything and uploading again the scenes?

    Thanks in advance for your help! :)
     
  2. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    586
    Looks like you need a
    yield return null
    inside your while loop so that it will update the progress once per frame instead of locking the frame until the load is complete (I'm surprised your application didn't just lock up completely). Then you'll need to change the function return type to
    IEnumerator
    and call
    StartCoroutine()
    on the enumerator returned by the function.

    [EDIT] Oh I see the function name says Completed, so I'm guessing you're adding that to the completed event callback. Instead what you should do it pass the handle to this function immediately instead of adding the callback.
     
  3. dfarjoun

    dfarjoun

    Joined:
    Aug 6, 2017
    Posts:
    42
    Hi,
    Thanks for the feedback.
    I was following Unity's Premium content lesson and they didn't use IEnumerator.

    I ended up with this, then:

    Code (CSharp):
    1. public void LoadScene(int index)
    2.     {
    3.         StartCoroutine(LoadAsyncScene(index));
    4.     }
    5.  
    6.  
    7.     IEnumerator LoadAsyncScene(int sceneIndex)
    8.     {
    9.  
    10.  
    11.         //use LoadSceneMode.Additive to merge scenes
    12.         Addressables.LoadSceneAsync(scene[sceneIndex], LoadSceneMode.Single).Completed += SceneLoadCompleted;
    13.  
    14.  
    15.         //AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneIndex);
    16.  
    17.         var asyncLoad = Addressables.LoadSceneAsync(scene[sceneIndex], LoadSceneMode.Single).PercentComplete;
    18.  
    19.         while (!Addressables.LoadSceneAsync(scene[sceneIndex], LoadSceneMode.Single).IsDone)
    20.         {
    21.             //Output the current progress
    22.             responseText.text = "Loading progress: " + (asyncLoad) + "%";
    23.             LoadProgressSlider.value = asyncLoad / 100;
    24.             yield return null;
    25.         }
    26.  
    27.  
    28.     }
    29.  
    30.  
    31.     private void SceneLoadCompleted(AsyncOperationHandle<SceneInstance> obj)
    32.     {
    33.         if (obj.Status == AsyncOperationStatus.Succeeded)
    34.         {
    35.             //set our reference to the AsyncOperationHandle
    36.  
    37.             Debug.Log(obj.Result.Scene.name + " sucessfully loaded.");
    38.             //optional - do more stuff
    39.  
    40.             //in your delegate, assign the reference to point to the AsyncOperationHandle. in the previous section it was named obj.
    41.             //add the following line in place of or after "do more stuff".
    42.             handle = obj;
    43.         }
    44.  
    45.     }

    But the loading didn't go out from 1%.

    Anything wrong that you can spot there?
     
    Last edited: Jun 5, 2020