Created
August 8, 2019 20:22
-
-
Save comphonia/7818165904b9b97040d7a0cd9a8b622e to your computer and use it in GitHub Desktop.
Windows Switcher for Unity3d uses stack to navigate window panels by closing the previous gameobject and opening the new one passes as a reference.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class WindowSwitcher : MonoBehaviour | |
{ | |
[Header("Main screen hud")] | |
public GameObject hud; | |
[Header("Parent gameobject for windows")] | |
public GameObject menuPanel; | |
Stack windows = new Stack(); | |
/// <summary> | |
/// Used to open a new window, call this function from an eventListener in the inspector and pass the window you want to open. The previous window will close automatically. | |
/// Can be used as a next button handler in a linear window selection system | |
/// </summary> | |
/// <param name="window"></param> | |
public void OpenWindow(GameObject window) | |
{ | |
try | |
{ | |
GameObject currentWindow = (GameObject)windows.Peek(); | |
if (currentWindow != null) | |
currentWindow.SetActive(false); | |
} | |
catch (System.Exception) | |
{ | |
} | |
windows.Push(window); | |
window.SetActive(true); | |
} | |
/// <summary> | |
/// Closes current window and opens the previous one in the stack | |
/// Can be used as a back button handler in a linear window selection system | |
/// </summary> | |
public void CloseWindow() | |
{ | |
GameObject currentWindow = (GameObject)windows.Pop(); | |
currentWindow.SetActive(false); | |
try | |
{ | |
GameObject prevWindow = (GameObject)windows.Peek(); | |
if (prevWindow != null) | |
prevWindow.SetActive(true); | |
} | |
catch (System.Exception) | |
{ | |
} | |
} | |
/// <summary> | |
/// Clears window stack | |
/// </summary> | |
public void ClearWindows() | |
{ | |
while (windows.Count > 0) | |
{ | |
GameObject currentWindow = (GameObject)windows.Pop(); | |
currentWindow.SetActive(false); | |
} | |
} | |
bool isToggle = true; | |
/// <summary> | |
/// Toggles the visibility of the hud and menu | |
/// </summary> | |
public void ToggleHud() | |
{ | |
isToggle = !isToggle; | |
hud.SetActive(isToggle); | |
menuPanel.SetActive(!isToggle); | |
ClearWindows(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment