当前位置:网站首页>Unity中实现嵌套滑动框
Unity中实现嵌套滑动框
2022-06-09 16:06:00 【Hello Bug.】
一:效果演示

二:前言
Unity中如果两个ScrollRect嵌套在一起,后面的会挡住前面的,当在二级列表区域拖动时导致一级列表无法滑动
三:实现原理
根据滑动的方向,来进行事件的渗透传递,如果当前滑动方向与组件的滑动方向不一致则把事件传递给父级列表
UGUI提供了事件执行的方法:ExecuteEvents.Execute
四:代码实现
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/// <summary>
/// 嵌套滑动框
/// </summary>
/// 支持嵌套滑动列表的滑动
public class ScrollRectNested : ScrollRect
{
/// <summary>
/// 滑动方向
/// </summary>
enum EScrollDir
{
Horizontal,
Vertical,
}
ScrollRectNested m_SRPro;//不同滑动方向的滑动框
EScrollDir m_ScrollDir;//组件滑动方向
EScrollDir m_CurScrollDir;//当前滑动方向
protected override void Awake()
{
base.Awake();
Transform parent = transform.parent;
if (parent != null)
{
m_SRPro = parent.GetComponentInParent<ScrollRectNested>();
}
m_ScrollDir = horizontal ? EScrollDir.Horizontal : EScrollDir.Vertical;
}
public override void OnBeginDrag(PointerEventData eventData)
{
if (m_SRPro != null)
{
m_CurScrollDir = Mathf.Abs(eventData.delta.x) >= Mathf.Abs(eventData.delta.y)
? EScrollDir.Horizontal
: EScrollDir.Vertical;
if (m_CurScrollDir != m_ScrollDir)
{
ExecuteEvents.Execute(m_SRPro.gameObject, eventData, ExecuteEvents.beginDragHandler);
return;
}
}
base.OnBeginDrag(eventData);
}
public override void OnDrag(PointerEventData eventData)
{
if (m_SRPro != null)
{
if (m_CurScrollDir != m_ScrollDir)
{
ExecuteEvents.Execute(m_SRPro.gameObject, eventData, ExecuteEvents.dragHandler);
return;
}
}
base.OnDrag(eventData);
}
public override void OnEndDrag(PointerEventData eventData)
{
if (m_SRPro != null)
{
if (m_CurScrollDir != m_ScrollDir)
{
ExecuteEvents.Execute(m_SRPro.gameObject, eventData, ExecuteEvents.endDragHandler);
return;
}
}
base.OnEndDrag(eventData);
}
public override void OnScroll(PointerEventData data)
{
if (m_SRPro != null)
{
if (m_CurScrollDir != m_ScrollDir)
{
ExecuteEvents.Execute(m_SRPro.gameObject, data, ExecuteEvents.scrollHandler);
return;
}
}
base.OnScroll(data);
}
}边栏推荐
- Solution reference for web page loading waiting (ttfb) taking too long
- Execution strategy of application software efficiency test
- 从 0 到 1,探究百亿流量验证下的 MVVM 框架设计
- Kubernetes core concepts
- JWT mind map
- Jpex launches bayc mayc contract series welcome experience
- [SEETF]Super Secure Requests Forwarder
- Reconstruction essentials learning
- 检测ip地址库内是否存在指定的ip
- Nth child selector
猜你喜欢
随机推荐
Technology - middle stage
How to lock the screen with one button when leaving the computer midway
How to learn mind map efficiently
Middle office architecture learning
R language plot visualization: use the plot visualization model to create a grid by using the classification contour line (contour line) of the entire data space and meshgrid, where the distance betwe
R language plot visualization: plot to visualize the two-dimensional histogram contour map, and add two variable edge histograms (2D histogram contour subplot) on the top and right of the two-dimensio
怎么替换或禁用 WordPress 前后台默认的蓝色 favicon.ico 图标
Cesium draw fence
PHP common date related functions
Deepin 运行 cherrytree报错找不到模块gtksourceview2
C language removes white space at the end of string
[ctfshow singles cup]web writeup & learn the basic usage of SED awk
Esql, this article is well written
测试必看,初次编写测试用例的要点
R language plot visualization: plot visualization horizontal boxplot
Ueeditor image cross domain upload solution
EasyWeChat实现微信真实支付操作
JPEX推出BAYC MAYC合约系列 欢迎体验
Deepin 编译VirtualBox实录以及编译报错解决
After the change of beauty brands and consumer demand, how can retailers not fall behind?








![[ctfshow singles cup]web writeup & learn the basic usage of SED awk](/img/e6/c0ce483e091ba03f81667dacc04c3b.png)
