GameMenuView.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. public Button[] funcItemButtons;
  9. private int funcItemButtonIndex = -1;
  10. void UpdateFuncItemButtonIndex(int deltaValue) {
  11. if (funcItemButtonIndex == -1)
  12. {
  13. funcItemButtonIndex = 0;
  14. return;
  15. }
  16. funcItemButtonIndex += deltaValue;
  17. while (funcItemButtonIndex < 0) funcItemButtonIndex += funcItemButtons.Length;
  18. funcItemButtonIndex %= funcItemButtons.Length;
  19. }
  20. void Update()
  21. {
  22. if (Input.GetKeyDown(KeyCode.UpArrow))
  23. {
  24. UpdateFuncItemButtonIndex(-1);
  25. SetSelectable(funcItemButtons[funcItemButtonIndex]);
  26. AudioMgr.ins.PlayBtn();
  27. }
  28. if (Input.GetKeyDown(KeyCode.DownArrow))
  29. {
  30. UpdateFuncItemButtonIndex(1);
  31. SetSelectable(funcItemButtons[funcItemButtonIndex]);
  32. AudioMgr.ins.PlayBtn();
  33. }
  34. if (Input.GetKeyDown(KeyCode.JoystickButton0) || Input.GetKeyDown(KeyCode.Return))
  35. {
  36. if (funcItemButtonIndex == -1) DuckHunter.TextSmartBowTip.Show("尚未选择按钮");
  37. else OnClikc_Confirm();
  38. }
  39. if (Input.GetKeyDown(KeyCode.Escape))
  40. {
  41. funcItemButtonIndex = -1;
  42. SetSelectable(null);
  43. AudioMgr.ins.PlayBtn();
  44. }
  45. }
  46. #region 选项功能
  47. Selectable _targetSelectable;
  48. Vector3 _targetlocalScale;
  49. Color _targetColor;
  50. Selectable.Transition _btnTransition;
  51. void SetSelectable(Selectable selectable) {
  52. if (selectable == _targetSelectable) return;
  53. ResumeTarget();
  54. if (!selectable) {
  55. _targetSelectable = null;
  56. return;
  57. }
  58. Button btn = selectable.GetComponent<Button>();
  59. if (!btn) return;
  60. if (!btn.interactable) return;
  61. _targetSelectable = selectable;
  62. RectTransform rtf = _targetSelectable.transform as RectTransform;
  63. _btnTransition = btn.transition;
  64. if (btn.transition == Selectable.Transition.ColorTint) { //变色和放大
  65. _targetlocalScale = rtf.localScale;
  66. rtf.localScale = rtf.localScale * 1.2f;
  67. _targetColor = _targetSelectable.targetGraphic.color;
  68. var newcolor = _targetSelectable.targetGraphic.color;
  69. newcolor.a = 0.4f;
  70. _targetSelectable.targetGraphic.color = newcolor;
  71. }
  72. }
  73. void ResumeTarget()
  74. {
  75. if (_targetSelectable) {
  76. if (_btnTransition == Selectable.Transition.ColorTint) {
  77. _targetSelectable.transform.localScale = _targetlocalScale;
  78. _targetSelectable.targetGraphic.color = _targetColor;
  79. }
  80. }
  81. }
  82. void OnClikc_Confirm()
  83. {
  84. try
  85. {
  86. if (_targetSelectable && _targetSelectable.interactable) {
  87. Button btn = _targetSelectable.GetComponent<Button>();
  88. btn.onClick.Invoke();
  89. }
  90. }
  91. catch (System.Exception e)
  92. {
  93. Debug.LogError(e.Message);
  94. Debug.LogError(e.StackTrace);
  95. }
  96. }
  97. #endregion
  98. }