216 lines
6.5 KiB
C#
216 lines
6.5 KiB
C#
using DG.Tweening;
|
|
using Stary.Evo;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class DeviceXEventHandler : MonoBehaviour
|
|
{
|
|
public bool XBackOrReturn = false;
|
|
private float pressStartTime;
|
|
private bool isPressing = false;
|
|
public HierarchyType hierarchyType;
|
|
|
|
// 在类中添加计时器和计数器变量
|
|
private float _lastJoystickButton2PressTime = 0f;
|
|
private int _joystickButton2PressCount = 0;
|
|
private const float DOUBLE_PRESS_TIME_THRESHOLD = 2f; // 2秒时间阈值
|
|
|
|
/// <summary>
|
|
/// 确定面板的CanvasGroup组件
|
|
/// </summary>
|
|
private CanvasGroup _confirmCGP;
|
|
|
|
/// <summary>
|
|
/// 退出面板的CanvasGroup组件
|
|
/// </summary>
|
|
private CanvasGroup _exitTimeCGP;
|
|
|
|
private Text _exitTimeText;
|
|
private GameObject exitPanelAni;
|
|
|
|
private void Awake()
|
|
{
|
|
var deviceXEventPanelHandle = Resources.Load<GameObject>("DeviceXEventPanel");
|
|
if (deviceXEventPanelHandle)
|
|
{
|
|
var deviceXEventPanel = Instantiate(deviceXEventPanelHandle);
|
|
_confirmCGP = deviceXEventPanel.GetComponent<CanvasGroup>();
|
|
_confirmCGP.alpha = 0f;
|
|
}
|
|
|
|
var exitPanelHandle = Resources.Load<GameObject>("ExitTimePanel");
|
|
if (exitPanelHandle)
|
|
{
|
|
var exitPanel = Instantiate(exitPanelHandle);
|
|
_exitTimeCGP = exitPanel.GetComponent<CanvasGroup>();
|
|
_exitTimeText = exitPanel.GetComponentInChildren<Text>();
|
|
_exitTimeCGP.alpha = 0f;
|
|
exitPanelAni = _exitTimeCGP.GetComponentInChildren<Animator>().gameObject;
|
|
}
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
StringEventSystem.Global.Register<bool>("X_Button", SetXbutton);
|
|
StringEventSystem.Global.Register<HierarchyType>("X_ButtonHierarchyType", SetHierarchyType);
|
|
}
|
|
void OnDestroy()
|
|
{
|
|
StringEventSystem.Global.UnRegister<bool>("X_Button", SetXbutton);
|
|
StringEventSystem.Global.UnRegister<HierarchyType>("X_ButtonHierarchyType", SetHierarchyType);
|
|
}
|
|
|
|
void SetXbutton(bool isActive)
|
|
{
|
|
XBackOrReturn = isActive;
|
|
}
|
|
private void SetHierarchyType(HierarchyType type)
|
|
{
|
|
hierarchyType = type;
|
|
switch (hierarchyType)
|
|
{
|
|
case HierarchyType.Domain:
|
|
XBackOrReturn = false;
|
|
break;
|
|
case HierarchyType.ScenePanel:
|
|
XBackOrReturn = true;
|
|
break;
|
|
case HierarchyType.SceneActive:
|
|
XBackOrReturn = true;
|
|
break;
|
|
}
|
|
}
|
|
void Update()
|
|
{
|
|
if (XBackOrReturn)
|
|
{
|
|
OnJoystickButton2DoubleUpdate(KeyCode.JoystickButton2);
|
|
OnJoystickButton2DoubleUpdate(KeyCode.X);
|
|
}
|
|
else
|
|
{
|
|
OnJoystickButton2ExitTimeUpdate(KeyCode.JoystickButton2);
|
|
OnJoystickButton2ExitTimeUpdate(KeyCode.X);
|
|
}
|
|
}
|
|
|
|
private void OnJoystickButton2ExitTimeUpdate(KeyCode keyCode)
|
|
{
|
|
if (Input.GetKeyDown(keyCode))
|
|
{
|
|
isPressing = true;
|
|
_exitTimeCGP.alpha = 1;
|
|
_exitTimeText.text = "5";
|
|
exitPanelAni.SetActive(true);
|
|
pressStartTime = Time.time;
|
|
}
|
|
|
|
if (isPressing)
|
|
{
|
|
float time = Time.time - pressStartTime;
|
|
_exitTimeText.text = (5 - (int)time).ToString();
|
|
if (time >= 5f)
|
|
{
|
|
Debug.Log("UnityEvo:程序已退出!!");
|
|
Application.Quit();
|
|
}
|
|
}
|
|
|
|
if (Input.GetKeyUp(keyCode))
|
|
{
|
|
isPressing = false;
|
|
_exitTimeText.text = "5";
|
|
_exitTimeCGP.alpha = 0;
|
|
exitPanelAni.SetActive(false);
|
|
}
|
|
}
|
|
|
|
private void OnJoystickButton2DoubleUpdate(KeyCode keyCode)
|
|
{
|
|
// ... existing code ...
|
|
|
|
// JoystickButton2 双击检测
|
|
if (Input.GetKeyDown(keyCode))
|
|
{
|
|
float currentTime = Time.time;
|
|
|
|
// 如果是第一次按下或者在时间阈值内再次按下
|
|
if (_joystickButton2PressCount == 0 ||
|
|
(currentTime - _lastJoystickButton2PressTime) <= DOUBLE_PRESS_TIME_THRESHOLD)
|
|
{
|
|
_joystickButton2PressCount++;
|
|
_lastJoystickButton2PressTime = currentTime;
|
|
// 如果达到一次按下,执行单击逻辑
|
|
if (_joystickButton2PressCount == 1)
|
|
{
|
|
_confirmCGP.alpha = 1f;
|
|
_confirmCGP.DOFade(0f, 2f).OnKill(() => { _confirmCGP.alpha = 0; });
|
|
}
|
|
|
|
// 如果达到两次按下,执行双击逻辑
|
|
if (_joystickButton2PressCount >= 2)
|
|
{
|
|
_confirmCGP.DOKill();
|
|
OnJoystickButton2DoublePress();
|
|
_joystickButton2PressCount = 0; // 重置计数器
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// 超过时间阈值,重置计数器
|
|
_joystickButton2PressCount = 1;
|
|
_lastJoystickButton2PressTime = currentTime;
|
|
}
|
|
}
|
|
|
|
// 检查是否超过时间阈值需要重置计数器
|
|
if (_joystickButton2PressCount > 0 &&
|
|
(Time.time - _lastJoystickButton2PressTime) > DOUBLE_PRESS_TIME_THRESHOLD)
|
|
{
|
|
_confirmCGP.DOKill();
|
|
_joystickButton2PressCount = 0;
|
|
}
|
|
}
|
|
|
|
// 双击事件处理方法
|
|
private void OnJoystickButton2DoublePress()
|
|
{
|
|
// 在这里执行双击后的逻辑
|
|
Debug.Log("JoystickButton2 双击事件触发!");
|
|
switch (hierarchyType)
|
|
{
|
|
case HierarchyType.Domain:
|
|
XBackOrReturn=false;
|
|
// 域层级双击逻辑
|
|
break;
|
|
case HierarchyType.ScenePanel:
|
|
// 场景面板层级双击逻辑
|
|
XBackOrReturn=false;
|
|
StringEventSystem.Global.Send("Rollback");
|
|
break;
|
|
case HierarchyType.SceneActive:
|
|
// 场景激活层级双击逻辑
|
|
XBackOrReturn=true;
|
|
StringEventSystem.Global.Send("UnloadCurrentScene");
|
|
break;
|
|
}
|
|
}
|
|
|
|
public enum HierarchyType
|
|
{
|
|
/// <summary>
|
|
/// 域层级
|
|
/// </summary>
|
|
Domain,
|
|
|
|
/// <summary>
|
|
/// 场景面板层级
|
|
/// </summary>
|
|
ScenePanel,
|
|
|
|
/// <summary>
|
|
/// 场景激活层级
|
|
/// </summary>
|
|
SceneActive
|
|
}
|
|
} |