GameMenuView.cs 3.1 KB

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