使用ui的Image,放在最上层,进行检测是否在ui上滑动
创建一个Image,设置为全屏,挂在下方脚本即可。
cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class SwipeDetector03 : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
[Header("滑动设置")]
[SerializeField] private float swipeThreshold = 50f; // 滑动阈值(像素)
private Vector2 startPosition;
private bool isDragging = false;
// 开始拖拽
public void OnBeginDrag(PointerEventData eventData)
{
startPosition = eventData.position;
isDragging = true;
//Debug.Log("开始滑动");
}
// 拖拽中
public void OnDrag(PointerEventData eventData)
{
// 可以在这里添加实时拖拽效果
}
// 结束拖拽
public void OnEndDrag(PointerEventData eventData)
{
if (!isDragging) return;
Vector2 endPosition = eventData.position;
float swipeDistance = endPosition.x - startPosition.x;
//Debug.Log("滑动距离: " + swipeDistance);
// 判断滑动距离是否超过阈值
if (Mathf.Abs(swipeDistance) > swipeThreshold)
{
if (swipeDistance > 0)
{
// 向右滑动
//Debug.Log("下一页");
// 在这里调用你的下一页逻辑
(UiPanel.Instance.GetUiPanelForType(UiPanelType.TwoLevelPanel03) as TwoLevelPanel03).
NextPanel();
}
else
{
// 向左滑动
//Debug.Log("上一页");
// 在这里调用你的上一页逻辑
(UiPanel.Instance.GetUiPanelForType(UiPanelType.TwoLevelPanel03) as TwoLevelPanel03).
PreviousPanel();
}
}
isDragging = false;
}
}