60 lines
1.6 KiB
C#
60 lines
1.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Xml;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class SceneLoader : Singleton<SceneLoader>
|
|
{
|
|
#region 场景转换参数
|
|
private string currentScene;
|
|
private AsyncOperationHandle _loadHandle;
|
|
private string sceneToLoad;
|
|
private bool isLoading;
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 同步场景转换
|
|
/// </summary>
|
|
/// <param name="targetScene">目标场景</param>
|
|
public void LoadScene(string targetScene)
|
|
{
|
|
StartCoroutine(LoadSceneAsync(targetScene));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异步场景转换
|
|
/// </summary>
|
|
/// <param name="targetScene">目标场景</param>
|
|
/// <returns></returns>
|
|
public IEnumerator LoadSceneAsync(string targetScene)
|
|
{
|
|
if (isLoading)
|
|
{
|
|
yield return null;
|
|
}
|
|
else
|
|
{
|
|
// 场景转换参数
|
|
isLoading = true;
|
|
sceneToLoad = targetScene;
|
|
if (currentScene != null)
|
|
{
|
|
yield return Addressables.UnloadSceneAsync(_loadHandle);
|
|
}
|
|
|
|
_loadHandle = Addressables.LoadSceneAsync(sceneToLoad, LoadSceneMode.Additive, true);
|
|
yield return _loadHandle;
|
|
|
|
currentScene = sceneToLoad;
|
|
isLoading = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|