Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: col000r on August 15, 2014, 07:42:15 AM

Title: Disable individual anchors in code
Post by: col000r 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... :)
Title: Re: Disable individual anchors in code
Post by: ArenMook 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.