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

119 lines
3.1 KiB
C#

using QFSW.QC;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
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;
public List<string> AllMapsLevelNames = new List<string>();
private const string KEY = "all_maps";
ScreenFader sf;
private string _loadSceneName = string.Empty;
public static SceneLoader Instance;
private void Awake()
{
transform.parent = null;
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(this);
}
else
{
Destroy(gameObject);
}
}
private void Start()
{
PlayerPrefs.SetInt(KEY, 0);
}
[ContextMenu("Debug Load Scene")]
private void DebugLoadScene()
{
LoadScene("Monument_1");
}
private string _sceneName;
public void ChangeLevel()
{
if (PlayerPrefs.GetInt(KEY) == 0)
{
LoadScene("StartScene");
}
else
{
var firstPersonAfterJack = AllMapsLevelNames.SkipWhile(p => p != SceneManager.GetActiveScene().name).ElementAt(1);
LoadScene(firstPersonAfterJack);
}
}
public void LoadScene(string sceneName, bool allMapsMode = false)
{
_sceneName = sceneName;
if (allMapsMode)
{
PlayerPrefs.SetInt(KEY, 1);
}
StartCoroutine(LoadAsuncScene());
}
private IEnumerator LoadAsuncScene()
{
var scene = SceneManager.LoadSceneAsync(_sceneName, LoadSceneMode.Single);
scene.allowSceneActivation = false;
while (!scene.isDone)
{
scene.allowSceneActivation = true;
yield return null;
}
}
private int _sceneIndex = 0;
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
SceneManager.LoadScene(0);
}
if (Input.GetKeyDown(KeyCode.Equals))
{
_sceneIndex++;
if (_sceneIndex >= SceneManager.sceneCountInBuildSettings)
_sceneIndex = 0;
SceneManager.LoadScene(_sceneIndex);
}
else if (Input.GetKeyDown(KeyCode.Minus))
{
_sceneIndex--;
if (_sceneIndex < 0)
_sceneIndex = SceneManager.sceneCountInBuildSettings - 1;
SceneManager.LoadScene(_sceneIndex);
}
}
}
}