滚动缩放关卡选择列表

请添加图片描述

#region 列表滑动方式
        //开始拖拽content
        public void BeginDrag_ScrollRect(PointerEventData eventData)
        {
            beginSmooth = false;
            pointBeginDragX = Input.mousePosition.x;
            Vector2 mouseDown = eventData.position;    //记录鼠标按下时的屏幕坐标
            Vector2 mouseUguiPos = new Vector2();
            bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(m_ContentRectTrans.parent.GetComponent<RectTransform>(), mouseDown, eventData.enterEventCamera, out mouseUguiPos);
            if (isRect)   //如果在
            {
                //计算图片中心和鼠标点的差值
                offsetMouse = m_ContentRectTrans.anchoredPosition - mouseUguiPos;
                isToNext = false;
            }
        }
        //拖拽中content
        public void OnDrag_ScrollRect(PointerEventData eventData)
        {

            float x = m_ContentRectTrans.anchoredPosition.x;
            float y = m_ContentRectTrans.anchoredPosition.y;

            int index = curIndex;
            float distance = (m_CellObjectWidth + m_Spacing) / 2f ;
            if (Input.mousePosition.x - pointBeginDragX > distance)
            {
                if (index > 0)
                {
                    pointBeginDragX = Input.mousePosition.x;
                    isToNext = true;
                    JumpCenterIndex(index - 1);
                }
            }
            else if ((Input.mousePosition.x - pointBeginDragX) < distance && (Input.mousePosition.x - pointBeginDragX) > 0)
            {
                SelectContentPos_InDrag(eventData, y);
            }
            else if ((Input.mousePosition.x - pointBeginDragX) > -distance && (Input.mousePosition.x - pointBeginDragX) < 0)
            {
                SelectContentPos_InDrag(eventData, y);
            }
            else if (Input.mousePosition.x - pointBeginDragX < -distance)
            {
                if (index < m_CellInfos.Length-1)
                {
                    pointBeginDragX = Input.mousePosition.x;
                    isToNext = true;
                    JumpCenterIndex(index + 1);
                }
            }
        }
        public void SelectContentPos_InDrag(PointerEventData eventData, float y)
        {
            if (!isToNext)
            {
                Vector2 mouseDrag = eventData.position;   //当鼠标拖动时的屏幕坐标
                Vector2 uguiPos = new Vector2();   //用来接收转换后的拖动坐标
                                                   //和上面类似
                bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(m_ContentRectTrans.parent.GetComponent<RectTransform>(), mouseDrag, eventData.enterEventCamera, out uguiPos);

                if (isRect)
                {
                    //设置图片的ugui坐标与鼠标的ugui坐标保持不变
                    m_ContentRectTrans.anchoredPosition = new Vector2((offsetMouse + uguiPos).x, y);
                    if ((offsetMouse + uguiPos).x > rangs[0])
                    {
                        m_ContentRectTrans.anchoredPosition = new Vector2(rangs[0], y);
                    }
                    if ((offsetMouse + uguiPos).x < rangs.Last())
                    {
                        m_ContentRectTrans.anchoredPosition = new Vector2(rangs.Last(), y);
                    }
                }
            }
            else
            {
                Vector2 mouseDown = eventData.position;    //记录鼠标按下时的屏幕坐标
                Vector2 mouseUguiPos = new Vector2();
                bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(m_ContentRectTrans.parent.GetComponent<RectTransform>(), mouseDown, eventData.enterEventCamera, out mouseUguiPos);
                if (isRect)   //如果在
                {
                    //计算图片中心和鼠标点的差值
                    offsetMouse = m_ContentRectTrans.anchoredPosition - mouseUguiPos;
                }
            }
        }
        //结束拖拽content
        public void EndDrag_ScrollRect(PointerEventData eventData)
        {
            offsetMouse = Vector2.zero;
            StartSmoothDamp();
        }

        public void SetRangsContent()
        {
            rangs = new List<float>();
            for (int i = 0; i < m_CellInfos.Length; i++)
            {
                float dragContent = (m_PlaneWidth - m_CellObjectWidth) / 2f - (m_CellObjectWidth + m_Spacing) * i;
                rangs.Add(dragContent);
            }
        }

        public void StartSmoothDamp()
        {
            if (smooth != null)
            {
                CoroutingTool.Instance.StopCoroutine(smooth);
            }
            smooth = SmoothDamp();
            beginSmooth = true;
            CoroutingTool.Instance.StartCoroutine(SmoothDamp());
        }
        public IEnumerator SmoothDamp()
        {
            float mMoveSpeed = 0f;
            float SMOOTH_TIME = 0.2f;
            //float SMOOTH_TIME = 0.02f;
            while (beginSmooth)
            {
                float x = m_ContentRectTrans.anchoredPosition.x;
                float y = m_ContentRectTrans.anchoredPosition.y;
                int index = curIndex;
                if (Mathf.Abs((int)x - rangs[index]) < 2f)
                {
                    x = rangs[index];
                    beginSmooth = false;
                    isToNext = false;
                    yield break;
                }
                m_ContentRectTrans.anchoredPosition =
                    new Vector2(Mathf.SmoothDamp(x, rangs[index], ref mMoveSpeed, SMOOTH_TIME), y);

                yield return 0;
            }
        }
        #endregion
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值