首页 > 编程语言 > 详细

unity组件Scroll Rect与其拖拽物体冲突解决办法

时间:2021-06-29 00:15:52      阅读:25      评论:0      收藏:0      [点我收藏+]

当我们想拖拽ScrollView上面的物体时 向上拖拽会跟ScrollView的向上滑动冲突。
直接看代码

 我们先创建一个父类 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using System;

public class DragBase : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    public Action<PointerEventData> beginAction;

    public Action<PointerEventData> dragingAction;

    public Action<PointerEventData> endAction;

    public void OnBeginDrag(PointerEventData eventData)
    {
        if (beginAction != null)
        {
            beginAction(eventData);
        }
    }

    public void OnDrag(PointerEventData eventData)
    {
        if (dragingAction != null)
        {
            dragingAction(eventData);
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        if (endAction != null)
        {
            endAction(eventData);
        }
    }

}

接下来看一下子类 在大于70度的时候让ScrollView移动小于的时候则拖拽物体移动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class DragManager : DragBase
{
    enum DragType
    {
        DragSelf,
        DragScroll,
        None
    }
    DragType dragType = DragType.None;
    public ScrollRect scrollRect;
    Vector3 totalDelta = Vector3.zero;
    void Start()
    {
        beginAction = BeginDrag;
        dragingAction = Draging;
        endAction = EndDrag;
    }

    private void SetDragType(DragType dragType)
    {
        this.dragType = dragType;
    }

    private bool CheckDragState(DragType state)
    {
        return dragType == state;
    }

    private void BeginDrag(PointerEventData eventData)
    {
        Vector3 delta = eventData.delta;
        Debug.Log(delta);
        scrollRect.OnBeginDrag(eventData);
        totalDelta += new Vector3(Mathf.Abs(delta.x), Mathf.Abs(delta.y), 0);
    }



    private void Draging(PointerEventData eventData)
    {
        Vector3 delta = eventData.delta;
        Debug.Log(delta);
        if (CheckDragState(DragType.None))
        {
            if (Mathf.Abs(delta.y) != Mathf.Abs(delta.x))
            {
                if (Mathf.Atan(Mathf.Abs(delta.y) / Mathf.Abs(delta.x)) > Mathf.Deg2Rad * 70)
                {
                    SetDragType(DragType.DragScroll);
                }
                else
                {
                    SetDragType(DragType.DragSelf);
                }
            }
        }
        switch (dragType)
        {
            case DragType.DragScroll:
                scrollRect.OnDrag(eventData);
                break;
            case DragType.DragSelf:
                transform.position += (Vector3)eventData.delta;
                break;
        }
    }

    private void EndDrag(PointerEventData eventData)
    {
        scrollRect.OnEndDrag(eventData);
        SetDragType(DragType.None);
        totalDelta = Vector3.zero;
    }
}

 

unity组件Scroll Rect与其拖拽物体冲突解决办法

原文:https://www.cnblogs.com/zjp959/p/14946210.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!