OvertakingLegend/Assets/Script/ScreenOperation.cs

94 lines
2.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
/// <summary>
/// 屏幕操作
/// </summary>
public class ScreenOperation
{
/// <summary>
/// 点击次数
/// </summary>
private int tapNumber;
/// <summary>
/// 用于双击的变量
/// </summary>
private float lastTapTime = 0f;
/// <summary>
/// 双击间隔阈值(秒)
/// </summary>
private float doubleTapThreshold = 0.3f;
/// <summary>
/// 手指开始位置
/// </summary>
private Vector3 scenePos;
/// <summary>
/// 滑动屏幕
/// </summary>
public void SliderScreen(UnityAction leftAction, UnityAction rightAction)
{
if (Scenes.Instance.gameScene != null && Scenes.Instance.e_GameState == E_GameState.Gameing)
{
if (Input.touchCount == 1)
{
if (Input.touches[0].phase == TouchPhase.Began)
{
scenePos = Input.touches[0].position;
}
if (Input.touches[0].phase == TouchPhase.Ended && Input.touches[0].phase != TouchPhase.Canceled)
{
Vector2 pos = Input.touches[0].position;
if (Mathf.Abs(scenePos.x - pos.x) > Mathf.Abs(scenePos.y - pos.y))
{
// 手指向左滑动
if (scenePos.x > pos.x)
{
tapNumber = 0;
leftAction?.Invoke();
}
else if (scenePos.x < pos.x)
{
tapNumber = 0;
rightAction?.Invoke();
}
}
}
}
}
}
/// <summary>
/// 双击屏幕
/// </summary>
public void DetectDoubleTap(UnityAction doubleTapAction)
{
if (Scenes.Instance.gameScene != null && Scenes.Instance.e_GameState == E_GameState.Gameing)
{
if (Input.touchCount == 1) // 如果正在滑动则不触发双击事件
{
if (Input.touches[0].phase == TouchPhase.Began)
{
tapNumber++;
// 检查双击时间间隔
if (Time.time - lastTapTime < doubleTapThreshold)
{
if (tapNumber >= 2)
{
// 双击触发事件
doubleTapAction?.Invoke();
}
}
lastTapTime = Time.time;
}
}
}
}
}