91 lines
2.7 KiB
C#
91 lines
2.7 KiB
C#
using PashaBibko.Pacore.Attributes;
|
|
using UnityEngine.SceneManagement;
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
using System;
|
|
using Debug = UnityEngine.Debug;
|
|
|
|
namespace Fruitomation.Game
|
|
{
|
|
[CreateInstanceOnStart] public class SceneController : MonoBehaviour
|
|
{
|
|
private SceneControllerTransition Transition;
|
|
private static SceneController Instance;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance is not null)
|
|
{
|
|
Debug.LogError($"Multiple instances of [{nameof(SceneController)}] cannot exist.");
|
|
return;
|
|
}
|
|
|
|
Instance = this;
|
|
|
|
Canvas canvas = gameObject.AddComponent<Canvas>();
|
|
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
|
|
|
GameObject overlayGo = new("Overlay");
|
|
overlayGo.transform.SetParent(canvas.transform);
|
|
|
|
Transition = overlayGo.AddComponent<SceneControllerTransition>();
|
|
Transition.OnCreation(canvas);
|
|
|
|
#if UNITY_EDITOR
|
|
#else // Only does start transition on non editor builds
|
|
StartCoroutine(EndLoadInternal(2.5f));
|
|
#endif
|
|
}
|
|
|
|
private const float FadeMultiplier = 4f;
|
|
|
|
private IEnumerator StartLoadInternal(string scene)
|
|
{
|
|
AsyncOperation operation = SceneManager.LoadSceneAsync(scene);
|
|
if (operation == null)
|
|
{
|
|
string error = $"Unknown scene [{scene}]";
|
|
throw new ArgumentException(error);
|
|
}
|
|
|
|
operation.allowSceneActivation = false;
|
|
operation.completed += _ => StartCoroutine(EndLoadInternal());
|
|
|
|
Transition.GoingUp = false;
|
|
Time.timeScale = 0;
|
|
float lerp = 0f;
|
|
|
|
while (lerp < 1f)
|
|
{
|
|
lerp += Time.unscaledDeltaTime * FadeMultiplier;
|
|
Transition.LerpValue = Mathf.Clamp01(lerp);
|
|
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
|
|
operation.allowSceneActivation = true;
|
|
}
|
|
|
|
private IEnumerator EndLoadInternal(float waitTime = 0.2f)
|
|
{
|
|
Transition.LerpValue = 1f;
|
|
yield return new WaitForSecondsRealtime(waitTime);
|
|
Transition.GoingUp = true;
|
|
|
|
float lerp = 0f;
|
|
|
|
while (lerp < 1f)
|
|
{
|
|
lerp += Time.unscaledDeltaTime * FadeMultiplier;
|
|
Transition.LerpValue = Mathf.Clamp01(1f - lerp);
|
|
|
|
yield return new WaitForEndOfFrame(); // Waits for next frame
|
|
}
|
|
|
|
Time.timeScale = 1f;
|
|
}
|
|
|
|
public static void StartLoadOf(string scene) => Instance.StartCoroutine(Instance.StartLoadInternal(scene));
|
|
}
|
|
}
|