/// <summary> /// 卸载场景。 /// </summary> /// <param name="sceneAssetName">场景资源名称。</param> /// <param name="unloadSceneCallbacks">卸载场景回调函数集。</param> /// <param name="userData">用户自定义数据。</param> public override void UnloadScene(string sceneAssetName, UnloadSceneCallbacks unloadSceneCallbacks, object userData) { #if UNITY_5_5_OR_NEWER if (gameObject.activeInHierarchy) { StartCoroutine(UnloadSceneCo(sceneAssetName, unloadSceneCallbacks, userData)); } else { SceneManager.UnloadSceneAsync(SceneComponent.GetSceneName(sceneAssetName)); } #else if (SceneManager.UnloadScene(SceneComponent.GetSceneName(sceneAssetName))) { if (unloadSceneCallbacks.UnloadSceneSuccessCallback != null) { unloadSceneCallbacks.UnloadSceneSuccessCallback(sceneAssetName, userData); } } else { if (unloadSceneCallbacks.UnloadSceneFailureCallback != null) { unloadSceneCallbacks.UnloadSceneFailureCallback(sceneAssetName, userData); } } #endif }
private IEnumerator UnloadSceneCo(string sceneAssetName, UnloadSceneCallbacks unloadSceneCallbacks, object userData) { AsyncOperation asyncOperation = SceneManager.UnloadSceneAsync(SceneComponent.GetSceneName(sceneAssetName)); if (asyncOperation == null) { yield break; } yield return(asyncOperation); if (asyncOperation.allowSceneActivation) { if (unloadSceneCallbacks.UnloadSceneSuccessCallback != null) { unloadSceneCallbacks.UnloadSceneSuccessCallback(sceneAssetName, userData); } } else { if (unloadSceneCallbacks.UnloadSceneFailureCallback != null) { unloadSceneCallbacks.UnloadSceneFailureCallback(sceneAssetName, userData); } } }
/// <summary> /// 异步加载场景。 /// </summary> /// <param name="sceneAssetName">要加载场景资源的名称。</param> /// <param name="priority">加载场景资源的优先级。</param> /// <param name="loadSceneCallbacks">加载场景回调函数集。</param> /// <param name="userData">用户自定义数据。</param> public void LoadScene(string sceneAssetName, int priority, LoadSceneCallbacks loadSceneCallbacks, object userData) { if (string.IsNullOrEmpty(sceneAssetName)) { Log.Error("Scene asset name is invalid."); return; } if (loadSceneCallbacks == null) { Log.Error("Load scene callbacks is invalid."); return; } #if UNITY_5_5_OR_NEWER AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneAssetName, LoadSceneMode.Additive); #else AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(SceneComponent.GetSceneName(sceneAssetName), LoadSceneMode.Additive); #endif if (asyncOperation == null) { return; } m_LoadSceneInfos.AddLast(new LoadSceneInfo(asyncOperation, sceneAssetName, priority, DateTime.Now, loadSceneCallbacks, userData)); }
/// <summary> /// 异步卸载场景。 /// </summary> /// <param name="sceneAssetName">要卸载场景资源的名称。</param> /// <param name="unloadSceneCallbacks">卸载场景回调函数集。</param> /// <param name="userData">用户自定义数据。</param> public void UnloadScene(string sceneAssetName, UnloadSceneCallbacks unloadSceneCallbacks, object userData) { if (string.IsNullOrEmpty(sceneAssetName)) { Log.Error("Scene asset name is invalid."); return; } if (unloadSceneCallbacks == null) { Log.Error("Unload scene callbacks is invalid."); return; } #if UNITY_5_5_OR_NEWER AsyncOperation asyncOperation = SceneManager.UnloadSceneAsync(sceneAssetName); if (asyncOperation == null) { return; } m_UnloadSceneInfos.AddLast(new UnloadSceneInfo(asyncOperation, sceneAssetName, unloadSceneCallbacks, userData)); #else if (SceneManager.UnloadScene(SceneComponent.GetSceneName(sceneAssetName))) { if (unloadSceneCallbacks.UnloadSceneSuccessCallback != null) { unloadSceneCallbacks.UnloadSceneSuccessCallback(sceneAssetName, userData); } } else { if (unloadSceneCallbacks.UnloadSceneFailureCallback != null) { unloadSceneCallbacks.UnloadSceneFailureCallback(sceneAssetName, userData); } } #endif }