Author Topic: UIAnchor on other widgets - limitations due to using local position only  (Read 2000 times)

tayl0r

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
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.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIAnchor on other widgets - limitations due to using local position only
« Reply #1 on: September 17, 2012, 05:32:27 AM »
I'll mention it to Phil, thanks!

PhilipC

  • Guest
Re: UIAnchor on other widgets - limitations due to using local position only
« Reply #2 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.

tayl0r

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: UIAnchor on other widgets - limitations due to using local position only
« Reply #3 on: September 17, 2012, 01:55:58 PM »
Awesome, thanks.