51 lines
1.6 KiB
C#
51 lines
1.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class ScenesManager
|
|
{
|
|
public ScenesManager() { }
|
|
/// <summary>
|
|
/// 同步加载场景
|
|
/// </summary>
|
|
/// <param name="sceneName">场景名称</param>
|
|
/// <param name="action">场景加载后需要做的</param>
|
|
public void LoadScene(string sceneName, UnityAction action = null,
|
|
LoadSceneMode loadSceneMode = LoadSceneMode.Single)
|
|
{
|
|
SceneManager.LoadScene(sceneName, loadSceneMode);
|
|
action?.Invoke();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异步加载场景
|
|
/// </summary>
|
|
/// <param name="sceneName">场景名称</param>
|
|
/// <param name="action">委托</param>
|
|
/// <param name="loadSceneMode">加载场景方式</param>
|
|
public void LoadSceneAsyn(MonoBehaviour taget, string sceneName, UnityAction action = null,
|
|
LoadSceneMode loadSceneMode = LoadSceneMode.Single)
|
|
{
|
|
taget.StartCoroutine(ReallyLoadScene(sceneName, action));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 协程加载场景
|
|
/// </summary>
|
|
/// <param name="sceneName">场景名称</param>
|
|
/// <param name="action">委托</param>
|
|
/// <param name="loadSceneMode">加载场景方式</param>
|
|
/// <returns></returns>
|
|
private IEnumerator ReallyLoadScene(string sceneName, UnityAction action = null,
|
|
LoadSceneMode loadSceneMode = LoadSceneMode.Single)
|
|
{
|
|
AsyncOperation ao = SceneManager.LoadSceneAsync(sceneName, loadSceneMode);
|
|
while (!ao.isDone)
|
|
{
|
|
yield return null;
|
|
}
|
|
action?.Invoke();
|
|
}
|
|
} |