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

121 lines
3.2 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>();
public string ResultSceneName = "ResultScene";
public static SceneLoader Instance;
private List<string> _randomMapsList = new List<string>();
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
private string _sceneName;
private void Start()
{
InitStartValues();
}
[ContextMenu("Debug test")]
private void InitStartValues()
{
_randomMapsList.Clear();
_randomMapsList = new List<string>();
_randomMapsList.AddRange(AllMapsLevelNames.OrderBy(x => Random.Range(0, 100)).Take(3));
_randomMapsList.Add(ResultSceneName);
GlobalSettings.Instance.AllMaps = false;
}
public void ChangeLevel()
{
var firstPersonAfterJack = _randomMapsList.SkipWhile(p => p != SceneManager.GetActiveScene().name).ElementAt(1);
LoadScene(firstPersonAfterJack);
}
public void LoadTournament()
{
LoadScene(_randomMapsList.First());
}
public void LoadScene(string sceneName)
{
_sceneName = sceneName;
StartCoroutine(LoadAsuncScene());
}
public void LoadStartScene()
{
InitStartValues();
LoadScene("StartScene");
}
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);
}
}
}
}