Author Topic: UICenterOnChild does not check UIDraggablePanel.restrictWithinPanel  (Read 2313 times)

Christian Tellefsen

  • Guest

I have an UIDraggablePanel with restrictWithinPanel set to true. The clipping size is set to exactly fill the screen. This works fine, I can drag the panel around, and it never goes out of bounds.
I then added UICenterOnChild to the same GameObject.

This can make the UIDraggablePanel no longer restricted to be within its bounds. If UICenterOnChild centers on a child that is too close to the edge of the panel, it will cause the panel to move out of bounds to center on the child. I would like to constrain the position SpringPanel moves to so that restrictWithinPanel is still honored.

Does anyone know how I can compute an alternate position to move to that does not cause the panel to move out of bounds?

Christian Tellefsen.


  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: UICenterOnChild does not check UIDraggablePanel.restrictWithinPanel
« Reply #1 on: April 29, 2013, 05:46:11 PM »
I suggest you look inside the UICenterOnChild and make your own version that accounts for the width of the cliprange (or height).

Christian Tellefsen

  • Guest
Thanks, I figured it out. Here's my code:

  1. UIPanel panel = mDrag.panel;
  2. Vector4 clip = panel.clipRange;
  3. Vector3 posToCenter = mCenteredObject.transform.localPosition;
  4. Bounds panelBounds = NGUIMath.CalculateRelativeWidgetBounds(panel.transform);
  5. Bounds targetBounds = new Bounds(posToCenter, new Vector3(clip.z, clip.w));
  6. Vector3 offset = NGUIMath.ConstrainRect(targetBounds.min, targetBounds.max, panelBounds.min, panelBounds.max).ToVector3();
  7. Vector3 constrainedTargetPos = posToCenter + offset;
  8. SpringPanel.Begin(mDrag.gameObject, -constrainedTargetPos, 8.0f).onFinished = onFinished;