解决两层ScrollView嵌套,底层事件无法触发的问题
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Engine
{
public class ScrollRectExtend : ScrollRect
{
[Range(0,1000)]
public float stopSlideSpeed = 2.0f; //滑动停止的速度,达到时就执行滑动结束回调
private ScrollRect parentScrollRect; //父ScrollRect
private bool isSelf = true;
private Action stopSlideCallback = null;//停止滑动时的回调
private bool dragEndFlag = false; //拖拽结束标记
public void SetParentScrollRect(ScrollRect parentScroll)
{
parentScrollRect = parentScroll;
}
public void SetStopSlideCallback(Action callback)
{
stopSlideCallback = callback;
}
public void SetStopSlideSpeed(float speed)
{
if (speed < 0.0f)
speed = 0.0f;
stopSlideSpeed = speed;
}
public override void OnBeginDrag(PointerEventData eventData)
{
dragEndFlag = false;
Vector2 touchDeltaPosition;
#if (UNITY_ANDROID || UNITY_IOS)
touchDeltaPosition = Input.GetTouch(0).deltaPosition;
#else
float delta_x = Input.GetAxis("Mouse X");
float delta_y = Input.GetAxis("Mouse Y");
touchDeltaPosition = new Vector2(delta_x, delta_y);
#endif
if ((horizontal && vertical) || (parentScrollRect == null))
{
isSelf = true;
base.OnBeginDrag(eventData);
return;
}
if (!horizontal && !vertical)
{
isSelf = false;
parentScrollRect.OnBeginDrag(eventData);
return;
}
if (vertical && !horizontal)
{
if (Mathf.Abs(touchDeltaPosition.x) < Mathf.Abs(touchDeltaPosition.y))
{
isSelf = true;
base.OnBeginDrag(eventData);
}
else
{
isSelf = false;
parentScrollRect.OnBeginDrag(eventData);
}
return;
}
if (!vertical && horizontal)
{
if (Mathf.Abs(touchDeltaPosition.x) > Mathf.Abs(touchDeltaPosition.y))
{
isSelf = true;
base.OnBeginDrag(eventData);
}
else
{
isSelf = false;
parentScrollRect.OnBeginDrag(eventData);
}
return;
}
}
public override void OnEndDrag(PointerEventData eventData)
{
if (isSelf)
{
base.OnEndDrag(eventData);
}
else
{
parentScrollRect.OnEndDrag(eventData);
}
dragEndFlag = true;
}
public override void OnDrag(PointerEventData eventData)
{
if (isSelf)
{
base.OnDrag(eventData);
}
else
{
parentScrollRect.OnDrag(eventData);
}
dragEndFlag = false;
}
private void Update()
{
base.LateUpdate();
}
protected override void LateUpdate()
{
base.LateUpdate();
bool isStopSlide = CheckIsStopSlide();
if (isStopSlide && dragEndFlag)
{
dragEndFlag = false;
StopMovement();
if (stopSlideCallback != null)
{
stopSlideCallback();
}
}
}
private bool CheckIsStopSlide()
{
var diff = 0.0;
if (horizontal)
{
diff = Math.Abs(velocity.x);
}
else if (vertical)
{
diff = Math.Abs(velocity.y);
}
if (diff <= stopSlideSpeed)
return true;
return false;
}
}
}