NGUI Scrollview 回弹控制

NGUI的回弹控制需要玩家自己修改源码或者自己实现,以下代码是修改的源码   

红色部分字添加和修改的,目前是纵向的,如果修改横向,那么需要调整的是X轴坐标



 public bool NeedbackPosition = true;

    public bool RestrictWithinBounds(bool instant) { return RestrictWithinBounds(instant, true, true); }

    ///


    /// Restrict the scroll view's contents to be within the scroll view's bounds.
    ///


    public bool RestrictWithinBounds(bool instant, bool horizontal, bool vertical)
    {
        if (mPanel == null) return false;

        Bounds b = bounds;
        Vector3 constraint = mPanel.CalculateConstrainOffset(b.min, b.max);

        if (!horizontal) constraint.x = 0f;
        if (!vertical) constraint.y = 0f;


        #region add by mym 2017.1.7
        if (NeedbackPosition)
        {
            Vector2 pv = NGUIMath.GetPivotOffset(contentPivot);
            float x = pv.x;
            float y = 1 - pv.y;
            Vector4 clip = mPanel.finalClipRegion;
            float hx = clip.z * 0.5f;
            float hy = clip.w * 0.5f;
            float left = b.min.x + hx;
            float right = b.max.x - hx;
            float bottom = b.min.y + hy;
            float top = b.max.y - hy;

            if (mPanel.clipping == UIDrawCall.Clipping.SoftClip)
            {
                left -= mPanel.clipSoftness.x;
                right += mPanel.clipSoftness.x;
                bottom -= mPanel.clipSoftness.y;
                top += mPanel.clipSoftness.y;
            }

            // Calculate the offset based on the scroll value
            float ox = Mathf.Lerp(left, right, x);
            float oy = Mathf.Lerp(top, bottom, y);



            if (canMoveVertically && !shouldMoveVertically)
            {

                constraint = new Vector3(pv.x, -oy - mTrans.localPosition.y, 0);
            }
            else if (canMoveHorizontally && !shouldMoveHorizontally)
            {
                /  no  do
            }
        }
        #endregion



        if (constraint.sqrMagnitude > 0.1f)
        {
            if (!instant && dragEffect == DragEffect.MomentumAndSpring)
            {
                // Spring back into place
                Vector3 pos = mTrans.localPosition + constraint;
                pos.x = Mathf.Round(pos.x);
                pos.y = Mathf.Round(pos.y);
                SpringPanel.Begin(mPanel.gameObject,  pos, 8f);  
            }
            else
            {
                // Jump back into place
                MoveRelative(constraint);

                // Clear the momentum in the constrained direction
                if (Mathf.Abs(constraint.x) > 0.01f) mMomentum.x = 0;
                if (Mathf.Abs(constraint.y) > 0.01f) mMomentum.y = 0;
                if (Mathf.Abs(constraint.z) > 0.01f) mMomentum.z = 0;
                mScroll = 0f;
            }
            return true;
        }
        return false;
    }

你可能感兴趣的:(Unity)