zoukankan      html  css  js  c++  java
  • unity3dScrollRect与OnDrag事件的冲突解决方法

    借鉴  https://www.jianshu.com/p/8bfe94822886

    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 Start()
        {
            mNewItemRoot = GameObject.Find("InfoBoard").transform;
            mScrollRect = GameObject.Find("InfoBoard").GetComponent<ScrollRect>();
        }
        void Update()
        {
            if (mScrollRect == null)
            {
    
            }
    
            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);
        }
    
    
    }
    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)
        {
    
        }
    }

    用法

    将ItemOnDrag挂在每个可拖拽的Item上面即可
      其中的mNewItem就是Item本身的预设体,mNewItemRoot是ScrollRect组件容器的transform,mScrollRect是ScrollRect组件容器的ScrollRect,但是mNewItemRoot和mScrollRect需要手动关联

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

  • 相关阅读:
    OCS 2007 R2单机测试虚拟环境的搭建(windows server 2008 R2 x64)
    Linq的概念解析
    WCF Data Service与net.tcp承载分析
    codeplex上20个有意思的WPF程序
    各种SmartPhone上的跨平台开源框架的总结
    新开发Apple Store上软件的实施步骤
    VSTO应用程序中加入键盘钩子
    绝非偶然 苹果iPhone领先5年背后的迷思 【推荐】
    Windows上安装Mac OS虚拟机
    Visual Studio 远程调试的步骤
  • 原文地址:https://www.cnblogs.com/lingLuoChengMi/p/10009414.html
Copyright © 2011-2022 走看看