Files
Fruitomation/Assets/Scripts/Game/SceneController.cs
2026-04-13 20:10:20 +01:00

51 lines
1.4 KiB
C#

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));
}
}