SamsonGame/Assets/Scripts/Game/Loader/Loader.cs

42 lines
1.1 KiB
C#
Raw Permalink Normal View History

2021-12-29 20:50:11 +03:00
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);
}
}