public class SceneManagerTool
{
private static string previousSceneName;
public static async UniTask LoadSceneAsync(string sceneName, System.Action onLoadComplete = null, LoadSceneMode loadSceneMode = LoadSceneMode.Additive)
{
await LoadSceneAsyncCoroutine(sceneName, onLoadComplete, loadSceneMode);
}
public static async UniTask LoadSceneAsyncCoroutine(string sceneName, System.Action onLoadComplete = null, LoadSceneMode loadSceneMode = LoadSceneMode.Additive)
{
var asyncOperation = SceneManager.LoadSceneAsync(sceneName, loadSceneMode);
asyncOperation.allowSceneActivation = false; // 禁止自动激活场景
while (!asyncOperation.isDone)
{
float progress = Mathf.Clamp01(asyncOperation.progress / 0.9f); // 进度范围从0到1
// 在这里更新加载进度,例如显示进度条或加载界面
if (progress >= 1.0f)
{
asyncOperation.allowSceneActivation = true; // 当进度达到100%时,允许激活场景
}
await UniTask.Yield();
}
await UniTask.SwitchToMainThread();
if (onLoadComplete != null)
{
onLoadComplete.Invoke();
}
}
private static IEnumerator LoadSceneAsyncCoroutine(int sceneBuildIndex, System.Action onLoadComplete, LoadSceneMode loadSceneMode)
{
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneBuildIndex, loadSceneMode);
previousSceneName = SceneManager.GetActiveScene().name;
while (!asyncOperation.isDone)
{
yield return null;
}
if (onLoadComplete != null)
{
onLoadComplete.Invoke();
}
}
public static void LoadScene(int sceneBuildIndex, System.Action onLoadComplete = null, LoadSceneMode loadSceneMode = LoadSceneMode.Single)
{
SceneManager.LoadScene(sceneBuildIndex, loadSceneMode);
previousSceneName = SceneManager.GetActiveScene().name;
if (onLoadComplete != null)
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
void OnSceneLoaded(UnityEngine.SceneManagement.Scene scene, LoadSceneMode mode )
{
if (scene.buildIndex == sceneBuildIndex)
{
onLoadComplete.Invoke();
SceneManager.sceneLoaded -= OnSceneLoaded;
}
}
}
public static void LoadScene(string sceneName, System.Action onLoadComplete = null, LoadSceneMode loadSceneMode = LoadSceneMode.Single)
{
SceneManager.LoadScene(sceneName, loadSceneMode);
previousSceneName = SceneManager.GetActiveScene().name;
if (onLoadComplete != null)
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
void OnSceneLoaded(UnityEngine.SceneManagement.Scene scene, LoadSceneMode mode )
{
if (scene.name == sceneName)
{
onLoadComplete.Invoke();
SceneManager.sceneLoaded -= OnSceneLoaded;
}
}
}
public static void UnloadScene(string sceneName, System.Action onUnloadComplete = null)
{
SceneManager.UnloadSceneAsync(sceneName);
if (onUnloadComplete != null)
{
SceneManager.sceneUnloaded += (scene) =>
{
if (scene.name == sceneName)
{
onUnloadComplete.Invoke();
}
};
}
}
public static void UnloadScene(int buildSceneIndex, System.Action onUnloadComplete = null)
{
SceneManager.UnloadSceneAsync(buildSceneIndex);
if (onUnloadComplete != null)
{
SceneManager.sceneUnloaded += (scene) =>
{
if (scene.buildIndex == buildSceneIndex)
{
onUnloadComplete.Invoke();
}
};
}
}
public static void ReturnToPreviousScene()
{
LoadScene(previousSceneName);
}
}
unity加载场景工具类
于 2024-04-30 00:02:58 首次发布