嵌套ScrollView 左右滑动不影响上下滑动

本文介绍了一个Unity中实现的UI多方向拖拽滚动视图的脚本。该脚本能够根据用户的触摸输入判断拖拽方向,并相应地启用水平或垂直滚动视图。文章详细展示了如何通过计算向量点乘和角度来确定拖拽方向。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using UnityEngine;
using System.Collections;

public class UIMultDragScrollView : MonoBehaviour {
    public UIScrollView hScrollView;
    public UIScrollView vScrollView;
    public UIDragScrollView dragScrollView;
    public float directionTolerance = 0.2f;
    bool dragCheck = true; 
    public float direcDegle = 45;
    public void OnDrag(Vector2 delta)
    {
        int sd = -1;
        if (dragCheck)
        {
            dragCheck = false;
            sd = GetSwipeDirection(UICamera.currentTouch.delta, directionTolerance);
        }
        Debug.Log("swp"+sd);
        if( sd == 0)
        {
            Debug.Log("左");
            dragScrollView.scrollView = hScrollView;
            vScrollView.movement = UIScrollView.Movement.Custom;
            vScrollView.currentMomentum = new Vector3(0, 0);
            hScrollView.movement = UIScrollView.Movement.Horizontal;
            hScrollView.enabled = true;
            vScrollView.enabled = false;
            vScrollView.Press(false);
            hScrollView.Press(true);
           // hScrollView.Drag();
        }
        if (sd == 1)
        {
            Debug.Log("上");
            dragScrollView.scrollView = vScrollView;
            hScrollView.enabled = false;
            hScrollView.movement = UIScrollView.Movement.Custom;
            hScrollView.currentMomentum = new Vector3(0, 0);
            vScrollView.movement = UIScrollView.Movement.Vertical;
            vScrollView.enabled = true;
            hScrollView.Press(false);
            vScrollView.Press(true);
           // vScrollView.Drag();
        }
    }
    void OnPress(bool isDown)
    {
        if (isDown)
        {
            dragCheck = true;
        }
    }
    int GetSwipeDirection(Vector3 dir, float tolerance)
    {
        float minSwipeDot = Mathf.Clamp01(1.0f - tolerance);
        if (Mathf.Rad2Deg * (Mathf.Acos(Vector2.Dot(dir, Vector2.right) / (dir.magnitude * Vector2.right.magnitude))) <= direcDegle
            && Vector2.Dot(dir, Vector2.right) >= minSwipeDot)
            return 0;
        if (Mathf.Rad2Deg * (Mathf.Acos(Vector2.Dot(dir, -Vector2.right) / (dir.magnitude * (-Vector2.right).magnitude))) <= direcDegle
            && Vector2.Dot(dir, -Vector2.right) >= minSwipeDot)
            return 0;
        if (Mathf.Rad2Deg * (Mathf.Acos(Vector2.Dot(dir, Vector2.up) / (dir.magnitude * Vector2.up.magnitude))) <= 90 - direcDegle
            && Vector2.Dot(dir, Vector2.up) >= minSwipeDot)
            return 1;
        if (Mathf.Rad2Deg * (Mathf.Acos(Vector2.Dot(dir, -Vector2.up) / (dir.magnitude * (-Vector2.up).magnitude))) <= 90 - direcDegle
            && Vector2.Dot(dir, -Vector2.up) >= minSwipeDot)
            return 1;
        return -1;
    }
}

  

转载于:https://www.cnblogs.com/hersen/p/3946390.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值