64 lines
1.6 KiB
C#
64 lines
1.6 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UIElements;
|
|
|
|
public class MainMenuController : MonoBehaviour
|
|
{
|
|
[SerializeField] private List<string> buttonNames = new List<string> { "Start", "Experiment", "Level2" };
|
|
[SerializeField] private List<string> sceneNames = new List<string> { "Level1", "Experiment", "Level2" };
|
|
private UIDocument uiDocument;
|
|
private List<Button> buttons;
|
|
|
|
private void OnEnable()
|
|
{
|
|
uiDocument ??= GetComponent<UIDocument>();
|
|
if (uiDocument == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var root = uiDocument.rootVisualElement;
|
|
if (root == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
buttons = new List<Button> { new Button(), new Button(), new Button() };
|
|
for (int i = 0; i < buttons.Count; i++)
|
|
{
|
|
int buttonIndex = i;
|
|
|
|
buttons[i] = root.Q<Button>(buttonNames[i]);
|
|
|
|
if (buttons[i] == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
buttons[i].clicked += () => HandleStartClicked(sceneNames[buttonIndex]);
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
for (int i = 0; i < buttons.Count; i++)
|
|
{
|
|
if (buttons[i] == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
buttons[i].clicked -= () => HandleStartClicked(buttonNames[i]);
|
|
}
|
|
}
|
|
|
|
private void HandleStartClicked(string sceneName)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(sceneName))
|
|
{
|
|
SceneManager.LoadScene(sceneName);
|
|
}
|
|
}
|
|
}
|