/// <summary> /// Drag the object along the plane. /// </summary> public void Drag() { if (enabled && NGUITools.GetActive(gameObject) && mShouldMove) { if (mDragID == -10) { mDragID = UICamera.currentTouchID; } UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta; // Prevents the drag "jump". Contributed by 'mixd' from the Tasharen forums. if (smoothDragStart && !mDragStarted) { mDragStarted = true; mDragStartOffset = UICamera.currentTouch.totalDelta; } Ray ray = smoothDragStart ? UICamera.currentCamera.ScreenPointToRay(UICamera.currentTouch.pos - mDragStartOffset) : UICamera.currentCamera.ScreenPointToRay(UICamera.currentTouch.pos); float dist = 0f; if (mPlane.Raycast(ray, out dist)) { Vector3 currentPos = ray.GetPoint(dist); Vector3 offset = currentPos - mLastPos; mLastPos = currentPos; Vector3 curScale = scale; if (curScale.x > 0 && curScale.y > 0 && !m_bBeingJudge && (offset.x != 0 || offset.y != 0)) { m_bHorizoning = Mathf.Abs(offset.x) > Mathf.Abs(offset.y); m_bBeingJudge = true; } if (m_bBeingJudge) { if (m_bHorizoning) { curScale.x = scale.x; curScale.y = 0; vecOnDragonMode = OnDragonMode.LeftOrRight; } else { curScale.y = scale.y; curScale.x = 0; vecOnDragonMode = OnDragonMode.TopOrBottom; } } // if (m_bHorizoning) { if (offset.x < 0) { curDragonMode = DragonMode.Left; } else { curDragonMode = DragonMode.Right; } } else { curDragonMode = DragonMode.None; } if (offset.x != 0f || offset.y != 0f) { offset = mTrans.InverseTransformDirection(offset); offset.Scale(curScale); offset = mTrans.TransformDirection(offset); } // Adjust the momentum mMomentum = Vector3.Lerp(mMomentum, mMomentum + offset * (0.01f * momentumAmount), 0.67f); // Move the panel if (!iOSDragEmulation) { MoveAbsolute(offset); } else { Vector3 constraint = mPanel.CalculateConstrainOffset(bounds.min, bounds.max); if (constraint.magnitude > 0.001f) { MoveAbsolute(offset * 0.5f); mMomentum *= 0.5f; } else { MoveAbsolute(offset); } } // We want to constrain the UI to be within bounds if (restrictWithinPanel && mPanel.clipping != UIDrawCall.Clipping.None && dragEffect != DragEffect.MomentumAndSpring) { RestrictWithinBounds(true); } } } }
/// <summary> /// Create a plane on which we will be performing the dragging. /// </summary> public void Press(bool pressed) { //Debug.Log("Press: " + pressed); if (!pressed) { m_bBeingJudge = false; } m_bBeingJudge = false; m_bHorizoning = false; if (smoothDragStart && pressed) { mDragStarted = false; mDragStartOffset = Vector2.zero; } if (enabled && NGUITools.GetActive(gameObject)) { if (!pressed && mDragID == UICamera.currentTouchID) { mDragID = -10; } mCalculatedBounds = false; mShouldMove = shouldMove; if (!mShouldMove) { return; } mPressed = pressed; if (pressed) { // Remove all momentum on press mMomentum = Vector3.zero; mScroll = 0f; // Disable the spring movement DisableSpring(); // Remember the hit position mLastPos = UICamera.lastHit.point; // Create the plane to drag along mPlane = new Plane(mTrans.rotation * Vector3.back, mLastPos); startDragonPos = UICamera.currentTouch.pos; } else { Vector2 pos = UICamera.currentTouch.pos - startDragonPos; if (vecOnDragonMode == OnDragonMode.LeftOrRight) { if (pos.x >= 100) { curDragonMode = DragonMode.Right; } else if (pos.x <= -100) { curDragonMode = DragonMode.Left; } else { curDragonMode = DragonMode.None; } } else { curDragonMode = DragonMode.None; } if (onDragFinished != null) { onDragFinished(curDragonMode); } if (restrictWithinPanel && mPanel.clipping != UIDrawCall.Clipping.None && dragEffect == DragEffect.MomentumAndSpring) { RestrictWithinBounds(false); } } } }