Author Topic: SetAnchor() runtime not behaving as expected  (Read 2131 times)

Paul

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 22
    • View Profile
SetAnchor() runtime not behaving as expected
« 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.



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?

Paul

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: SetAnchor() runtime not behaving as expected
« Reply #1 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.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: SetAnchor() runtime not behaving as expected
« Reply #2 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.

Paul

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: SetAnchor() runtime not behaving as expected
« Reply #3 on: October 21, 2015, 04:12:59 PM »
Fantastic, thanks ArenMook!! UIEnvelopContent was the missing piece of the puzzle. =)