嵌套列表拖拽事件冲突问题
今天项目中遇到这类需求,我的项目是循环列表与列表的冲突,导致事件被拦截,苦思两天终于搞定,上代码
using UnityEngine.UI; using UnityEngine.EventSystems; using UnityEngine; /// <summary> /// 解决嵌套使用ScrollRect时的Drag冲突问题。请将该脚本放置到内层ScrollRect上(外层的ScrollRect的Drag事件会被内层的拦截) /// </summary> public class UINestedScrollRect : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { /// <summary> /// 外层被拦截需要正常拖动的ScrollRect,可不指定,默认在父对象中找 /// </summary> public ScrollRect m_AnotherScrollRect; /// <summary> /// 当前的ScrollRect(本脚本所放置的物体上)的拖动方向默认为上下拖动,否则为左右拖动型 /// </summary> public bool m_ThisIsUpAndDown = true; private LoopScrollRect m_ThisScrollRect; void Awake() { m_ThisScrollRect = GetComponent<LoopScrollRect>(); if (m_AnotherScrollRect == null) m_AnotherScrollRect = GetComponentsInParent<ScrollRect>()[0]; } public void OnBeginDrag(PointerEventData eventData) { m_AnotherScrollRect.OnBeginDrag(eventData); } public void OnDrag(PointerEventData eventData) { m_AnotherScrollRect.OnDrag(eventData); float angle = Vector2.Angle(eventData.delta, Vector2.left); //判断拖动方向,防止水平与垂直方向同时响应导致的拖动时整个界面都会动 if (angle > 45f && angle < 135f) { m_ThisScrollRect.enabled = !m_ThisIsUpAndDown; m_AnotherScrollRect.enabled = m_ThisIsUpAndDown; } else { m_AnotherScrollRect.enabled = !m_ThisIsUpAndDown; m_ThisScrollRect.enabled = m_ThisIsUpAndDown; } } public void OnEndDrag(PointerEventData eventData) { m_AnotherScrollRect.OnEndDrag(eventData); m_AnotherScrollRect.enabled = true; m_ThisScrollRect.enabled = true; } }
完美解决事件拦截问题,可以根据自己的需求改对应的参数