unity3dScrollRect与OnDrag事件的冲突解决方法

项目中需要有一个scrollRect组件可以左右拖拽角色头像进行滚动,同时在长按角色头像的时候能移动角色头像与滚动列表下面的角色组进行交换,这就遇到了scrollrect与onDrag的组件冲突问题,这里做了一个解决办法


测试效果.gif
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/// <summary>
/// 脚本挂载到每个可拖拽的Item上面即可
/// </summary>
public class ItemOnDrag : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    // 实例化的新item
    public GameObject mNewItem;
    public Transform mNewItemRoot;
    public ScrollRect mScrollRect;
    // 是否按下
    public bool mIsDown = false;
    // 按下与松开鼠标之间的距离
    public float mBorderDis = 0.5f;
    // 总的按下时间
    public float mTotalTime = 1;
    private float mCurTime = 0;
    // 当前鼠标位置
    public Vector3 mCurPos;
    // 上一次鼠标位置
    public Vector3 mPrevPos;
    private NewItemOnDrag mNewDrag;

    void Update()
    {
        if (mIsDown)
        {
            mCurTime += Time.deltaTime * 1;
            if (mCurTime >= mTotalTime)
            {
                if (Vector3.Distance(mPrevPos, mCurPos) > mBorderDis)
                {
                    mCurTime = 0f;
                    return;
                }
                mCurTime = 0f;
                mIsDown = false;
                GameObject item = Instantiate(mNewItem);
                item.transform.SetParent(mNewItemRoot);
                item.transform.localScale = Vector3.one;
                mNewDrag = item.GetComponent<NewItemOnDrag>();
                if (mNewDrag == null)
                    mNewDrag = item.AddComponent<NewItemOnDrag>();
                item.transform.position = Input.mousePosition;
            }
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        mNewDrag = null;
        mPrevPos = Input.mousePosition;
        mCurPos = Input.mousePosition;
        mIsDown = true;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        mPrevPos = Vector3.zero;
        mCurPos = Vector3.zero;
        mIsDown = false;
        mNewDrag = null;
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        mScrollRect.OnBeginDrag(eventData);
    }

    public void OnDrag(PointerEventData eventData)
    {
        mCurPos = eventData.position;
        if (mNewDrag == null)
            mScrollRect.OnDrag(eventData);
        else
            mNewDrag.transform.position = Input.mousePosition;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        mNewDrag = null;
        mScrollRect.OnEndDrag(eventData);
    }


}

这个脚本会自动挂载到新实例化出来的item上的,不要做处理,后期可加入检测

using UnityEngine;
using UnityEngine.EventSystems;

public class NewItemOnDrag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    public void OnBeginDrag(PointerEventData eventData)
    {

    }

    public void OnDrag(PointerEventData eventData)
    {
        transform.position = Input.mousePosition;
    }

    public void OnEndDrag(PointerEventData eventData)
    {

    }
}

界面布局.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,020评论 25 709
  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,936评论 22 665
  • 相信无论是商家还是玩家给翡翠估价都是一个令人困惑的问题。甚至是玩翡翠很多年的老手只要有三个月不去关注翡翠市场的发展...
    毛叶林阅读 525评论 0 1
  • 和另一种由二氧睾酮衍生的强有力的合成代谢类固醇康复龙Anadrol 不同,氧雄龙的副作用相对比较温和。对于健康的成...
    巍w阅读 964评论 0 0
  • 介绍 许多应用需要处理及时收到的数据,Spark Streaming是Spark为这些应用而设计的模型。它允许用户...
    raincoffee阅读 747评论 0 0