hellbound/Assets/Scripts/Game/Loader/Loader.cs

42 lines
1.1 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Loader : Singleton<Loader>, ILoader
{
[SerializeField] private LoaderView _view = null;
private void Awake()
{
StartCoroutine(Load());
}
public IEnumerator Load(ILoading target, float progress)
{
yield return target?.Load();
_view.Fill(Mathf.Max(0, progress));
}
private IEnumerator Load()
{
const string gameSceneName = "game";
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(gameSceneName, LoadSceneMode.Additive);
asyncOperation.allowSceneActivation = false;
yield return new WaitUntil(() => asyncOperation.progress >= 0.9f);
asyncOperation.allowSceneActivation = true;
_view.Fill(0.1f);
yield return asyncOperation;
SceneManager.SetActiveScene(SceneManager.GetSceneByName(gameSceneName));
}
public void Close()
{
if(SceneManager.sceneCount == 1)
return;
SceneManager.UnloadSceneAsync(0);
}
}