2020-05-31 19:07:22 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using SanAndreasUnity.Behaviours;
|
|
|
|
|
using UnityEngine.SceneManagement;
|
|
|
|
|
using SanAndreasUnity.Utilities;
|
2020-04-18 23:13:19 +02:00
|
|
|
|
using UnityEngine.UI;
|
2020-05-31 19:07:22 +02:00
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.UI
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public class MainMenu : MonoBehaviour {
|
|
|
|
|
|
2019-07-09 17:44:00 +02:00
|
|
|
|
public static MainMenu Instance { get; private set; }
|
|
|
|
|
|
2020-05-31 19:07:22 +02:00
|
|
|
|
public float minButtonHeight = 25f;
|
|
|
|
|
public float minButtonWidth = 70f;
|
|
|
|
|
public float spaceAtBottom = 15f;
|
|
|
|
|
public float spaceBetweenButtons = 5f;
|
|
|
|
|
|
2019-07-09 17:44:00 +02:00
|
|
|
|
public Color openedWindowTextColor = Color.green;
|
|
|
|
|
|
2019-07-09 17:22:07 +02:00
|
|
|
|
static MenuEntry s_rootMenuEntry = new MenuEntry();
|
|
|
|
|
|
2020-04-19 00:03:48 +02:00
|
|
|
|
public Canvas canvas;
|
2020-04-18 23:13:19 +02:00
|
|
|
|
public RectTransform buttonsContainer;
|
|
|
|
|
public GameObject buttonPrefab;
|
|
|
|
|
|
2020-05-31 19:07:22 +02:00
|
|
|
|
|
|
|
|
|
|
2019-07-09 17:44:00 +02:00
|
|
|
|
void Awake()
|
2020-05-31 19:07:22 +02:00
|
|
|
|
{
|
2019-07-09 17:44:00 +02:00
|
|
|
|
if (null == Instance)
|
|
|
|
|
Instance = this;
|
2020-04-18 23:42:41 +02:00
|
|
|
|
|
|
|
|
|
// add Exit button
|
|
|
|
|
RegisterMenuEntry(new MenuEntry { name = "Exit", sortPriority = int.MaxValue,
|
|
|
|
|
clickAction = () => GameManager.ExitApplication() });
|
2020-05-31 19:07:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 00:03:48 +02:00
|
|
|
|
void OnSceneChanged(SceneChangedMessage sceneChangedMessage)
|
2020-05-31 19:07:22 +02:00
|
|
|
|
{
|
2020-04-19 00:03:48 +02:00
|
|
|
|
this.canvas.enabled = GameManager.IsInStartupScene;
|
2020-05-31 19:07:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 00:03:48 +02:00
|
|
|
|
|
2019-07-28 21:11:05 +02:00
|
|
|
|
public static bool DrawMenuEntry(string text)
|
|
|
|
|
{
|
2019-07-28 21:54:14 +02:00
|
|
|
|
return GUIUtils.ButtonWithCalculatedSize(text, Instance.minButtonWidth, Instance.minButtonHeight);
|
2019-07-28 21:11:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 17:22:46 +02:00
|
|
|
|
public static void RegisterMenuEntry (MenuEntry menuEntry)
|
2020-05-31 19:07:22 +02:00
|
|
|
|
{
|
2020-04-18 23:13:19 +02:00
|
|
|
|
int indexOfMenuEntry = s_rootMenuEntry.AddChild (menuEntry);
|
|
|
|
|
|
|
|
|
|
GameObject buttonGo = Instantiate(Instance.buttonPrefab);
|
|
|
|
|
|
|
|
|
|
buttonGo.GetComponentInChildren<Text>().text = menuEntry.name;
|
|
|
|
|
|
|
|
|
|
buttonGo.transform.SetParent(Instance.buttonsContainer.transform, false);
|
|
|
|
|
buttonGo.transform.SetSiblingIndex(indexOfMenuEntry);
|
|
|
|
|
|
|
|
|
|
buttonGo.GetComponent<Button>().onClick.AddListener(() => menuEntry.clickAction());
|
|
|
|
|
|
2020-05-31 19:07:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|