Search Unity

preload scene

Discussion in 'Addressables' started by cemleme, Apr 4, 2020.

  1. cemleme

    cemleme

    Joined:
    Mar 24, 2017
    Posts:
    30
    Hello,

    I am trying to preload a large scene using addressables and show this on a loading scene.

    unity 2019.3.7f1
    addressables 1.7.5​
    • my scene is a local asset
    • I have assigned the scene as an addressable.
    • The download size always returns 0
    • asyncHandle.PercentComplete returns either 0 or 1
    • testing it on play mode.

      I suppose it shows this result because of the editor play mode?
    how can I test this on play mode? do I need to test this on a build?

    I use this code to load it

    Code (CSharp):
    1.    
    2.  
    3.     public AssetReference level;
    4.     float downloadSize;
    5.     AsyncOperationHandle asyncHandle;
    6.  
    7.     void Awake()
    8.     {
    9.         if (Level.RuntimeKeyIsValid())
    10.             Addressables.GetDownloadSizeAsync(level).Completed += GetDownloadSize_Completed;
    11.     }
    12.  
    13.     void GetDownloadSize_Completed(AsyncOperationHandle<long> obj)
    14.     {
    15.         if (obj.IsValid())
    16.         {
    17.             downloadSize = obj.Result;
    18.  
    19.             Debug.Log("->download size: " +downloadSize);
    20.  
    21.             asyncHandle = Addressables.DownloadDependenciesAsync(level);
    22.             asyncHandle.Completed += Preloader_Completed;
    23.         }
    24.     }
    25.  
    26.     void Preloader_Completed(AsyncOperationHandle obj)
    27.     {
    28.         if (obj.IsValid())
    29.         {
    30.             if (obj.Result != null)
    31.             {
    32.                 Addressables.LoadSceneAsync(level);
    33.             }
    34.         }
    35.     }
    36.  
    37.     void Update()
    38.     {
    39.         if (asyncHandle.IsValid())
    40.         {
    41.             Debug.Log("%" + asyncHandle.PercentComplete);
    42.             Debug.Log(asyncHandle.PercentComplete * downloadSize);
    43.         }
    44.     }
    Thanks
     
  2. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    586
    I was curious about this, so I dug into their code to see how they do it. I found that they are using LoadSceneAsync under the hood, and the AsyncOperation returned by that should report its progress. Try loading your scene directly like that in editor and see what progress you get from it. Then you can tell if it's an issue with Addressables or the Editor SceneManager.

     return UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(path, new LoadSceneParameters() { loadSceneMode = mode });
     
  3. SpiralCircus

    SpiralCircus

    Joined:
    Feb 1, 2014
    Posts:
    34
    To make the editor behave as close to the built player as possible you would have to build you addressable groups then select "use existing build" as the play mode script. This will use the built addressables data instead of the unity editor's standard assetdatabase setup.

    Bear in mind that changes to your scenes won't show up in play mode if you dont rebuild the addressables