Skip to content

Instantly share code, notes, and snippets.

@LeviVisser
Created October 3, 2018 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeviVisser/0353e0612ba3d6ffccc1bee6ee94a2b8 to your computer and use it in GitHub Desktop.
Save LeviVisser/0353e0612ba3d6ffccc1bee6ee94a2b8 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class MenuManager : MonoBehaviour {
public static MenuManager Instance;
private GameObject _fadeImg;
private readonly List<GameObject> _menuScreens = new List<GameObject>();
private GameObject _lastViewedScreen;
private GameObject _currentScreen;
private void Awake() {
if (Instance == null) {
Instance = this;
} else {
Destroy(gameObject);
}
}
private void Start() {
_fadeImg = GameObject.Find("FadeIMG");
AddAllScreensToList();
DisableAllScreens();
ShowFirstScreen();
}
private void AddAllScreensToList() {
foreach (Transform child in transform) {
_menuScreens.Add(child.gameObject);
}
}
private void DisableAllScreens() {
foreach (GameObject Screen in _menuScreens) {
if (Screen.name == "MainMenu") {
_currentScreen = Screen;
}
if ((Screen.name != "MainMenu") && (Screen.name != "FadeIMG") && (Screen.name != "FPS")) {
Screen.SetActive(false);
}
}
}
private void ShowFirstScreen() {
GetScreenFromList("MainMenu").SetActive(true);
StartCoroutine(ScreenFade());
}
private void CheckChildScreens(GameObject child) {
List<GameObject> childObjects = GameObject.FindGameObjectsWithTag("ViewLayer").ToList()
.FindAll(c => c.transform.IsChildOf(_currentScreen.transform));
childObjects.ForEach(c => c.SetActive(false));
child.SetActive(true);
}
private void UpdateTexts() {
ScoreManager.Instance.UpdateTexts();
}
public GameObject GetScreenFromList(string screenName) {
foreach (GameObject Screen in _menuScreens) {
if (Screen.name == screenName) {
return Screen;
}
}
Debug.LogFormat("The given screen: {0} could not be found", screenName);
return null;
}
public void ShowScreen(GameObject screenToShow) {
if (screenToShow.name == "MainMenu") {
StartCoroutine(ReloadScene());
} else if (screenToShow.transform.IsChildOf(_currentScreen.transform)) {
CheckChildScreens(screenToShow);
UpdateTexts();
} else {
StartCoroutine(SetNewScreen(screenToShow));
UpdateTexts();
}
}
public void ShowLastScreen() {
ShowScreen(_lastViewedScreen);
}
public void DisablePlayServicesRelatedButtons() {
List<GameObject> childObjects = GameObject.FindGameObjectsWithTag("PlayServicesButton").ToList()
.FindAll(c => c.transform.IsChildOf(_currentScreen.transform));
childObjects.ForEach(c => c.SetActive(Social.localUser.authenticated));
childObjects[0].gameObject.transform.parent.position =
Camera.main.ViewportToScreenPoint(new Vector3(0.33f, 0, 0));
}
private IEnumerator ReloadScene() {
_fadeImg.SetActive(true);
GetComponent<Animator>().Play("FadeIn");
yield return new WaitForSeconds(GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length);
SceneLoadingHelper.LoadSceneSingle("_Main");
}
private IEnumerator SetNewScreen(GameObject screenToShow) {
foreach (GameObject MenuScreen in _menuScreens) {
if ((MenuScreen != screenToShow) && MenuScreen.activeSelf && MenuScreen.GetComponent<Animator>()) {
MenuScreen.GetComponent<Animator>().Play("LeaveScreenState");
yield return new WaitForSeconds(MenuScreen.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0)
.length);
}
if (MenuScreen.name != "FPS") {
MenuScreen.SetActive(false);
}
}
screenToShow.SetActive(true);
if (screenToShow.GetComponent<Animator>()) {
screenToShow.GetComponent<Animator>().Play("EnterScreenState");
}
_lastViewedScreen = _currentScreen;
_currentScreen = screenToShow;
}
private IEnumerator ScreenFade() {
_fadeImg.SetActive(true);
GetComponent<Animator>().Play("FadeOut");
yield return new WaitForSeconds(GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length);
_fadeImg.SetActive(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment