Author Topic: Setting Anchor TARGET Side At Runtime  (Read 2827 times)

Thickney

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 4
    • View Profile
Setting Anchor TARGET Side At Runtime
« on: July 02, 2014, 12:30:13 PM »
I've done a bit of searching and haven't found this addressed anywhere. What I have is widgets in a vertical list and I want to anchor the top of widget #2 to the bottom of widget #1.

Currently I'm setting the anchors in code using SetAnchors(), but the target side for the widget I'm anchoring to is always set to "Center". I want to set that to "Bottom", but it doesn't look like there's any way for me to do that at runtime?
« Last Edit: July 02, 2014, 06:33:55 PM by Thickney »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Setting Anchor TARGET Side At Runtime
« Reply #1 on: July 03, 2014, 04:29:44 AM »
Only widgets, panels, and cameras have sides like top, bottom, left and right. Regular game objects don't -- and you seem to be specifying just an object with no rectangle on it.

Thickney

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Setting Anchor TARGET Side At Runtime
« Reply #2 on: July 06, 2014, 07:17:03 PM »
Hi, thanks for the reply. You were right, I was passing in the root object.

However, now that I'm using the correct UISprite object I still can't seem to set the anchor of the target properly. It now anchors to the top side of the target widget, not the center. While I can now change this easily in the Unity editor (it's not greyed out like before), I'm still not sure how it should be done in code.

Basically what I need is something like mySprite.SetAnchors(leftTarget, TargetSide.Left, offsetValue, ...) or something else I can call to set the "Target Side" property after calling SetAnchors.

Edit: Okay so if I set a dummy object in the list and have the first item in the list preloaded and anchored to the dummy properly in the editor, then the prefabs for the other list items seem to get the correct side. I'd still feel better about setting it explicitly though.
« Last Edit: July 06, 2014, 07:26:13 PM by Thickney »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Setting Anchor TARGET Side At Runtime
« Reply #3 on: July 07, 2014, 03:52:13 AM »
Have a look at the UIRect.AnchorPoint.Set function.

widget.leftAnchor.Set(target, relative, absolute).

'relative' is a value, generally ranging from 0 to 1. For horizontal, 0 means "left", 0.5 means "center" and 1 means "right". For vertical, that's bottom, center and top, respectively. Check UIRect's documentation page for more info.