https://www.cnblogs.com/suoluo/p/5643885.html
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine;
/// <summary>
/// 解決嵌套使用ScrollRect時的Drag沖突問題艺玲。請將該腳本放置到內(nèi)層ScrollRect上(外層的ScrollRect的Drag事件會被內(nèi)層的攔截)
/// </summary>
public class NestedScrollRect : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
????/// <summary>
????/// 外層被攔截需要正常拖動的ScrollRect括蝠,可不指定,默認(rèn)在父對象中找
????/// </summary>
????public ScrollRect anotherScrollRect;
????/// <summary>
????/// 當(dāng)前的ScrollRect(本腳本所放置的物體上)的拖動方向默認(rèn)為上下拖動板驳,否則為左右拖動型
????/// </summary>
????public bool thisIsUpAndDown = true;
????private ScrollRect thisScrollRect;
????void Awake ()
????{
????????thisScrollRect = GetComponent<ScrollRect> ();
????????if (anotherScrollRect == null)
????????????anotherScrollRect = GetComponentsInParent<ScrollRect> ()[1];
????}
????public void OnBeginDrag (PointerEventData eventData)
????{
????????anotherScrollRect.OnBeginDrag (eventData);
????}
????public void OnDrag (PointerEventData eventData)
????{
????????anotherScrollRect.OnDrag (eventData);
????????float angle = Vector2.Angle (eventData.delta, Vector2.up);
????????//判斷拖動方向又跛,防止水平與垂直方向同時響應(yīng)導(dǎo)致的拖動時整個界面都會動
????????if (angle > 45f && angle < 135f)
????????{
????????????thisScrollRect.enabled = !thisIsUpAndDown;
????????????anotherScrollRect.enabled = thisIsUpAndDown;
????????}
????????else
????????{
????????????anotherScrollRect.enabled = !thisIsUpAndDown;
????????????thisScrollRect.enabled = thisIsUpAndDown;
????????}
????}
????public void OnEndDrag (PointerEventData eventData)
????{
????????anotherScrollRect.OnEndDrag (eventData);
????????anotherScrollRect.enabled = true;
????????thisScrollRect.enabled = true;
????}
}