public void LoadSceneFromBundle(string sceneName) { BundleData bundleData = bundleDb.Database.bundleData.Find((bd) => { return(bd.sceneName == sceneName); }); if (bundleData == null) { Debug.LogError("Scene not found!" + sceneName); } StartCoroutine(LoadSceneCoroutine(bundleData)); }
private IEnumerator LoadSceneCoroutine(BundleData data) { if (loadingScene) { yield break; } loadingScene = true; // unload current scene if (currentSceneName.Length > 0) { AsyncOperation ao = null; try { ao = SceneManager.UnloadSceneAsync(currentSceneName); } catch { Debug.LogWarning(currentSceneName + " could not be unloaded."); } if (ao != null) { yield return(new WaitUntil(() => ao.isDone)); } } // if new scene in other bundle, unload current scene bundle List <string> keys = new List <string>(loadedDependencies.Keys); keys.ForEach((k) => { if (!data.dependencies.Contains(k)) { AssetBundle toUnload; loadedDependencies.TryGetValue(k, out toUnload); toUnload.Unload(true); } }); // unload unnecessary dependencies and/or bundle with the current scene if (loadedSceneBundleName != data.sceneBundleName) { if (loadedSceneBundle != null) { loadedSceneBundle.Unload(true); loadedSceneBundleName = ""; } } // load necessary dependencies data.dependencies.ForEach((d) => { if (!loadedDependencies.ContainsKey(d)) { AssetBundle toLoad = AssetBundle.LoadFromFile(Configuration.Data.bundlesPath + d); loadedDependencies.Add(d, toLoad); } }); // load scene bundle if (loadedSceneBundle == null) { loadedSceneBundle = AssetBundle.LoadFromFile(Configuration.Data.bundlesPath + data.sceneBundleName); loadedSceneBundleName = data.sceneBundleName; } // load scene SceneManager.LoadScene(data.sceneName, LoadSceneMode.Additive); currentSceneName = data.sceneName; loadingScene = false; }