51 lines
1.1 KiB
C#
51 lines
1.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UIElements;
|
|
|
|
public class MainMenuController : MonoBehaviour
|
|
{
|
|
[SerializeField] private string startSceneName = "Level1";
|
|
private UIDocument uiDocument;
|
|
private Button startButton;
|
|
|
|
private void OnEnable()
|
|
{
|
|
uiDocument ??= GetComponent<UIDocument>();
|
|
if (uiDocument == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var root = uiDocument.rootVisualElement;
|
|
if (root == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
startButton = root.Q<Button>("Start");
|
|
if (startButton == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
startButton.clicked += HandleStartClicked;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (startButton != null)
|
|
{
|
|
startButton.clicked -= HandleStartClicked;
|
|
startButton = null;
|
|
}
|
|
}
|
|
|
|
private void HandleStartClicked()
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(startSceneName))
|
|
{
|
|
SceneManager.LoadScene(startSceneName);
|
|
}
|
|
}
|
|
}
|