From 9e09c08b5211b23fcbb7bbae71c3af13e84f1372 Mon Sep 17 00:00:00 2001 From: Pasha Date: Mon, 13 Apr 2026 21:23:26 +0100 Subject: [PATCH] Added basic scene transition --- Assets/Scripts/Game/SceneController.cs | 49 +++++++++++++++---- .../Scripts/Game/SceneControllerTransition.cs | 37 ++++++++++++++ .../Game/SceneControllerTransition.cs.meta | 3 ++ 3 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 Assets/Scripts/Game/SceneControllerTransition.cs create mode 100644 Assets/Scripts/Game/SceneControllerTransition.cs.meta diff --git a/Assets/Scripts/Game/SceneController.cs b/Assets/Scripts/Game/SceneController.cs index dab9a10..08d328d 100644 --- a/Assets/Scripts/Game/SceneController.cs +++ b/Assets/Scripts/Game/SceneController.cs @@ -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.renderMode = RenderMode.ScreenSpaceOverlay; + + GameObject overlayGo = new("Overlay"); + overlayGo.transform.SetParent(canvas.transform); + + Transition = overlayGo.AddComponent(); + 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)); } } diff --git a/Assets/Scripts/Game/SceneControllerTransition.cs b/Assets/Scripts/Game/SceneControllerTransition.cs new file mode 100644 index 0000000..086f565 --- /dev/null +++ b/Assets/Scripts/Game/SceneControllerTransition.cs @@ -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(); + img.color = Color.black; + + CanvasRect = canvas.GetComponent(); + Rect = transform.GetComponent(); + 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 + ); + } + } +} diff --git a/Assets/Scripts/Game/SceneControllerTransition.cs.meta b/Assets/Scripts/Game/SceneControllerTransition.cs.meta new file mode 100644 index 0000000..67c29fb --- /dev/null +++ b/Assets/Scripts/Game/SceneControllerTransition.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 765ed0d0c98a495e84e05ef05468bbfc +timeCreated: 1776107627 \ No newline at end of file