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;
    }
}
相关推荐
mxwin8 小时前
Unity Shader 半透明物体为什么不能写入深度缓冲?
unity·游戏引擎·shader
晚枫歌F9 小时前
三层时间轮的实现
网络·unity·游戏引擎
咸鱼永不翻身10 小时前
Lua脚本事件检查工具
unity·lua·工具
leo__52012 小时前
单载波中继系统资源分配算法MATLAB仿真程序
算法·matlab·unity
努力长头发的程序猿13 小时前
Unity使用ScriptableObject序列化资源
unity·游戏引擎
mxwin13 小时前
Unity Shader 手写基于 PBR 的 URP Lit Shader 核心光照计算
unity·游戏引擎·shader
小贺儿开发13 小时前
Unity3D 智能云端数字标牌系统
unity·阿里云·人机交互·视频·oss·广告·互动
魔士于安14 小时前
Unity windows 同步 异步 打开文件文件夹工具
游戏·unity·游戏引擎·贴图·模型
笑虾14 小时前
cocos2d-x lua 加载 Cocos Studio 导出的 csb
游戏引擎·lua·cocos2d
魔士于安14 小时前
unity lowpoly 风格 城市 建筑 道路 交通标志
游戏·unity·游戏引擎·贴图·模型