Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: tayl0r on September 17, 2012, 05:30:06 AM

Title: UIAnchor on other widgets - limitations due to using local position only
Post by: tayl0r on September 17, 2012, 05:30:06 AM
I love that UIAnchor lets you anchor on other widgets now. But, one limitation that is holding me back from using it is that the child UIAnchor has to be a sibling of the widget you are anchoring to, since UIAnchor on a widget only looks at the local position of the widget. A lot of my UI windows are not setup with this type of hierarchy so it breaks.

Any plan to also take into account the global position so you could anchor on widgets anywhere in the transform hierarchy?

FWIW, I made a new class, TUIAnchorOnSprite, that is a copy of UIAnchor (not inherited), and added this functionality. I've been using it for about a month and it seems to work fine for me (it would probably break a lot of people's setups though). I tried switching back over to using the new widget anchoring functionality of UIAnchor but because of this limitation I couldn't.
http://pastebin.com/uyhAjnSC

It still does all the calculations using local position, but then when it sets the position of the widget it first converts the local position to global:
  1. Vector3 worldPosition = m_anchorWidget.transform.parent.TransformPoint(v);
And assigns that to the .position property and not .localPosition.

I also added a scale feature so you can tie the scale of the child widget to the scale of the parent. This isn't useful for making "pixel perfect" UIs but it is very useful for making your UIs look the same in a bunch of different resolutions so it works the best with 9-sliced sprites.
Title: Re: UIAnchor on other widgets - limitations due to using local position only
Post by: ArenMook on September 17, 2012, 05:32:27 AM
I'll mention it to Phil, thanks!
Title: Re: UIAnchor on other widgets - limitations due to using local position only
Post by: PhilipC on September 17, 2012, 11:39:35 AM
UIAnchor change is done :). Thanks for the suggestion/ fix.

Looking to see if i can do something similar for UIStretch.
Title: Re: UIAnchor on other widgets - limitations due to using local position only
Post by: tayl0r on September 17, 2012, 01:55:58 PM
Awesome, thanks.