123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- using System.Collections;
- using System.Collections.Generic;
- public class PanelManager : MonoBehaviour {
- public Animator initiallyOpen;
- private int m_OpenParameterId;
- private Animator m_Open;
- private GameObject m_PreviouslySelected;
- const string k_OpenTransitionName = "Open";
- const string k_ClosedStateName = "Closed";
- public void OnEnable()
- {
- m_OpenParameterId = Animator.StringToHash (k_OpenTransitionName);
- if (initiallyOpen == null)
- return;
- OpenPanel(initiallyOpen);
- }
- public void OpenPanel (Animator anim)
- {
- if (m_Open == anim)
- return;
- anim.gameObject.SetActive(true);
- var newPreviouslySelected = EventSystem.current.currentSelectedGameObject;
- anim.transform.SetAsLastSibling();
- CloseCurrent();
- m_PreviouslySelected = newPreviouslySelected;
- m_Open = anim;
- m_Open.SetBool(m_OpenParameterId, true);
- GameObject go = FindFirstEnabledSelectable(anim.gameObject);
- SetSelected(go);
- }
- 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;
- }
- public void CloseCurrent()
- {
- if (m_Open == null)
- return;
- m_Open.SetBool(m_OpenParameterId, false);
- SetSelected(m_PreviouslySelected);
- StartCoroutine(DisablePanelDeleyed(m_Open));
- m_Open = null;
- }
- IEnumerator DisablePanelDeleyed(Animator anim)
- {
- bool closedStateReached = false;
- bool wantToClose = true;
- while (!closedStateReached && wantToClose)
- {
- if (!anim.IsInTransition(0))
- closedStateReached = anim.GetCurrentAnimatorStateInfo(0).IsName(k_ClosedStateName);
- wantToClose = !anim.GetBool(m_OpenParameterId);
- yield return new WaitForEndOfFrame();
- }
- if (wantToClose)
- anim.gameObject.SetActive(false);
- }
- private void SetSelected(GameObject go)
- {
- EventSystem.current.SetSelectedGameObject(go);
- }
- }
|