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