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. Dismiss Notice

Loading Scene async issue

Discussion in 'Scripting' started by LiableDuke, Mar 26, 2021.

  1. LiableDuke

    LiableDuke

    Joined:
    Feb 8, 2018
    Posts:
    32
    I'm having an issue when I load my scene async the start method get called before the scene transitions using that start method a list of asset bundles are async downloaded this works in the editor but for some reason when I build it everything even the asset bundles get downloaded before the scene transitions. I have no clue why this is happening

    This part of the scene loader the scene loader is a dont destroy on load

    Code (CSharp):
    1. public void LoadScene(string BundleName, string SceneName = ""){
    2.  
    3.         #if UNITY_EDITOR
    4.         if(!LoadScenesFromAssetBundles && SceneName != ""){
    5.             Debug.Log("LoadSceneAsync: " + SceneName);
    6.             SceneManager.LoadSceneAsync(SceneName);
    7.             return;
    8.         }
    9.         #endif
    10.  
    11.         LoadingScreenBehaviour.current.ShowLoading();
    12.         // Assign event triggers
    13.         AssetBundleLoader.current.OnBundleLoaded += onSceneBundleLoaded;
    14.         AssetBundleLoader.current.OnLoadingFailed += onSceneBundleLoadingError;
    15.  
    16.         Debug.Log("try to load assetbundle: " + BundleName);
    17.         AssetBundleLoader.current.LoadAssetBundle(BundleName);
    18.     }
    19.  
    20.     void onSceneBundleLoaded(AssetBundle loadedBundle){
    21.         // Remove event listeners
    22.         AssetBundleLoader.current.OnBundleLoaded -= onSceneBundleLoaded;
    23.         AssetBundleLoader.current.OnLoadingFailed -= onSceneBundleLoadingError;
    24.  
    25.         AppManager.current.ResetBackInformation();
    26.  
    27.         if(loadedBundle.name == "paxis_scene"){
    28.             AnalyticsEvent.Custom("app_open", new Dictionary<string, object>{
    29.                     { "app_name", "Paxis" }
    30.                 });
    31.         } else if(loadedBundle.name == "tj_scene"){
    32.             AnalyticsEvent.Custom("app_open", new Dictionary<string, object>{
    33.                     { "app_name", "Tussock Jumper" }
    34.                 });
    35.         } else if(loadedBundle.name == "photobooth"){
    36.             AnalyticsEvent.Custom("app_open", new Dictionary<string, object>{
    37.                     { "app_name", "PhotoBooth" }
    38.                 });
    39.         }
    40.  
    41.         string[] scenePath = loadedBundle.GetAllScenePaths();
    42.          Debug.Log("Loading scene async with  ScenePath: " + scenePath[0]); // -> "Assets/scene.unity"
    43.         SceneManager.LoadSceneAsync(System.IO.Path.GetFileNameWithoutExtension(scenePath[0]));
    44.     }
    this is part of the code is in the new scene that is being loaded

    Code (CSharp):
    1. private void Start()
    2.     {
    3.         Setup();
    4.     }
    5.  
    6. private void Setup()
    7.     {
    8.         if (DorukEker.WineForce.Umbrella.CountryManager.current == null) return;
    9.  
    10.         wineAvailability = new List<WineAvailabilityItem>();
    11.  
    12.         _selectedCountry = DorukEker.WineForce.Umbrella.CountryManager.current.selectedCountry;
    13.  
    14.         // Check if there is already wine availability list in the player pref
    15.         string wineAvailabilityFromPlayerPref = "";
    16.         wineAvailabilityFromPlayerPref = PlayerPrefs.GetString(WINE_AVAILABILITY_KEY, "");
    17.         if (wineAvailabilityFromPlayerPref != "")
    18.         {
    19.             // Exist in the player pref
    20.             // Set priv var
    21.             wineAvailability = WineAvailabilityStringToList(wineAvailabilityFromPlayerPref);
    22.             // update vuforia markers
    23.             UpdateVuforiaMarkersForCurrentCountry();
    24.         }
    25.         else
    26.         {
    27.             UpdateFromRemote();
    28.         }
    29.  
    30.         // Start listening for the remote setting update
    31.         RemoteSettings.Updated += new RemoteSettings.UpdatedEventHandler(UpdateFromRemote);
    32.     }
    please not this is not my code this is an existing app im working on and the code is all over the place
     
    Last edited: Mar 26, 2021
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Without more info, we can't really help you. Code is always helpful for us to direct what you need to do.
    The editor isn't always a perfect indication of how things will work, especially when it deals with timing.
     
  3. LiableDuke

    LiableDuke

    Joined:
    Feb 8, 2018
    Posts:
    32
    Updated
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Ok, so explain to me a bit more about what you expect to happen? It sounds like you want the scene to transition and then stuff should happen. If that is the case, don't have these in scene 1. Have the stuff you need to run after transition in scene 2.

    A few things to keep in mind. The order of operations for Starts isn't guaranteed. So if in editor, script 1 runs and then script 2 runs, it may be the other way in a build with script 2 running first.

    Next in your scripts I see #if UNITY_EDITOR
    The code within that code block will only run in the editor and not in a build, so keep that in mind for another reason the editor might behave differently. This may be your issue. It might be hitting the code block, loading the next scene, but then it also returns. In a build, it loads the assetbundles but then hits the scene load. I'm only taking guesses based on your description and what code you provided. An easy way to test if this code block is creating issues in editor is just to comment it out and then hit play and see if you get the same behavior as a build.

    Adding debug.log messages may help you see the order stuff is executing.