UGUI 嵌套ScrollRect时的Drag拖动冲突问题总结

直接重写ScrollRect 部分方法即可

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using SuperScrollView;
public class VHScrollRect : ScrollRect {
    public ScrollRect parentScroll;

    public LoopListView2 loopView;

    public bool isVertical = false;

    private bool isSelf = false;

    protected override void Start() {
        base.Start();

        isVertical = base.vertical;
    }

    public override void OnBeginDrag(PointerEventData eventData) {
        Vector2 touchDeltaPosition;
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
        float delta_x = Input.GetAxis("Mouse X");
        float delta_y = Input.GetAxis("Mouse Y");
        touchDeltaPosition = new Vector2(delta_x, delta_y);
#else
         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);
                if (loopView != null) {
                    loopView.OnBeginDrag(eventData);
                }
            }
        } else {
            if (Mathf.Abs(touchDeltaPosition.x) > Mathf.Abs(touchDeltaPosition.y)) {
                isSelf = true;
                base.OnBeginDrag(eventData);
            } else {
                isSelf = false;
                parentScroll.OnBeginDrag(eventData);
                if (loopView != null) {
                    loopView.OnBeginDrag(eventData);
                }
            }
        }
    }

    public override void OnDrag(PointerEventData eventData) {
        if (isSelf) {
            base.OnDrag(eventData);
        } else {
            parentScroll.OnDrag(eventData);

            if (loopView != null) {
                loopView.OnDrag(eventData);
            }
        }
    }

    public override void OnEndDrag(PointerEventData eventData) {
        if (isSelf) {
            base.OnEndDrag(eventData);
        } else {
            if (loopView != null) {
                loopView.OnEndDrag(eventData);
            }
            parentScroll.OnEndDrag(eventData);
        }
    }
}

你可能感兴趣的:(Unity3D,工具合集)