为了记录下方便之后看,纯转载代码:转载地址:https://blog.csdn.net/zahll1993/article/details/51446856
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class VHScrollRect :ScrollRect
{
public ScrollRect parentScroll; //创建列表的时候赋值(这个是对我来说是上下滑动的ScrollRect)
public bool isVertical = false;
private bool isSelf = false;
public override void OnBeginDrag(PointerEventData eventData)
{
Vector2 touchDeltaPosition;
#if UNITY_EDITOR
float delta_x = Input.GetAxis("Mouse X");
float delta_y = Input.GetAxis("Mouse Y");
touchDeltaPosition = new Vector2(delta_x, delta_y);
#endif
#if UNITY_ANDROID && !UNITY_EDITOR
touchDeltaPosition = Input.GetTouch(0).deltaPosition;
#endif
if (isVertical)
{
if (Mathf.Abs(touchDeltaPosition.x) < Mathf.Abs(touchDeltaPosition.y))
{
isSelf = true;
base.OnBeginDrag(eventData);
}
else
{
isSelf = false;
parentScroll.OnBeginDrag(eventData);
}
}
else
{
if (Mathf.Abs(touchDeltaPosition.x) > Mathf.Abs(touchDeltaPosition.y))
{
isSelf = true;
base.OnBeginDrag(eventData);
}
else
{
isSelf = false;
parentScroll.OnBeginDrag(eventData);
}
}
}
public override void OnDrag(PointerEventData eventData)
{
if (isSelf)
{
base.OnDrag(eventData);
}
else
{
parentScroll.OnDrag(eventData);
}
}
public override void OnEndDrag(PointerEventData eventData)
{
if (isSelf)
{
base.OnEndDrag(eventData);
}
else
{
parentScroll.OnEndDrag(eventData);
}
}
}