123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- using UnityEngine;
- using UnityEngine.UI;
- namespace NRKernal
- {
- /// <summary> A nr home menu. </summary>
- public class NRHomeMenu : MonoBehaviour
- {
- /// <summary> The confirm control. </summary>
- public Button confirmBtn;
- /// <summary> The cancel control. </summary>
- public Button cancelBtn;
- /// <summary> The instance. </summary>
- private static NRHomeMenu m_Instance;
- /// <summary> Full pathname of the menu prefab file. </summary>
- private static string m_MenuPrefabPath = "NRUI/NRHomeMenu";
- /// <summary> Transform of center camera. </summary>
- private Transform CameraCenter { get { return NRInput.CameraCenter; } }
- /// <summary> True if is showing, false if not. </summary>
- public static bool IsShowing { get; private set; }
- /// <summary> Action to excute on home menu show. </summary>
- public static System.Action OnHomeMenuShow;
- /// <summary> Action to excute on home menu hide. </summary>
- public static System.Action OnHomeMenuHide;
- /// <summary> Starts this object. </summary>
- void Start()
- {
- confirmBtn.onClick.AddListener(OnComfirmButtonClick);
- cancelBtn.onClick.AddListener(OnCancelButtonClick);
- }
- /// <summary> Updates this object. </summary>
- void Update()
- {
- if (IsShowing && NRInput.RaycastMode == RaycastModeEnum.Laser)
- {
- FollowCamera();
- }
- }
- /// <summary> Executes the 'comfirm button click' action. </summary>
- private void OnComfirmButtonClick()
- {
- Hide();
- NRAppManager.QuitApplication();
- }
- /// <summary> Executes the 'cancel button click' action. </summary>
- private void OnCancelButtonClick()
- {
- Hide();
- }
- /// <summary> Follow camera. </summary>
- private void FollowCamera()
- {
- if (m_Instance && CameraCenter)
- {
- m_Instance.transform.position = CameraCenter.position;
- m_Instance.transform.rotation = CameraCenter.rotation;
- }
- }
- /// <summary> Creates the menu. </summary>
- private static void CreateMenu()
- {
- GameObject menuPrefab = Resources.Load<GameObject>(m_MenuPrefabPath);
- if (menuPrefab == null)
- {
- NRDebugger.Error("Can not find prefab: " + m_MenuPrefabPath);
- return;
- }
- GameObject menuGo = Instantiate(menuPrefab);
- m_Instance = menuGo.GetComponent<NRHomeMenu>();
- if (m_Instance)
- {
- DontDestroyOnLoad(menuGo);
- }
- else
- {
- Destroy(menuGo);
- }
- }
- /// <summary> Toggles this object. </summary>
- public static void Toggle()
- {
- if (IsShowing)
- {
- Hide();
- }
- else
- {
- Show();
- }
- }
- /// <summary> Shows this object. </summary>
- public static void Show()
- {
- if (m_Instance == null)
- {
- CreateMenu();
- }
- if (m_Instance)
- {
- m_Instance.gameObject.SetActive(true);
- IsShowing = true;
- if (NRInput.RaycastMode == RaycastModeEnum.Gaze)
- {
- m_Instance.FollowCamera();
- }
- if (OnHomeMenuShow != null)
- {
- OnHomeMenuShow();
- }
- }
- }
- /// <summary> Hides this object. </summary>
- public static void Hide()
- {
- if (m_Instance)
- {
- m_Instance.gameObject.SetActive(false);
- IsShowing = false;
- if (OnHomeMenuHide != null)
- {
- OnHomeMenuHide();
- }
- }
- }
- }
- }
|