private IEnumerator UnloadSceneCore(SceneInstance sceneInstance) { var scene = sceneInstance.GetScene(); if (!scene.HasValue) { yield break; } if (SceneManager.sceneCount <= 1) { yield break; } UnityAction <Scene> sceneUnloaded = s => { if (s.IsValid()) { if (sceneInstance.Identifier.HasValue) { var identifier = sceneInstance.Identifier.Value; if (loadedscenes.ContainsKey(identifier)) { loadedscenes.Remove(identifier); } } if (cacheScenes.Contains(sceneInstance)) { cacheScenes.Remove(sceneInstance); } if (onUnloadScene != null) { onUnloadScene.OnNext(sceneInstance); } } }; var rootObjects = scene.Value.GetRootGameObjects(); foreach (var rootObject in rootObjects) { var targets = UnityUtility.FindObjectsOfInterface <ISceneEvent>(rootObject); foreach (var target in targets) { yield return(target.OnUnloadSceneAsObservable().ToYieldInstruction()); } } AsyncOperation op = null; try { SceneManager.sceneUnloaded += sceneUnloaded; op = SceneManager.UnloadSceneAsync(scene.Value); } catch (Exception e) { SceneManager.sceneUnloaded -= sceneUnloaded; Debug.LogException(e); if (onUnloadError != null) { onUnloadError.OnNext(Unit.Default); } yield break; } while (!op.isDone) { yield return(op); } SceneManager.sceneUnloaded -= sceneUnloaded; if (onUnloadSceneComplete != null) { onUnloadSceneComplete.OnNext(sceneInstance); } }