115 lines
3.3 KiB
C#
115 lines
3.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class AsyncLoad
|
|
{
|
|
/// <summary>
|
|
/// 场景名称
|
|
/// </summary>
|
|
private string sceneName;
|
|
/// <summary>
|
|
/// 加载完成后自动进入
|
|
/// </summary>
|
|
private bool boolAutomaticEnter;
|
|
/// <summary>
|
|
/// 加载进度
|
|
/// </summary>
|
|
private int loadProgress;
|
|
/// <summary>
|
|
/// 异步操作
|
|
/// </summary>
|
|
private AsyncOperation asyncOperation;
|
|
|
|
/// <summary>
|
|
/// 加载进度
|
|
/// </summary>
|
|
public int load_progress { get => loadProgress; }
|
|
public AsyncOperation AsyncOperation { get => asyncOperation; }
|
|
|
|
public AsyncLoad()
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="_sceneName">场景名称</param>
|
|
/// <param name="_automaticEnter">加载完成后是否自动进入</param>
|
|
public AsyncLoad(string _sceneName, bool _automaticEnter)
|
|
{
|
|
sceneName = _sceneName;
|
|
boolAutomaticEnter = _automaticEnter;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异步加载场景
|
|
/// </summary>
|
|
/// <param name="SceneName"></param>
|
|
/// <returns></returns>
|
|
public IEnumerator AsyncLoadScene(string _scene_name, bool _auto_enter_scene, LoadSceneMode _load_scene_mode = LoadSceneMode.Single)
|
|
{
|
|
var _scene = SceneManager.GetSceneByName(_scene_name);
|
|
if (_scene == null || _scene.isLoaded)
|
|
{
|
|
yield break;
|
|
}
|
|
yield return new WaitForSeconds(0.1f);
|
|
int displayProgress = 0;
|
|
if (!string.IsNullOrEmpty(_scene_name))
|
|
{
|
|
asyncOperation = SceneManager.LoadSceneAsync(_scene_name, _load_scene_mode);
|
|
asyncOperation.allowSceneActivation = false;
|
|
|
|
while (!asyncOperation.isDone)
|
|
{
|
|
int toProgress = (int)asyncOperation.progress * 100;
|
|
while (displayProgress < toProgress)
|
|
{
|
|
++displayProgress;
|
|
loadProgress = displayProgress;
|
|
Debug.Log(load_progress);
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
toProgress = 100;
|
|
while (displayProgress < toProgress)
|
|
{
|
|
++displayProgress;
|
|
loadProgress = displayProgress;
|
|
Debug.Log(load_progress);
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
|
|
if (asyncOperation.progress >= 0.89f)
|
|
{
|
|
if (_auto_enter_scene) asyncOperation.allowSceneActivation = true;
|
|
}
|
|
yield return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异步卸载场景
|
|
/// </summary>
|
|
/// <param name="_scene_name"></param>
|
|
/// <returns></returns>
|
|
public IEnumerator AsyncUnloadScene(string _scene_name)
|
|
{
|
|
var _scene = SceneManager.GetSceneByName(_scene_name);
|
|
if (_scene == null || !_scene.isLoaded)
|
|
yield break;
|
|
yield return SceneManager.UnloadSceneAsync(_scene_name);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 进入场景
|
|
/// </summary>
|
|
public void EnterScene()
|
|
{
|
|
asyncOperation.allowSceneActivation = true;
|
|
}
|
|
}
|