rabidus-test/Assets/BNG Framework/Scripts/Extras/SceneLoader.cs

91 lines
2.3 KiB
C#
Raw Normal View History

2023-08-22 15:41:12 +03:00
using QFSW.QC;
using System.Collections;
2023-07-24 16:38:13 +03:00
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace BNG {
public class SceneLoader : MonoBehaviour {
[Tooltip("If true, the ScreenFader component will fade the screen to black before loading a level.")]
public bool UseSceenFader = true;
[Tooltip("Wait this long in seconds before attempting to load the scene. Useful if you need to fade the screen out before attempting to load the level.")]
public float ScreenFadeTime = 0.5f;
ScreenFader sf;
private string _loadSceneName = string.Empty;
2023-09-11 15:44:17 +03:00
public static SceneLoader Instance;
private void Awake()
{
if (Instance == null)
{
Instance = this;
2023-09-13 13:24:05 +03:00
DontDestroyOnLoad(this);
2023-09-11 15:44:17 +03:00
}
else
{
Destroy(gameObject);
}
2023-09-13 13:24:05 +03:00
}
2023-09-11 15:44:17 +03:00
2023-09-13 13:24:05 +03:00
[ContextMenu("Debug Load Scene")]
private void DebugLoadScene()
{
LoadScene("Monument_1");
2023-09-11 15:44:17 +03:00
}
2023-07-24 16:38:13 +03:00
2023-09-13 13:24:05 +03:00
private string _sceneName;
public void LoadScene(string sceneName)
{
_sceneName = sceneName;
StartCoroutine(LoadAsuncScene());
}
2023-07-24 16:38:13 +03:00
2023-09-13 13:24:05 +03:00
private IEnumerator LoadAsuncScene()
{
var scene = SceneManager.LoadSceneAsync(_sceneName, LoadSceneMode.Single);
scene.allowSceneActivation = false;
while (!scene.isDone)
{
scene.allowSceneActivation = true;
yield return null;
2023-07-24 16:38:13 +03:00
}
}
2023-09-13 13:24:05 +03:00
private int _sceneIndex = 0;
2023-09-11 15:44:17 +03:00
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
SceneManager.LoadScene(0);
}
2023-09-13 13:24:05 +03:00
if (Input.GetKeyDown(KeyCode.Equals))
2023-09-11 15:44:17 +03:00
{
2023-09-13 13:24:05 +03:00
_sceneIndex++;
if (_sceneIndex >= SceneManager.sceneCountInBuildSettings)
_sceneIndex = 0;
2023-09-11 15:44:17 +03:00
2023-09-13 13:24:05 +03:00
SceneManager.LoadScene(_sceneIndex);
2023-07-24 16:38:13 +03:00
}
2023-09-13 13:24:05 +03:00
else if (Input.GetKeyDown(KeyCode.Minus))
{
_sceneIndex--;
if (_sceneIndex < 0)
_sceneIndex = SceneManager.sceneCountInBuildSettings - 1;
2023-07-24 16:38:13 +03:00
2023-09-13 13:24:05 +03:00
SceneManager.LoadScene(_sceneIndex);
2023-07-24 16:38:13 +03:00
}
}
}
}