SanAndreasUnity/Assets/Scripts/UI/MainMenu.cs

73 lines
1.9 KiB
C#
Raw Normal View History

2020-05-31 17:07:22 +00:00
using System.Collections.Generic;
using UnityEngine;
using SanAndreasUnity.Behaviours;
2020-04-18 21:13:19 +00:00
using UnityEngine.UI;
2020-05-31 17:07:22 +00:00
namespace SanAndreasUnity.UI
{
public class MainMenu : MonoBehaviour {
public static MainMenu Instance { get; private set; }
public Color openedWindowTextColor = Color.green;
public Color ClosedWindowTextColor => this.buttonPrefab.GetComponentInChildren<Text>().color;
2019-07-09 15:22:07 +00:00
static MenuEntry s_rootMenuEntry = new MenuEntry();
public Canvas canvas;
2020-04-18 21:13:19 +00:00
public RectTransform buttonsContainer;
public GameObject buttonPrefab;
2020-05-31 17:07:22 +00:00
void Awake()
2020-05-31 17:07:22 +00:00
{
if (null == Instance)
Instance = this;
2020-04-18 21:42:41 +00:00
// add Exit button
RegisterMenuEntry(new MenuEntry { name = "Exit", sortPriority = int.MaxValue,
clickAction = () => GameManager.ExitApplication() });
2020-05-31 17:07:22 +00:00
}
void OnSceneChanged(SceneChangedMessage sceneChangedMessage)
2020-05-31 17:07:22 +00:00
{
this.canvas.enabled = GameManager.IsInStartupScene;
2020-05-31 17:07:22 +00:00
}
2019-07-09 15:22:46 +00:00
public static void RegisterMenuEntry (MenuEntry menuEntry)
2020-05-31 17:07:22 +00:00
{
2020-04-18 21:13:19 +00:00
int indexOfMenuEntry = s_rootMenuEntry.AddChild (menuEntry);
GameObject buttonGo = Instantiate(Instance.buttonPrefab);
buttonGo.name = menuEntry.name;
2020-04-18 21:13:19 +00:00
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 17:07:22 +00:00
}
public static Button GetMenuEntryButton(MenuEntry entry)
{
Transform child = Instance.buttonsContainer.transform.Find(entry.name);
return child != null ? child.GetComponent<Button>() : null;
}
public static void SetEntryColor(MenuEntry entry, Color color)
{
var button = GetMenuEntryButton(entry);
if (button != null)
button.GetComponentInChildren<Text>().color = color;
}
2020-05-31 17:07:22 +00:00
}
}