1
NGUI 3 Support / UIAnchor on other widgets - limitations due to using local position only
« 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:
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.
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:
- Vector3 worldPosition = m_anchorWidget.transform.parent.TransformPoint(v);
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.