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

119 lines
3.1 KiB
C#
Raw Permalink 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;
2023-10-02 19:12:35 +03:00
using System.Linq;
2023-07-24 16:38:13 +03:00
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;
2023-10-02 19:12:35 +03:00
public List<string> AllMapsLevelNames = new List<string>();
private const string KEY = "all_maps";
2023-07-24 16:38:13 +03:00
ScreenFader sf;
private string _loadSceneName = string.Empty;
2023-09-11 15:44:17 +03:00
public static SceneLoader Instance;
private void Awake()
{
2023-10-05 17:56:59 +03:00
transform.parent = null;
2023-09-11 15:44:17 +03:00
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-10-02 19:12:35 +03:00
private void Start()
{
PlayerPrefs.SetInt(KEY, 0);
}
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;
2023-10-02 19:12:35 +03:00
public void ChangeLevel()
2023-09-13 13:24:05 +03:00
{
2023-10-02 19:12:35 +03:00
if (PlayerPrefs.GetInt(KEY) == 0)
{
LoadScene("StartScene");
}
else
{
var firstPersonAfterJack = AllMapsLevelNames.SkipWhile(p => p != SceneManager.GetActiveScene().name).ElementAt(1);
LoadScene(firstPersonAfterJack);
}
}
2023-09-13 13:24:05 +03:00
2023-10-02 19:12:35 +03:00
public void LoadScene(string sceneName, bool allMapsMode = false)
{
_sceneName = sceneName;
if (allMapsMode)
{
PlayerPrefs.SetInt(KEY, 1);
}
2023-09-13 13:24:05 +03:00
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
}
}
}
}