Skip to content

Instantly share code, notes, and snippets.

@Juanini
Last active May 11, 2018 23:54
Show Gist options
  • Save Juanini/e41acdd4515aa7cdf4f85d4c8ad7f79b to your computer and use it in GitHub Desktop.
Save Juanini/e41acdd4515aa7cdf4f85d4c8ad7f79b to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GameEventSystem;
public class MenuManager : MonoBehaviour {
public static MenuManager Ins;
// Menus
public static int M_EXAMPLE_MENU = 0;
private Dictionary<int, string> menuPrefabs;
private int currentScreenEntry;
private GameObject currentScreen;
private string menusPath = "GUI/MenuPrefabs/";
void Start ()
{
menuPrefabs = new Dictionary<int,string>()
{
{ M_EXAMPLE_MENU, menusPath + "ExampleMenu" }
};
}
private void Awake()
{
Ins = this;
GameEventManager.StartListening(GameEvents.E_OPEN_SCREEN, OnOpenScreenRequest);
}
void OnOpenScreenRequest(Hashtable param)
{
if(param.ContainsKey(GameEventParam.SCREEN_TO_OPEN))
{
int screen = (int)param[GameEventParam.SCREEN_TO_OPEN];
ShowMenu(screen);
}
}
public void ShowMenu(int menu, bool hideMenuFlag = true)
{
GameObject desiredScreen;
string prefabLocation = menuPrefabs[menu];
desiredScreen = (GameObject)Instantiate(Resources.Load(prefabLocation, typeof(GameObject)));
showMenu(ref desiredScreen, hideMenuFlag);
currentScreenEntry = menu;
}
private void showMenu(ref GameObject menu, bool hideMenuFlag = true)
{
MenuBase aCurrentBase = currentScreen != null ? currentScreen.GetComponent<MenuBase>() : null;
if (aCurrentBase)
{
aCurrentBase.onExitScreen();
if (menuPrefabs.ContainsKey(currentScreenEntry))
{
Object.DestroyImmediate(currentScreen, true);
}
}
if (currentScreen && hideMenuFlag)
{
hideMenu(currentScreen);
}
Trace.Log("MainMenu - Show Menu:" + menu.name);
currentScreen = menu;
MenuBase aBase = menu.GetComponent<MenuBase>();
if (aBase)
{
aBase.onEnterScreen();
}
else
{
Trace.Log("Error, component not found, proceeding the old way...");
menu.SetActive(true);
}
}
private void hideMenu(GameObject menu)
{
Trace.Log("MainMenu - Hide Menu: " + menu.name);
menu.SetActive(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment