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

LoadSceneAsync takes too much time after loaded bundles

Discussion in 'Asset Bundles' started by georgenda182, Oct 1, 2018.

  1. georgenda182

    georgenda182

    Joined:
    May 20, 2017
    Posts:
    8
    Hi, everyone.

    I am loading some asset bundles and all their assets to, then, load asynchronously the scene that depends on these assets, but the loading time of the scene is the same than if I load the scene directly. Here's is my code:
    Code (CSharp):
    1. private void Start()
    2. {
    3.     Application.backgroundLoadingPriority = ThreadPriority.High;
    4.     m_levelInfo = m_gameStats.m_currentLevelsOrderLine.GetNextLevelToLoad();
    5.     LoadNextLevelBundles();
    6. }
    7.  
    8. private void LoadNextLevelBundles()
    9. {
    10.    m_initialTime = Time.time;
    11.    float initialTime = Time.time;
    12.    AssetBundle assetBundle;
    13.  
    14.    for (int i = 0; i < m_levelInfo.m_bundlesToLoad.Count; i++)
    15.    {
    16.       AssetBundleCreateRequest bundleLoading =
    17.           LoadAssetBundleAsync(m_levelInfo.m_bundlesToLoad[i]);
    18.       bundleLoading.completed += delegate(AsyncOperation operation)
    19.       {
    20.          assetBundle = bundleLoading.assetBundle;
    21.          assetBundle.LoadAllAssetsAsync().completed +=
    22.              delegate(AsyncOperation asyncOperation)
    23.              {
    24.                 TryLoadNextSceneLevel();
    25.              };
    26.       };
    27.    }
    28. }
    29.  
    30. private void TryLoadNextSceneLevel()
    31. {
    32.    m_levelBundlesLoaded++;
    33.    if (m_levelBundlesLoaded >= m_levelInfo.m_bundlesToLoad.Count)
    34.    {
    35.       SceneManager.LoadSceneAsync(m_levelInfo.m_levelName);
    36.    }
    37. }
    38.  
    39. private AssetBundleCreateRequest LoadAssetBundleAsync(string bundleName)
    40. {
    41.    byte[] fileBytes = null;
    42.    string path = Path.Combine(Application.streamingAssetsPath, bundleName);
    43.    m_initialTime = Time.time;
    44.  
    45.    fileBytes = File.ReadAllBytes(path);
    46.  
    47.    AssetBundleCreateRequest bundleLoading =
    48.        AssetBundle.LoadFromMemoryAsync(fileBytes, 0);
    49.    return bundleLoading;
    50. }
    "LoadScene" and "LoadSceneAsync" ignore if assets are already loaded in memory? Am I doing it right or it does what it has to do?
     
  2. georgenda182

    georgenda182

    Joined:
    May 20, 2017
    Posts:
    8
    Ok, I think I just realized I was wrong at the very first moment... Are the assetbundles a tool to optimize loading times?