using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
///
/// 屏幕操作
///
public class ScreenOperation
{
///
/// 点击次数
///
private int tapNumber;
///
/// 用于双击的变量
///
private float lastTapTime = 0f;
///
/// 双击间隔阈值(秒)
///
private float doubleTapThreshold = 0.3f;
///
/// 手指开始位置
///
private Vector3 scenePos;
///
/// 滑动屏幕
///
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();
}
}
}
}
}
}
///
/// 双击屏幕
///
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;
}
}
}
}
}