Author Topic: Disable individual anchors in code  (Read 4245 times)

col000r

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 3
  • Posts: 43
  • Mighty Emperor of Planet "Home Office"
    • View Profile
    • BLACKISH
Disable individual anchors in code
« on: August 15, 2014, 07:42:15 AM »
I love the anchoring system, but it is also slowly driving me insane... :)

Please tell me: What is the correct way to deal with Anchors in code?

If I do something like this:

  1. widget.leftAnchor.target = widget.rightAnchor.target = contentArea.cachedTransform;
  2. widget.leftAnchor.relative = 0f;
  3. widget.rightAnchor.relative = 1f;
  4. widget.leftAnchor.absolute = 48;
  5. widget.rightAnchor.absolute = -48;
  6. widget.topAnchor.target = widget.bottomAnchor.target = null;

Do I need to do anything else after that? Call ResetAnchors() or anything? It seems every time I work with anchors in code things start getting weird...

PS: The evil, tricky part about the above code is that works correctly ONLY IF that GameObject is NOT active in the inspector. Took me a while to figure that one out... :)
Games: BLACKISH | Blog | Assets

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Disable individual anchors in code
« Reply #1 on: August 15, 2014, 08:11:36 AM »
You are missing widget.ResetAndUpdateAnchors();

Without it NGUI won't actually know that you modified those values. This is why it only works for you while the object is disabled. Add that line, and it will work always.