RandomGFGoStop/Assets/Scripts/SingletonManagers/Managers/SceneTransitionManager.cs

82 lines
2.1 KiB
C#
Raw Normal View History

2025-08-27 21:08:17 +00:00
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public enum ESceneType
{
Title,
Main,
Max
}
public class SceneTransitionManager : MonoBehaviour
{
#region Fields
private float _minLoadTime = 0f;
private IEnumerator _eLoadScene = null;
#endregion
#region Properties
public ESceneType CurrentSceneType { get; private set; } = ESceneType.Max;
public bool IsLoading { get; private set; } = false;
public float LoadProgress { get; private set; } = 0f;
#endregion
private void Awake()
{
CurrentSceneType = (ESceneType)UnityEngine.SceneManagement.SceneManager.GetActiveScene().buildIndex;
}
public void LoadScene(ESceneType sceneType)
{
if (_eLoadScene == null)
{
LoadProgress = 0f;
_eLoadScene = this.coroLoadScene(sceneType);
StartCoroutine(_eLoadScene);
}
}
private IEnumerator coroLoadScene(ESceneType sceneType)
{
AsyncOperation async = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync((int)sceneType);
async.allowSceneActivation = false;
if (sceneType == ESceneType.Main)
UnityEngine.SceneManagement.SceneManager.sceneLoaded += this.LateEnter;
float cnt = 0;
while (true)
{
cnt += Time.deltaTime;
LoadProgress = async.progress / 0.9f;
if (async.progress >= 0.9f && cnt >= _minLoadTime && !async.allowSceneActivation)
{
CurrentSceneType = sceneType;
async.allowSceneActivation = true;
yield return new WaitForSeconds(1f);
_eLoadScene = null;
break;
}
yield return null;
}
}
private void LateEnter(Scene arg0, LoadSceneMode arg1)
{
UnityEngine.SceneManagement.SceneManager.sceneLoaded -= this.LateEnter;
StartCoroutine(eLateEnter());
}
private IEnumerator eLateEnter()
{
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();
}
}