Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Paul on October 16, 2015, 07:16:41 PM

Title: SetAnchor() runtime not behaving as expected
Post by: Paul on October 16, 2015, 07:16:41 PM
I'm trying to anchor a sprite to the left side of the longest of two dynamic text strings.

(http://i.imgur.com/9HWv91U.png)

I do this by calling SetAnchor(longestLabel) in code, however the sprite's position shifts at runtime when anchored to the top label. I believe this is because the Left, Right, Bottom & Top offsets are not being updated at runtime when SetAnchor() is called.

However the L, R, T & B offsets DO update automatically in the editor when dragging a new Target onto the anchor property. How do I replicate that behavior at runtime?

Or is there an easier way to accomplish what I'm trying to achieve?
Title: Re: SetAnchor() runtime not behaving as expected
Post by: Paul on October 16, 2015, 09:20:27 PM
In the interest of time, I decided to use the max myLabel.width property to position the sprite, but I would still like to know if this is possible with anchors.
Title: Re: SetAnchor() runtime not behaving as expected
Post by: ArenMook on October 18, 2015, 06:46:55 PM
Yes, it's possible by doing this:

Widget (use it for positioning the content)
- Empty game object
-- Label 1
-- Label 2
- Widget A (another invisible widget) with UIEnvelopContent on it targeting the Empty game object
- Sprite anchored to the Widget's left side

You can also just make the Widget A's pivot be top-left instead of center, then the sprite can simply be its child positioned where you need it to be. You can skip anchoring it then.
Title: Re: SetAnchor() runtime not behaving as expected
Post by: Paul on October 21, 2015, 04:12:59 PM
Fantastic, thanks ArenMook!! UIEnvelopContent was the missing piece of the puzzle. =)