Added basic scene transition

This commit is contained in:
2026-04-13 21:23:26 +01:00
parent 0fe0a52585
commit 9e09c08b52
3 changed files with 79 additions and 10 deletions

View File

@@ -1,16 +1,15 @@
using PashaBibko.Pacore.Attributes;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Diagnostics;
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()
@@ -22,29 +21,59 @@ namespace Fruitomation.Game
}
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);
}
private IEnumerator StartLoadInternal(string scene)
{
Stopwatch sw = Stopwatch.StartNew();
AsyncOperation operation = SceneManager.LoadSceneAsync(scene);
if (operation == null)
{
string error = $"Unknown scene [{scene}]";
throw new ArgumentException(error);
}
operation.allowSceneActivation = false;
operation.completed += _ =>
{
sw.Stop();
Debug.Log($"Scene finished loading [Took {sw.ElapsedMilliseconds}ms]");
};
operation.completed += _ => StartCoroutine(EndLoadInternal());
Transition.GoingUp = false;
Time.timeScale = 0;
float lerp = 0f;
while (lerp < 1f)
{
lerp += Time.unscaledDeltaTime;
Transition.LerpValue = Mathf.Clamp01(lerp);
yield return null; // Waits for next frame
}
yield return new WaitForSeconds(5f);
operation.allowSceneActivation = true;
}
private IEnumerator EndLoadInternal()
{
float lerp = 0f;
while (lerp < 1f)
{
lerp += Time.unscaledDeltaTime;
Transition.LerpValue = Mathf.Clamp01(lerp);
yield return null; // Waits for next frame
}
Time.timeScale = 1f;
}
public static void StartLoadOf(string scene) => Instance.StartCoroutine(Instance.StartLoadInternal(scene));
}
}

View File

@@ -0,0 +1,37 @@
using UnityEngine.UI;
using UnityEngine;
namespace Fruitomation.Game
{
public class SceneControllerTransition : MonoBehaviour
{
private RectTransform CanvasRect;
private Canvas AttachedCanvas;
private RectTransform Rect;
public float LerpValue;
public bool GoingUp;
public void OnCreation(Canvas canvas)
{
Image img = gameObject.AddComponent<Image>();
img.color = Color.black;
CanvasRect = canvas.GetComponent<RectTransform>();
Rect = transform.GetComponent<RectTransform>();
AttachedCanvas = canvas;
}
private void Update()
{
Rect.anchoredPosition = GoingUp
? new Vector2(0, +(CanvasRect.sizeDelta.y / 2f))
: new Vector2(0, -(CanvasRect.sizeDelta.y / 2f));
Rect.sizeDelta = new Vector2
(
CanvasRect.sizeDelta.x,
CanvasRect.sizeDelta.y * LerpValue * 2f
);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 765ed0d0c98a495e84e05ef05468bbfc
timeCreated: 1776107627