Author Topic: how to set scrollview child's position?  (Read 4529 times)

Arcanor

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 46
    • View Profile
how to set scrollview child's position?
« on: February 12, 2014, 02:04:59 PM »
I have a UIScrollView on a panel, set to Vertical movement.  The scroll view has one child, which is a (draggable) UIGrid containing a dynamic list of buttons.  The UIGrid and all its children (the buttons) have UIDragScrollView components on them.  Vertical dragging/scrolling is working fine.

When I make dynamic changes to the button list (i.e. adding new buttons at runtime via code), I call Reposition() on the grid.  However, sometimes the particular button that I want to show as "selected" (it has an extra glow around it) is outside the bounds of the clipping panel.  How can I ensure that a particular button is showing after Reposition()?

Again, here's my object setup:
-panel/scrollview
-- grid
--- button
--- button
--- button
--- button
--- (etc.)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: how to set scrollview child's position?
« Reply #1 on: February 13, 2014, 12:44:36 AM »
Make sure to use NGUITools.AddChild(parent, prefab), not Object.Instantiate.

If something shows up outside the bounds of the scroll view, it suggests that that object is not a part of that scroll view. Otherwise it would have been clipped.

Arcanor

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 46
    • View Profile
Re: how to set scrollview child's position?
« Reply #2 on: February 13, 2014, 02:34:43 AM »
Thanks for the tip about NGUITools.AddChild().  I don't think that's the problem, but it looks like a helpful function anyway.

In any case, in my post I guess I didn't make it clear that the selected button IS being clipped.  The problem is how do I keep the grid in position so the selected button is visible?


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: how to set scrollview child's position?
« Reply #3 on: February 13, 2014, 03:14:16 AM »
UIScrollView.ResetPosition() forcefully resets the position of the content. If you are trying to ensure that something is visible, consider simply springing the panel to it (SpringPanel.Begin -- look at how it's used in UICenterOnClick).
  1.         void OnClick ()
  2.         {
  3.                 UIPanel panel = NGUITools.FindInParents<UIPanel>(gameObject);
  4.                 UIScrollView sv = panel.GetComponent<UIScrollView>();
  5.  
  6.                 Vector3 offset = -panel.cachedTransform.InverseTransformPoint(transform.position);
  7.                 if (!sv.canMoveHorizontally) offset.x = panel.cachedTransform.localPosition.x;
  8.                 if (!sv.canMoveVertically) offset.y = panel.cachedTransform.localPosition.y;
  9.                 SpringPanel.Begin(panel.cachedGameObject, offset, 6f);
  10.         }