Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

How can I load a scene asynchronously using addressables?

Discussion in 'Addressables' started by elaballaaaaaaa, Jul 6, 2018.

  1. elaballaaaaaaa

    elaballaaaaaaa

    Joined:
    Apr 6, 2018
    Posts:
    4
    In our game we've built our level editor so that each level has a corresponding scene, and I'm trying to get scene addressables to work. I've managed to add the scenes to the Default Local Group but when I try to load the scene asset, it fails. Here is the code I've written to try and load the scene asset:

    Code (CSharp):
    1. # sceneAsset is an AssetReference
    2. sceneAsset.LoadAsset<SceneAsset>().Completed += operation => { Debug.Log($"Loaded Scene: {operation.Result} in async operation {operation}"); };
    This will log out this text when run:

    Loaded Scene: in async operation UnityEngine.ResourceManagement.ChainOperation`2[UnityEditor.SceneAsset,System.Boolean] result = , status = Failed, Valid = True, canRelease = False​

    Any help in this manner would be greatly appreciated!
     
    MizzouVRLab likes this.
  2. elaballaaaaaaa

    elaballaaaaaaa

    Joined:
    Apr 6, 2018
    Posts:
    4
    Ah, of course shortly after posting this thread I discovered how to do it myself. Here is the code for anyone interested:

    Code (CSharp):
    1. Addressables.LoadScene(sceneAsset, LoadSceneMode.Additive);
     
  3. roshaantariq

    roshaantariq

    Joined:
    Dec 6, 2017
    Posts:
    14
    How to show the progress like the progress showing on UI in code below:

    Code (CSharp):
    1. private void OnLoadDone(IAsyncOperation<GameObject> async)
    2.     {
    3.         if (async.Status == AsyncOperationStatus.Failed)
    4.         {
    5.             Debug.LogException(async.OperationException);
    6.             report.text = "Failed to instantiate";
    7.         }
    8.  
    9.         else
    10.         {
    11.             report.text = string.Format("Loading: {0}%", async.PercentComplete);
    12.  
    13.             if (async.Status == AsyncOperationStatus.Succeeded)
    14.             {
    15.                 report.text = "Successfully loaded";
    16.                 myPlayerObject = async.Result;
    17.                 button2.SetActive(true);
    18.             }
    19.         }
    20.     }
    21.  
    22.     public void DownloadModel()
    23.     {
    24.         myPlayer.LoadAsset<GameObject>().Completed += OnLoadDone;
    25.     }
     
  4. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    119
    @roshaantariq use var op = myPlayer.LoadAsset<GameObject>();op.Completed +=onLoadDone;
    And e.g. in a coroutine while the async operation has not completed;
    Debug.Log(op.percentComplete);