Author Topic: Moving UIScrollView To Center On Child Sprite  (Read 20326 times)

vexir

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 47
    • View Profile
Moving UIScrollView To Center On Child Sprite
« on: December 29, 2013, 03:17:41 AM »
I have a UIScrollView with a few child sprites parented to it (quite a large hierarchy in fact.. it's a map type screen). I've seen many threads on the topic but I can't quite sort out what's from old versions of NGUI and what the different classes are that can accomplish this, but of course I'm sure it can be done.

I just need to be able to say that given this sprite (that is a child of the ScrollView, some ways down the hierarchy), center the ScrollView on that sprite's position so the player can see what's going on.

How can I accomplish this?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Moving UIScrollView To Center On Child Sprite
« Reply #1 on: December 29, 2013, 06:22:18 PM »
UICenterOnChild does this for you. In particular -- UICenterOnChild.CenterOn (Transform).

UICenterOnClick is a helper script that works with UICenterOnChild and makes it possible to center on an object you clicked, if you need an example on how to use it.

You can also move the scroll view directly by using SpringPanel.Begin. It's also used in UICenterOnClick (if the UICenterOnChild is not found), if you need an example.

vexir

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 47
    • View Profile
Re: Moving UIScrollView To Center On Child Sprite
« Reply #2 on: December 31, 2013, 01:06:53 AM »
That worked great!

One slight problem: it appears as though that has changed the min and max of the scroll view (i.e. things that were permitted to be on screen are now off in the "springy" zone and cannot be focused on).

Is that supposed to happen or am I doing something wrong?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Moving UIScrollView To Center On Child Sprite
« Reply #3 on: December 31, 2013, 07:19:13 AM »
I am not quite sure what you mean?

vexir

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 47
    • View Profile
Re: Moving UIScrollView To Center On Child Sprite
« Reply #4 on: December 31, 2013, 01:56:28 PM »


As you can see, the content is no longer fully allowed to be on screen. This is true even if I don't call CenterOn() - just having the script attached to the ScrollView object causes this to happen.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Moving UIScrollView To Center On Child Sprite
« Reply #5 on: December 31, 2013, 03:44:29 PM »
Do you have a weird hierarchy in there? UICenterOnChild works on children, so it expects to have all of your content to be a child of UICenterOnChild -- or at least hierarchical in nature, like so:

UICenterOnChild
- Container 1
-- Widget 1
-- Widget 2
- Container 2
-- Widget 1
-- Widget 2

vexir

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 47
    • View Profile
Re: Moving UIScrollView To Center On Child Sprite
« Reply #6 on: December 31, 2013, 04:09:48 PM »
Everything that needs to scroll is definitely a child of the script.

My hierarchy looks like:

ScrollView (has UIPanel, UIScrollView, and UICenterOnChild)
-Empty Object used for grouping
--UISprite
--UISprite
---UISprite
--UISlider
---Foreground Sprite
---Background Sprite

Is UICenterOnChild running at all times? I don't know how it picks which child to center on without ever getting a call to CenterOn(), but it feels like that's what is happening.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Moving UIScrollView To Center On Child Sprite
« Reply #7 on: December 31, 2013, 04:20:35 PM »
That's the problem. You only have one object under your UICenterOnChild, so it can only center on that object.

vexir

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 47
    • View Profile
Re: Moving UIScrollView To Center On Child Sprite
« Reply #8 on: December 31, 2013, 04:23:43 PM »
That's not the full hierarchy - there are 4 or 5 of those groups underneath the UICenterOnChild.

ScrollView (has UIPanel, UIScrollView, and UICenterOnChild)
-Empty Object used for grouping
--UISprite
--UISprite
---UISprite (X)
--UISlider
---Foreground Sprite
---Background Sprite
-Repeat 5x

I really want it to center the scroll view on the UISprite marked X. Calling CenterOn(UISpriteX) seems to do that properly, but having the script exist at all is resulting in this strange behavior.

vexir

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 47
    • View Profile
Re: Moving UIScrollView To Center On Child Sprite
« Reply #9 on: January 01, 2014, 08:40:12 PM »
@Aren, what more information can I give you to identify the problem?

Vexir

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Moving UIScrollView To Center On Child Sprite
« Reply #10 on: January 01, 2014, 10:23:30 PM »
Then it will center on your grouping object. I suggest you use widgets for grouping objects, not just empty game objects. This way they have a rect to work with. You can add invisible widgets using the ALT+SHIFT+W shortcut key.

vexir

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 47
    • View Profile
Re: Moving UIScrollView To Center On Child Sprite
« Reply #11 on: January 02, 2014, 02:05:05 AM »
So UICenterOnChild will just pick an immediate child of the transform it's attached to?
  • What happens when you feed in a non-immediate child to CenterOn()?
  • If I were to attach UICenterOnChild on the parent of UISprite (X), and would it be smart enough to know which scroll view it should be centering?
  • How do I stop UICenterOnChild from operating constantly? The ideal behavior is that it would center the scroll view once, and then stop ever working again. Is the right solution to attach it dynamically and then delete it once I want the scroll view to have it's full range of motion?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Moving UIScrollView To Center On Child Sprite
« Reply #12 on: January 02, 2014, 11:32:23 AM »
1. It should work fine.
2. Center on child script picks up transforms directly underneath it, but not its children. It should ideally be placed on the same object as UIGrid. You should only have one UICenterOnChild script per scroll view. If you use more than one, results may be undefined.
3. Disable the script.

skatola

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 31
    • View Profile
Re: Moving UIScrollView To Center On Child Sprite
« Reply #13 on: August 02, 2015, 06:25:30 PM »
i have exactly the same issue, so i post here : )
i read the answers but i still have problem with that : (
i have a vertical scrollview with this hierarchy:

vertical scrollview
  - vertical sprite to scroll
       - grid
            - child object
            - child object
            - child object
            - (n) child objects


at the start of the scene i instantiate a variable number of child objects in the grid, i just want to focus the scroll at the start on the last child object attached, can u help me ArenMook?
thank you, maybe it's easy but i really can't figure out to solve this, tried to attach UICenterOnChild script to the scrollview but like u said it do something only on "vertical sprite to scroll".
« Last Edit: August 02, 2015, 06:59:34 PM by skatola »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Moving UIScrollView To Center On Child Sprite
« Reply #14 on: August 05, 2015, 07:50:17 AM »
UICenterOnChild needs to go on the object directly above children. In your case -- the "grid" object.

I'm also not sure why you have a "vertical sprite to scroll" object there and why everything else is a child of it. The hierarchy should be:

Scroll view
- Grid
-- child
-- child
-- child