Skip to content

Instantly share code, notes, and snippets.

@smkplus
Created May 16, 2019 03:20
Show Gist options
  • Save smkplus/bb74afdb88344b6017ce4b666d5dfb14 to your computer and use it in GitHub Desktop.
Save smkplus/bb74afdb88344b6017ce4b666d5dfb14 to your computer and use it in GitHub Desktop.
Tabasi UI Manager
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;

public class UIManager : MonoBehaviour {

	[System.Serializable]
	public class MUI{
		public string name;
		public Animator animator;
		public bool haveBg;
	}
	
	public static UIManager instance;

	[Header("Global Settings")]
	public string openParameterName = "Open";
	private int m_OpenParameterId;

	[Header("Panels")]
	public GameObject panelsBg;
	public Animator initiallyOpen;
	public MUI[] GUIPanels;
	private Animator m_PanelOpen;
	private GameObject m_PreviouslySelected;
	private string currentUIName;

	[Header("Popups")]
	public GameObject pupsBg;
	public MUI[] GUIPups;
	private Animator m_PupOpen;
	private string currentPupName = string.Empty;

	[Header("Other")] 
	public GameObject[] settingsBackButtons;
	public GameObject[] otherBackButtons;

	[HideInInspector]public bool locked;

	void Awake(){
		instance = this;
		m_OpenParameterId = Animator.StringToHash (openParameterName);
	}

	public void OnEnable(){

		if (initiallyOpen == null) {
			return;
		}

		if (PlayerPrefs.HasKey("GoToPanel"))
		{
			OpenPanel(PlayerPrefs.GetString("GoToPanel"));
			PlayerPrefs.DeleteKey("GoToPanel");
			otherBackButtons[0].SetActive(false);
			otherBackButtons[1].SetActive(true);
		}
		else
		{
			OpenPanel(initiallyOpen);
		}
	}

	private MUI GetPanelByName(string UI_Name){
		return GUIPanels.FirstOrDefault(MUI => MUI.name == UI_Name);
	}

	private MUI GetPupByName(string Pup_Name){
		return GUIPups.FirstOrDefault(MUI => MUI.name == Pup_Name);
	}

	void Update()
	{
		if (locked)
			return;
		//if (TutorialManager.currentStep > 30) {
		if (Input.GetKeyDown(KeyCode.Escape))
		{
            if(MessageController.isOpened)
            {
                MessageController.instance.SetPanelEnableStatus(false);
                return;
            }
			if (currentPupName != string.Empty)
			{
				if (currentPupName == "Pup_Settings")
				{
					settingsBackButtons[0].SetActive(false);
					settingsBackButtons[1].SetActive(true);
				}
				CloseCurrentPup();
			}
			else
			{
				if (m_PanelOpen == GetPanelByName("Pnl_MainMenu").animator)
				{
					ConfirmExit();
				}
				else
				{
					GameObject.FindGameObjectWithTag("Btn_Exit").GetComponent<Button>().onClick.Invoke();
				}
			}
		}

		if (m_PanelOpen != null || currentUIName != string.Empty)
		{
			m_PanelOpen.gameObject.SetActive(true);
		}
		//}
	}

	public void OpenPanel (Animator anim){
		if (m_PanelOpen == anim)
			return;

		anim.gameObject.SetActive(true);
		GameObject newPreviouslySelected = null;
		if(EventSystem.current.currentSelectedGameObject != null)
			newPreviouslySelected = EventSystem.current.currentSelectedGameObject;

		anim.transform.SetAsLastSibling();

		locked = true;
		
		CloseCurrentPanel();
		
		m_PreviouslySelected = newPreviouslySelected;

		m_PanelOpen = anim;
		currentUIName = anim.gameObject.name;
		m_PanelOpen.SetBool(m_OpenParameterId, true);

		GameObject go = FindFirstEnabledSelectable(anim.gameObject);

		SetSelected(go);

		if(panelsBg)
			panelsBg.SetActive(GetPanelByName(currentUIName).haveBg);

	}

	public void OpenPanel (string UI_Name){
		if (currentUIName == UI_Name)
			return;

		GetPanelByName (UI_Name).animator.gameObject.SetActive(true);
		GameObject newPreviouslySelected = null;
		if(EventSystem.current.currentSelectedGameObject != null)
			newPreviouslySelected = EventSystem.current.currentSelectedGameObject;

		GetPanelByName (UI_Name).animator.transform.SetAsLastSibling();

		locked = true;
		
		CloseCurrentPanel();
		
		m_PreviouslySelected = newPreviouslySelected;

		m_PanelOpen = GetPanelByName (UI_Name).animator;
		currentUIName = UI_Name;
		m_PanelOpen.SetBool(m_OpenParameterId, true);

		GameObject go = FindFirstEnabledSelectable(GetPanelByName (UI_Name).animator.gameObject);

		SetSelected(go);

		if(panelsBg)
			panelsBg.SetActive(GetPanelByName(currentUIName).haveBg);
	}

	public void CloseCurrentPanel(){
		if (m_PanelOpen == null)
		{
			locked = false;
			return;
		}

		m_PanelOpen.SetBool(m_OpenParameterId, false);
		SetSelected(m_PreviouslySelected);
		StartCoroutine(DisableMUIDeleyed(m_PanelOpen));
		m_PanelOpen = null;

		if(GetPanelByName(currentUIName).haveBg && panelsBg)
			panelsBg.SetActive (false);
	}

	public void OpenPup (Animator anim){
		if (m_PupOpen == anim)
			return;

		anim.gameObject.SetActive(true);
		GameObject newPreviouslySelected = null;
		if(EventSystem.current.currentSelectedGameObject != null)
			newPreviouslySelected = EventSystem.current.currentSelectedGameObject;

		anim.transform.SetAsLastSibling();

		CloseCurrentPup();

		m_PreviouslySelected = newPreviouslySelected;

		m_PupOpen = anim;
		currentPupName = anim.gameObject.name;
		m_PupOpen.SetBool(m_OpenParameterId, true);

		GameObject go = FindFirstEnabledSelectable(anim.gameObject);

		SetSelected(go);

		if(GetPupByName(currentPupName).haveBg){
			if(panelsBg.GetComponent<Image> ())
				panelsBg.GetComponent<Image> ().raycastTarget = false;
			pupsBg.SetActive (true);
		}
		else{
			pupsBg.SetActive (false);
		}
	}

	public void OpenPup (string Pup_Name){
		if (currentPupName == Pup_Name)
			return;

		//Debug.Log(Pup_Name);
		GetPupByName (Pup_Name).animator.gameObject.SetActive(true);
		GameObject newPreviouslySelected = null;
		if(EventSystem.current.currentSelectedGameObject != null)
			newPreviouslySelected = EventSystem.current.currentSelectedGameObject;

		GetPupByName (Pup_Name).animator.transform.SetAsLastSibling();

		CloseCurrentPup();

		m_PreviouslySelected = newPreviouslySelected;

		m_PupOpen = GetPupByName (Pup_Name).animator;
		currentPupName = Pup_Name;
		m_PupOpen.SetBool(m_OpenParameterId, true);

		GameObject go = FindFirstEnabledSelectable(GetPupByName (Pup_Name).animator.gameObject);

		SetSelected(go);

		if(GetPupByName(currentPupName).haveBg){
			if(panelsBg.GetComponent<Image> ())
				panelsBg.GetComponent<Image> ().raycastTarget = false;
			pupsBg.SetActive (true);
		}
		else{
			pupsBg.SetActive (false);
		}
	}

	public void CloseCurrentPup(){
		if (m_PupOpen == null)
			return;

		m_PupOpen.SetBool(m_OpenParameterId, false);
		SetSelected(m_PreviouslySelected);
		StartCoroutine(DisableMUIDeleyed(m_PupOpen));
		m_PupOpen = null;
	
		if(GetPupByName(currentPupName).haveBg){
			if(panelsBg.GetComponent<Image> ())
				panelsBg.GetComponent<Image> ().raycastTarget = true;
			pupsBg.SetActive (false);
		}
		currentPupName = string.Empty;
	}

	IEnumerator DisableMUIDeleyed(Animator anim){
		yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo (0).length);
		anim.gameObject.SetActive(false);
		locked = false;
	}

	static GameObject FindFirstEnabledSelectable (GameObject gameObject){
		GameObject go = null;
		var selectables = gameObject.GetComponentsInChildren<Selectable> (true);
		foreach (var selectable in selectables) {
			if (selectable.IsActive () && selectable.IsInteractable ()) {
				go = selectable.gameObject;
				break;
			}
		}
		return go;
	}

	private void SetSelected(GameObject go){
		EventSystem.current.SetSelectedGameObject(go);

		var standaloneInputModule = EventSystem.current.currentInputModule as StandaloneInputModule;
		if (standaloneInputModule != null)
			return;

		EventSystem.current.SetSelectedGameObject(null);
	}

	public void ConfirmExit()
	{
		MessageController.instance.CallYesNo("خروج","از بازی خارج می شوید؟",
			() =>
			{
			#if UNITY_EDITOR && 
				UnityEditor.EditorApplication.isPlaying = false;
			#else
				Application.Quit();
			#endif
			},
			() => { });
	}

	public string GetCurrentUIName()
	{
		return currentUIName;
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment