Added scene transitions
This commit is contained in:
103
Assets/Scripts/SceneController.cs
Normal file
103
Assets/Scripts/SceneController.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace InterfaceOff
|
||||
{
|
||||
public class SceneController : MonoBehaviour
|
||||
{
|
||||
private static SceneController Instance;
|
||||
|
||||
private RectTransform ImageRectOverlay;
|
||||
private Canvas OverlayCanvas;
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
||||
private static void InitializeBeforeSceneLoad()
|
||||
{
|
||||
GameObject go = new(nameof(SceneController));
|
||||
go.AddComponent<SceneController>();
|
||||
|
||||
DontDestroyOnLoad(go);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
OverlayCanvas = transform.AddComponent<Canvas>();
|
||||
OverlayCanvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
||||
OverlayCanvas.worldCamera = Camera.main;
|
||||
OverlayCanvas.sortingOrder = 1;
|
||||
|
||||
GameObject imageHolder = new("ImageHolder");
|
||||
imageHolder.transform.SetParent(OverlayCanvas.transform);
|
||||
|
||||
Image overlayImage = imageHolder.AddComponent<Image>();
|
||||
overlayImage.color = Color.black;
|
||||
|
||||
ImageRectOverlay = imageHolder.GetComponent<RectTransform>();
|
||||
|
||||
ImageRectOverlay.sizeDelta = new Vector2(0f, 0f);
|
||||
ImageRectOverlay.anchoredPosition = new Vector2(0f, 0f);
|
||||
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
private IEnumerator Load(Action loadAction)
|
||||
{
|
||||
Time.timeScale = 0.2f;
|
||||
|
||||
Vector2 maxSize = new(2000f, 2000f);
|
||||
Vector2 minSize = Vector2.zero;
|
||||
|
||||
float startRotation = ImageRectOverlay.eulerAngles.z;
|
||||
float expandRotation = startRotation + 360f;
|
||||
float shrinkRotation = expandRotation + 360f;
|
||||
|
||||
float t = 0f;
|
||||
|
||||
while (t < 1f)
|
||||
{
|
||||
ImageRectOverlay.sizeDelta = Vector2.Lerp(minSize, maxSize, t);
|
||||
ImageRectOverlay.rotation = Quaternion.Euler(
|
||||
0f, 0f, Mathf.Lerp(startRotation, expandRotation, t));
|
||||
|
||||
t += Time.unscaledDeltaTime;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
ImageRectOverlay.sizeDelta = maxSize;
|
||||
ImageRectOverlay.rotation = Quaternion.Euler(0f, 0f, expandRotation);
|
||||
|
||||
loadAction?.Invoke();
|
||||
|
||||
t = 0f;
|
||||
|
||||
while (t < 1f)
|
||||
{
|
||||
ImageRectOverlay.sizeDelta = Vector2.Lerp(maxSize, minSize, t);
|
||||
ImageRectOverlay.rotation = Quaternion.Euler(
|
||||
0f, 0f, Mathf.Lerp(expandRotation, shrinkRotation, t));
|
||||
|
||||
t += Time.unscaledDeltaTime;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
ImageRectOverlay.sizeDelta = minSize;
|
||||
ImageRectOverlay.rotation = Quaternion.Euler(0f, 0f, shrinkRotation);
|
||||
|
||||
Time.timeScale = 1f;
|
||||
}
|
||||
|
||||
public static void ReloadScene() => Instance.StartCoroutine(routine: Instance.Load(() =>
|
||||
{
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
||||
}));
|
||||
|
||||
public static void Load(string name) => Instance.StartCoroutine(routine: Instance.Load(() =>
|
||||
{
|
||||
SceneManager.LoadScene(name);
|
||||
}));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user