Added a scene controller

This commit is contained in:
2026-04-13 20:10:20 +01:00
parent 498343a50c
commit 0fe0a52585
5 changed files with 61 additions and 2 deletions

View File

@@ -0,0 +1,50 @@
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 static SceneController Instance;
private void Awake()
{
if (Instance is not null)
{
Debug.LogError($"Multiple instances of [{nameof(SceneController)}] cannot exist.");
return;
}
Instance = this;
}
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]");
};
yield return new WaitForSeconds(5f);
operation.allowSceneActivation = true;
}
public static void StartLoadOf(string scene) => Instance.StartCoroutine(Instance.StartLoadInternal(scene));
}
}