Unity在屏幕上滑动

使用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;
    }
}
相关推荐
黄思搏1 天前
基于标注平台数据的 Unity UI 自动化构建工作流设计与工程实践
ui·unity·蓝湖·vectoui
羊羊20351 天前
开发手札:Unity6000与Android交互
android·unity·android-studio
Zarek枫煜2 天前
C3 编程语言 - 现代 C 的进化之选
c语言·开发语言·青少年编程·rust·游戏引擎
Sator12 天前
Unity AStarPath的踩坑点
unity
榮華2 天前
DOTA全图透视辅助下载DOTA全图科技辅助下载DOTA外挂下载魔兽争霸WAR3全图下载
数据库·科技·游戏·游戏引擎·游戏程序·ai编程·腾讯云ai代码助手
RPGMZ2 天前
RPGMakerMZ 游戏引擎 野外采集点制作
javascript·游戏·游戏引擎·rpgmz·野外采集点
星河耀银海2 天前
Unity基础:摄像机Camera的参数设置与视角控制
unity·游戏引擎·lucene
星河耀银海2 天前
Unity基础:Transform组件的位移、旋转与缩放详解
unity·游戏引擎·lucene
weixin_409383123 天前
godot 击败敌人后增加经验的脚本
游戏引擎·godot
海清河晏1113 天前
数据结构 | 单链表
数据结构·unity·dreamweaver