GameMenuView.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.EventSystems;
  6. public class GameMenuView : MonoBehaviour
  7. {
  8. [SerializeField] GameObject funcView;
  9. Button[] funcItemButtons;
  10. [SerializeField] Text keyCodeText;
  11. void Awake()
  12. {
  13. funcItemButtons = funcView.transform.Find("Items").GetComponentsInChildren<Button>();
  14. for (int i = 0; i < funcItemButtons.Length; i++)
  15. {
  16. int id = i;
  17. funcItemButtons[i].onClick.AddListener(delegate() {
  18. SelectItem(id, true);
  19. ConfirmItem(id);
  20. });
  21. }
  22. SelectItem(0, true);
  23. }
  24. void Update()
  25. {
  26. if (Input.GetKeyDown(KeyCode.UpArrow))
  27. {
  28. SelectItem(-1);
  29. AudioMgr.ins.PlayBtn();
  30. }
  31. if (Input.GetKeyDown(KeyCode.DownArrow))
  32. {
  33. SelectItem(+1);
  34. AudioMgr.ins.PlayBtn();
  35. }
  36. if (Input.GetKeyDown(KeyCode.JoystickButton0) || Input.GetKeyDown(KeyCode.Return))
  37. {
  38. if (funcView.activeSelf) {
  39. ConfirmItem(funcItemIndex);
  40. }
  41. if (recordItemIns && recordItemOkBtn.interactable) {
  42. recordItemOkBtn.onClick.Invoke();
  43. }
  44. }
  45. if (Input.GetKeyDown(KeyCode.Escape))
  46. {
  47. if (recordItemIns && recordItemBackBtn.interactable) {
  48. recordItemBackBtn.onClick.Invoke();
  49. }
  50. }
  51. }
  52. void OnGUI()
  53. {
  54. if (Input.anyKeyDown)
  55. {
  56. Event e = Event.current;
  57. if (e.isKey)
  58. {
  59. keyCodeText.text = string.Format("按下的键值:[{0}]", e.character);
  60. }
  61. }
  62. }
  63. void ShowFuncView(bool active)
  64. {
  65. funcView.SetActive(active);
  66. }
  67. int funcItemIndex = 0;
  68. void SelectItem(int deltaIndex, bool isFixed = false)
  69. {
  70. if (!funcView.activeSelf) return;
  71. funcItemIndex += deltaIndex;
  72. if (funcItemIndex < 0) {
  73. funcItemIndex = funcItemButtons.Length - 1;
  74. } else if (funcItemIndex >= funcItemButtons.Length) {
  75. funcItemIndex = 0;
  76. }
  77. if (isFixed) funcItemIndex = deltaIndex;
  78. for (int i = 0; i < funcItemButtons.Length; i++) {
  79. funcItemButtons[i].GetComponentInChildren<Image>().color = i == funcItemIndex ? Color.yellow : Color.white;
  80. Text ttt = funcItemButtons[i].GetComponentInChildren<Text>();
  81. if (ttt) ttt.color = i == funcItemIndex ? Color.yellow : Color.white;
  82. }
  83. }
  84. void ConfirmItem(int id)
  85. {
  86. if (!funcView.activeSelf) return;
  87. AudioMgr.ins.PlayBtn();
  88. switch (id)
  89. {
  90. case 1:
  91. ShowFuncView(false);
  92. GameObject v1 = DeviceCalibrateView.Create(DeviceCalibrateItem.Gyr);
  93. RecordItem(v1);
  94. break;
  95. case 2:
  96. ShowFuncView(false);
  97. GameObject v2 = DeviceCalibrateView.Create(DeviceCalibrateItem.Mag);
  98. RecordItem(v2);
  99. break;
  100. case 3:
  101. AimHandler.ins.DoIdentity();
  102. break;
  103. case 4:
  104. if (LoginMgr.myUserInfo.arrowAccValue == 16) {
  105. LoginMgr.myUserInfo.arrowAccValue = 64;
  106. } else {
  107. LoginMgr.myUserInfo.arrowAccValue = 16;
  108. }
  109. LoginMgr.myUserInfo.Save();
  110. RenderArrowAcc();
  111. break;
  112. case 0:
  113. Application.Quit();
  114. break;
  115. }
  116. }
  117. public void RenderArrowAcc()
  118. {
  119. funcItemButtons[4].GetComponentInChildren<Text>().text = LoginMgr.myUserInfo.arrowAccValue + "G使用中";
  120. }
  121. GameObject recordItemIns;
  122. Button recordItemOkBtn;
  123. Button recordItemBackBtn;
  124. void RecordItem(GameObject itemIns)
  125. {
  126. ClearRecordItem();
  127. recordItemIns = itemIns;
  128. (recordItemOkBtn, recordItemBackBtn) = itemIns.GetComponent<DeviceCalibrateView>().GetInterfaceBtns();
  129. recordItemBackBtn.onClick.AddListener(delegate() {
  130. ShowFuncView(true);
  131. });
  132. }
  133. void ClearRecordItem()
  134. {
  135. if (recordItemIns) Destroy(recordItemIns);
  136. }
  137. }