1
NGUI 3 Support / Re: understanding anchors at runtime
« on: November 04, 2014, 10:25:53 PM »
I did get this working... however, I find it a bit confusing and the documentation I think could be better. What I needed is something like this:
background.SetAnchor(uiRoot);
background.leftAnchor.Set(1f, -475); //right edge of screen minus width of sprite
background.rightAnchor.Set(1f, 0); //no idea why this is 1f? wouldn't 0f be right edge since the call is to rightAnchor?
background.topAnchor.Set(1f, -150);//150 is offset from top edge
background.bottomAnchor.Set(1f, -400); // 400 = -150 offset - height of sprite
background.ResetAnchors();
background.UpdateAnchors();
7 calls of somewhat cryptic params for something that imo could be a simple single call:
UISprite.SetAnchor(Transform parent, bool left, bool right, bool top, bool bottom);
Some things can be assumed like a right anchor anchors to right side of sprite (what's the point to anchor it starting offscreen?) left anchor to the left side of the sprite, etc.
anchor both right/left or top/bottom and assume stretching, etc or add a stretch bool. If multiple sides are anchored without stretch then center between the two relative to starting position %s. If you are setting anchors in code chances are you already set the position you want the sprite in prior to anchoring. The editor tool does this for you perfectly well, how about a simple helper to do the same and make the same assumptions. Keep the tedious way for those that have the rare exception.
background.SetAnchor(uiRoot);
background.leftAnchor.Set(1f, -475); //right edge of screen minus width of sprite
background.rightAnchor.Set(1f, 0); //no idea why this is 1f? wouldn't 0f be right edge since the call is to rightAnchor?
background.topAnchor.Set(1f, -150);//150 is offset from top edge
background.bottomAnchor.Set(1f, -400); // 400 = -150 offset - height of sprite
background.ResetAnchors();
background.UpdateAnchors();
7 calls of somewhat cryptic params for something that imo could be a simple single call:
UISprite.SetAnchor(Transform parent, bool left, bool right, bool top, bool bottom);
Some things can be assumed like a right anchor anchors to right side of sprite (what's the point to anchor it starting offscreen?) left anchor to the left side of the sprite, etc.
anchor both right/left or top/bottom and assume stretching, etc or add a stretch bool. If multiple sides are anchored without stretch then center between the two relative to starting position %s. If you are setting anchors in code chances are you already set the position you want the sprite in prior to anchoring. The editor tool does this for you perfectly well, how about a simple helper to do the same and make the same assumptions. Keep the tedious way for those that have the rare exception.
