NRHomeMenu.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace NRKernal
  4. {
  5. /// <summary> A nr home menu. </summary>
  6. public class NRHomeMenu : MonoBehaviour
  7. {
  8. /// <summary> The confirm control. </summary>
  9. public Button confirmBtn;
  10. /// <summary> The cancel control. </summary>
  11. public Button cancelBtn;
  12. /// <summary> The instance. </summary>
  13. private static NRHomeMenu m_Instance;
  14. /// <summary> Full pathname of the menu prefab file. </summary>
  15. private static string m_MenuPrefabPath = "NRUI/NRHomeMenu";
  16. /// <summary> Transform of center camera. </summary>
  17. private Transform CameraCenter { get { return NRInput.CameraCenter; } }
  18. /// <summary> True if is showing, false if not. </summary>
  19. public static bool IsShowing { get; private set; }
  20. /// <summary> Action to excute on home menu show. </summary>
  21. public static System.Action OnHomeMenuShow;
  22. /// <summary> Action to excute on home menu hide. </summary>
  23. public static System.Action OnHomeMenuHide;
  24. /// <summary> Starts this object. </summary>
  25. void Start()
  26. {
  27. confirmBtn.onClick.AddListener(OnComfirmButtonClick);
  28. cancelBtn.onClick.AddListener(OnCancelButtonClick);
  29. }
  30. /// <summary> Updates this object. </summary>
  31. void Update()
  32. {
  33. if (IsShowing && NRInput.RaycastMode == RaycastModeEnum.Laser)
  34. {
  35. FollowCamera();
  36. }
  37. }
  38. /// <summary> Executes the 'comfirm button click' action. </summary>
  39. private void OnComfirmButtonClick()
  40. {
  41. Hide();
  42. NRAppManager.QuitApplication();
  43. }
  44. /// <summary> Executes the 'cancel button click' action. </summary>
  45. private void OnCancelButtonClick()
  46. {
  47. Hide();
  48. }
  49. /// <summary> Follow camera. </summary>
  50. private void FollowCamera()
  51. {
  52. if (m_Instance && CameraCenter)
  53. {
  54. m_Instance.transform.position = CameraCenter.position;
  55. m_Instance.transform.rotation = CameraCenter.rotation;
  56. }
  57. }
  58. /// <summary> Creates the menu. </summary>
  59. private static void CreateMenu()
  60. {
  61. GameObject menuPrefab = Resources.Load<GameObject>(m_MenuPrefabPath);
  62. if (menuPrefab == null)
  63. {
  64. NRDebugger.Error("Can not find prefab: " + m_MenuPrefabPath);
  65. return;
  66. }
  67. GameObject menuGo = Instantiate(menuPrefab);
  68. m_Instance = menuGo.GetComponent<NRHomeMenu>();
  69. if (m_Instance)
  70. {
  71. DontDestroyOnLoad(menuGo);
  72. }
  73. else
  74. {
  75. Destroy(menuGo);
  76. }
  77. }
  78. /// <summary> Toggles this object. </summary>
  79. public static void Toggle()
  80. {
  81. if (IsShowing)
  82. {
  83. Hide();
  84. }
  85. else
  86. {
  87. Show();
  88. }
  89. }
  90. /// <summary> Shows this object. </summary>
  91. public static void Show()
  92. {
  93. if (m_Instance == null)
  94. {
  95. CreateMenu();
  96. }
  97. if (m_Instance)
  98. {
  99. m_Instance.gameObject.SetActive(true);
  100. IsShowing = true;
  101. if (NRInput.RaycastMode == RaycastModeEnum.Gaze)
  102. {
  103. m_Instance.FollowCamera();
  104. }
  105. if (OnHomeMenuShow != null)
  106. {
  107. OnHomeMenuShow();
  108. }
  109. }
  110. }
  111. /// <summary> Hides this object. </summary>
  112. public static void Hide()
  113. {
  114. if (m_Instance)
  115. {
  116. m_Instance.gameObject.SetActive(false);
  117. IsShowing = false;
  118. if (OnHomeMenuHide != null)
  119. {
  120. OnHomeMenuHide();
  121. }
  122. }
  123. }
  124. }
  125. }