| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- public class GameMenuView : MonoBehaviour
- {
- public Button[] funcItemButtons;
- private int funcItemButtonIndex = -1;
- void UpdateFuncItemButtonIndex(int deltaValue) {
- if (funcItemButtonIndex == -1)
- {
- funcItemButtonIndex = 0;
- return;
- }
- funcItemButtonIndex += deltaValue;
- while (funcItemButtonIndex < 0) funcItemButtonIndex += funcItemButtons.Length;
- funcItemButtonIndex %= funcItemButtons.Length;
- }
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.UpArrow))
- {
- UpdateFuncItemButtonIndex(-1);
- SetSelectable(funcItemButtons[funcItemButtonIndex]);
- AudioMgr.ins.PlayBtn();
- }
- if (Input.GetKeyDown(KeyCode.DownArrow))
- {
- UpdateFuncItemButtonIndex(1);
- SetSelectable(funcItemButtons[funcItemButtonIndex]);
- AudioMgr.ins.PlayBtn();
- }
- if (Input.GetKeyDown(KeyCode.JoystickButton0) || Input.GetKeyDown(KeyCode.Return))
- {
- if (funcItemButtonIndex == -1) DuckHunter.TextSmartBowTip.Show("尚未选择按钮");
- else OnClikc_Confirm();
- }
- if (Input.GetKeyDown(KeyCode.Escape))
- {
- funcItemButtonIndex = -1;
- SetSelectable(null);
- AudioMgr.ins.PlayBtn();
- }
- }
- #region 选项功能
- Selectable _targetSelectable;
- Vector3 _targetlocalScale;
- Color _targetColor;
- Selectable.Transition _btnTransition;
- void SetSelectable(Selectable selectable) {
- if (selectable == _targetSelectable) return;
- ResumeTarget();
- if (!selectable) {
- _targetSelectable = null;
- return;
- }
- Button btn = selectable.GetComponent<Button>();
- if (!btn) return;
- if (!btn.interactable) return;
- _targetSelectable = selectable;
- RectTransform rtf = _targetSelectable.transform as RectTransform;
- _btnTransition = btn.transition;
- if (btn.transition == Selectable.Transition.ColorTint) { //变色和放大
- _targetlocalScale = rtf.localScale;
- rtf.localScale = rtf.localScale * 1.2f;
- _targetColor = _targetSelectable.targetGraphic.color;
- var newcolor = _targetSelectable.targetGraphic.color;
- newcolor.a = 0.4f;
- _targetSelectable.targetGraphic.color = newcolor;
- }
- }
- void ResumeTarget()
- {
- if (_targetSelectable) {
- if (_btnTransition == Selectable.Transition.ColorTint) {
- _targetSelectable.transform.localScale = _targetlocalScale;
- _targetSelectable.targetGraphic.color = _targetColor;
- }
- }
- }
- void OnClikc_Confirm()
- {
- try
- {
- if (_targetSelectable && _targetSelectable.interactable) {
- Button btn = _targetSelectable.GetComponent<Button>();
- btn.onClick.Invoke();
- }
- }
- catch (System.Exception e)
- {
- Debug.LogError(e.Message);
- Debug.LogError(e.StackTrace);
- }
- }
- #endregion
- }
|