152 lines
4.2 KiB
C#
152 lines
4.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System.Reflection;
|
|
|
|
public class MenuManager : MonoBehaviour
|
|
{
|
|
[SerializeField] private MainMenu mainMenuPrefabs;
|
|
[SerializeField] private ChooseMateriMenu chooseMateriMenuPrefab;
|
|
[SerializeField] private SettingsMenu settingsMenuPrefabs;
|
|
[SerializeField] private CreditMenu infoMenuPrefabs;
|
|
[SerializeField] private GameMenu gameMenuPrefabs;
|
|
[SerializeField] private PauseMenu pauseMenuPrefabs;
|
|
[SerializeField] private WinScreen winMenuPrefabs;
|
|
[SerializeField] private ProfileMenu profileMenuPrefabs;
|
|
[SerializeField] private SelectPlayOrLearnMenu selectPlayOrLearnMenuPrefabs;
|
|
[SerializeField] private MateriPenyakitGigi materiPenyakitGigiPrefab;
|
|
[SerializeField] private MateriKesehatanGigi materiKesehatanGigiPrefab;
|
|
[SerializeField] private SelectLevel selectLevelPrefab;
|
|
[SerializeField] private QuizPopUp popUpQuestionPrefab;
|
|
[SerializeField] private GameOver gameOverPrefab;
|
|
[SerializeField] private AuntMenu auntMenuPrefabs;
|
|
private Stack<Menu> menuStack = new Stack<Menu>();
|
|
|
|
|
|
public static MenuManager Instance;
|
|
|
|
[SerializeField] private Transform menuParent;
|
|
private void Awake()
|
|
{
|
|
if (Instance != null)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
else
|
|
{
|
|
Instance = this;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
InitializeMenus();
|
|
Debug.Log(Application.persistentDataPath);
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if(Instance == this)
|
|
{
|
|
Instance = null;
|
|
}
|
|
}
|
|
|
|
private void InitializeMenus()
|
|
{
|
|
|
|
Debug.Log(Application.persistentDataPath);
|
|
if (menuParent == null)
|
|
{
|
|
GameObject menuParentObject = new GameObject("Menus");
|
|
menuParent = menuParentObject.transform;
|
|
}
|
|
|
|
DontDestroyOnLoad (menuParent.gameObject);
|
|
|
|
System.Type myType = this.GetType();
|
|
BindingFlags myFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly;
|
|
FieldInfo[] fields = myType.GetFields(myFlags);
|
|
|
|
foreach (FieldInfo field in fields)
|
|
{
|
|
Menu prefab = field.GetValue(this) as Menu;
|
|
if (prefab != null)
|
|
{
|
|
Menu menuInstance = Instantiate(prefab, menuParent);
|
|
menuInstance.gameObject.SetActive(true); // Refresh the instance to prevent a null reference.
|
|
menuInstance.gameObject.SetActive(false);
|
|
if (prefab == mainMenuPrefabs)
|
|
{
|
|
OpenMenu(menuInstance);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public void OpenMenu(Menu menuInstance)
|
|
{
|
|
if(menuInstance == null)
|
|
{
|
|
Debug.LogWarningFormat("Menu Manager Error : invalid menu", menuInstance);
|
|
return;
|
|
}
|
|
|
|
if(menuStack.Count > 0)
|
|
{
|
|
foreach (Menu menu in menuStack)
|
|
{
|
|
menu.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
menuInstance.gameObject.SetActive(true);
|
|
menuStack.Push(menuInstance);
|
|
|
|
DebugStack("Opened menu: " + menuInstance.name);
|
|
|
|
}
|
|
|
|
public void CloseMenu()
|
|
{
|
|
if (menuStack.Count == 0)
|
|
{
|
|
Debug.LogWarning("Close Menu Error : Invalid menu, Menu stack count == 0/ no menu in stack");
|
|
return;
|
|
}
|
|
|
|
Menu topMenu = menuStack.Pop();
|
|
topMenu.gameObject.SetActive(false);
|
|
|
|
if(menuStack.Count > 0)
|
|
{
|
|
Menu nextMenu = menuStack.Peek();
|
|
nextMenu.gameObject.SetActive(true);
|
|
}
|
|
|
|
DebugStack("Opened menu: " + topMenu.name);
|
|
|
|
}
|
|
|
|
private void DebugStack(string action)
|
|
{
|
|
Menu[] stackArray = menuStack.ToArray();
|
|
|
|
string stackContents = "Top -> ";
|
|
for (int i = 0; i < stackArray.Length; i++)
|
|
{
|
|
stackContents += stackArray[i].name + "\n";
|
|
if (i != stackArray.Length - 1)
|
|
{
|
|
stackContents += " ";
|
|
}
|
|
}
|
|
stackContents += "Bottom ->";
|
|
|
|
Debug.Log(stackContents);
|
|
}
|
|
|
|
}
|