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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

LoadSceneAsync from AssetBundle?

Discussion in 'Scripting' started by young-xyz, Nov 19, 2017.

  1. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    51
    I'm trying to load a scene from asset bundle with progress, So I used this code

    Code (CSharp):
    1. void Start()
    2. {
    3.  AssetBundle.LoadFromFile("bundleName");
    4.  StartCoroutine(SceneLoad("sceneName"));
    5. }
    6.  
    7. IEnumerator SceneLoad(string sceneName)
    8. {
    9.  AsyncOperation op = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
    10.  op.allowSceneActivation = false;
    11.  int count = 0;
    12.  while (op.progress < 0.8)
    13.  {
    14.   Debug.Log(count++);
    15.   yield return null;
    16.  }
    17.  op.allowSceneActivation = true;
    18. }
    but it has only progress 0, and many errors that

    and finished successfully.

    So the progress is only 0 and 0.9.
    What's the problem?
     
    Last edited: Nov 19, 2017
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Is the name of your bundel "bundleName"?
    Is the name of your scene "sceneName"?
    Because that's what you have in your Start function.
     
  3. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    51
    Thanks for reply.
    It is a sample name, of course.
    I think the name is not a problem, because next scene is loaded successfully.
     
    Last edited: Jul 12, 2018
    mowax74 likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    You are just async-loading a scene called sceneName (directly from your available scenes) and you are doing nothing with the actual asset bundle you loaded on line 3.

    Instead you want to get an AssetBundle object out of your AssetBundle.LoadFromFile() call ( which can also be async!), and then use the .LoadAssetAsync<Scene>() method on that asset bundle instance that you have gotten back.

    Check these docs: https://docs.unity3d.com/ScriptReference/AssetBundle.LoadFromFileAsync.html
     
    xVergilx likes this.
  5. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    51
    Hi Kurt,
    I've changed line 9 as you said,
    Code (CSharp):
    1. AssetBundleRequest op = bundle.LoadAssetAsync<Scene>(sceneName);
    but it is not worked but has error like this
    Is there something I have to do additionally?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
  7. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    51
    Thanks Kurt, but it is not the point of my problem.
    Scene is loaded with original code, but the progress count is not work.
    Debug message shows 0 first, followed by many assertion error messages as below(all same messages).
    error.png

    After that, next scene "sceneName" is loaded successfully.

    What is the error message means and what to do to make it work?
     
    Last edited: Nov 20, 2017
  8. RivenWork

    RivenWork

    Joined:
    Nov 11, 2015
    Posts:
    10
    hi, has this been resolved?
     
  9. Red-Owl-Games

    Red-Owl-Games

    Joined:
    Feb 4, 2015
    Posts:
    28
    I don't know for sure, but I struggled with the same 'assertion failed' error and managed to solve it by upgrading to a newer unity version.
    (see https://issuetracker.unity3d.com/is...-with-loadasync-throws-assertion-failed-error )

    As the url title says, there's a bug that throws an 'assertion failed' error when trying to load an asset that has an animator component on it. There is no problem loading it, but it spits out those errors.

    So my guess is that the scene you're loading contains an asset that has an animator component on it.

    The 'assertion failed' error has been solved in Unity 2018.1

    But this is only about the 'assertion failed' error.