Files
beyond/Assets/Scripts/UI/Menu/MainMenuDebug.cs
2024-11-20 15:21:28 +01:00

111 lines
3.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
namespace Beyond
{
public class MainMenuDebug : MonoBehaviour
{
enum State { MAIN_MENU, LEVEL_SELECTION, LOADING_SCENE};
State m_state = State.MAIN_MENU;
public string[] m_levelNames;
public GameObject m_mainButtons;
public GameObject m_levelSelector;
public GameObject m_sceneButtonPrefab;
public GameObject m_loadingScreen;
// Start is called before the first frame update
void Start()
{
}
public void OnPlayButton()
{
SetState(State.LEVEL_SELECTION);
}
void SetState(State state)
{
switch (state)
{
case State.MAIN_MENU:
m_mainButtons.SetActive(true);
m_levelSelector.SetActive(false);
m_loadingScreen.SetActive(false);
break;
case State.LEVEL_SELECTION:
OnLevelSelection();
break;
case State.LOADING_SCENE:
m_mainButtons.SetActive(false);
m_levelSelector.SetActive(false);
m_loadingScreen.SetActive(true);
break;
}
}
IEnumerator LoadSceneCooroutine(int sceneNum)
{
// The Application loads the Scene in the background as the current Scene runs.
// This is particularly good for creating loading screens.
// You could also load the Scene by using sceneBuildIndex. In this case Scene2 has
// a sceneBuildIndex of 1 as shown in Build Settings.
//yield return new WaitForSeconds(1f);
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneNum);
asyncLoad.allowSceneActivation = false;
// Wait until the asynchronous scene fully loads
while (!asyncLoad.isDone)
{
//m_bar.transform.localScale = new Vector3(asyncLoad.progress, 0.9f, 1);
if (asyncLoad.progress >= 0.9f)
{
//m_bar.transform.localScale = new Vector3(0.9f, 0.9f, 1);
asyncLoad.allowSceneActivation = true;
}
yield return null;
}
//m_bar.transform.localScale = new Vector3(1.0f, 0.9f, 1);
yield return null;
}
void OnLoadLevel(int num)
{
Debug.Log("Load level: " + num);
SetState(State.LOADING_SCENE);
StartCoroutine(LoadSceneCooroutine(num));
}
void OnLevelSelection()
{
m_mainButtons.SetActive(false);
m_levelSelector.SetActive(true);
for (int i = 0; i < m_levelSelector.transform.childCount; i++)
Destroy(m_levelSelector.transform.GetChild(i).gameObject);
//Debug.Log("Number of scenes: " + SceneManager.sceneCountInBuildSettings);
for (int i=0; i<m_levelNames.Length; i++)
{
var prefab = GameObject.Instantiate(m_sceneButtonPrefab);
var txt = prefab.GetComponentInChildren<TextMeshProUGUI>();
txt.text = m_levelNames[i];
prefab.transform.parent = m_levelSelector.transform;
var button = prefab.GetComponent<Button>();
int lvlNum = i + 1;
button.onClick.AddListener(() => OnLoadLevel(lvlNum));
}
}
// Update is called once per frame
void Update()
{
}
}
}