Tasharen Entertainment Forum

Support => NGUI 3 Documentation => Topic started by: ArenMook on November 21, 2013, 01:14:39 AM

Title: UIAnchor
Post by: ArenMook on November 21, 2013, 01:14:39 AM
*** Deprecated in 3.0.7 ***

This component has been deprecated in NGUI version 3.0.7. Widgets (http://www.tasharen.com/forum/index.php?topic=6702) and Panels (http://www.tasharen.com/forum/index.php?topic=6705) have a new Anchor section.


UIAnchor makes it possible to anchor game objects to sides and corners of the screen as well as other widgets. This is the key component you should use to create modular UIs in NGUI 3.0.6 and earlier versions.

(http://www.tasharen.com/ngui/uianchor.jpg)   (http://www.tasharen.com/ngui/hierarchy.jpg)

Have you ever wanted to have a part of your UI remain "glued" to a corner or side of the screen? Take World of Warcraft for example. In that game, the map was always in the Top Right corner of the screen, and the hotkey bar was attached to the bottom of the screen, regardless of which screen resolution you went with. How can you do something similar? Well, with anchors, of course!

NGUI's default UI layout starts with one anchor for you -- a centered anchor. Going by the same World of Warcraft UI example, you'd have to add two more beside it. One anchor would be set to TopRight side, and the other would be set to Bottom. You would then add child widgets underneath them, and when the screen size changes, your UI will remain exactly where you want them to be.

You can also use anchors to attach UI elements to other UI elements by specifying a Container. If this container points to a UIWidget (http://www.tasharen.com/forum/index.php?topic=6702) then that widget's dimensions will be used instead of the screen's rectangle. If it's a game object, then the sum of the bounds of all the widgets underneath will be used. Because of this reason, be very careful when you specify a parent game object as a container for some anchored widget, as that sum will include the widget itself, which is likely not what you want.

You can adjust the Relative Offset to alter the position using relative values. For X of 1 this means 100% of the container's width. Y of 1? 100% of the container's height. 0.5 = 50%, and so on. You can specify negative values as well, if you prefer.

Although it might be easier to use Pixel Offset instead. It's exactly what you can guess -- the anchor's position is going to be offset by the given pair of values along the X and Y, in pixels.

By default, the anchor will only execute once and will then disable itself. It will still react to screen size changes, but nothing else. If you want the anchor's math to be executed every update, turn off the Run Only Once option.


You can combine anchors with stretch (http://www.tasharen.com/forum/index.php?topic=6713) scripts to create modular UIs:

Class Documentation


If you have a question regarding this component or would like me to clarify something, just post a reply here.
Title: Re: UIAnchor
Post by: palehawk on August 03, 2014, 11:39:43 PM
I suggest  add pivot in anchors,then I can just drag one object to the second object's  "pivot" in anchors,the the second object will be align with the first object's pivot.It will be convenient than making Lelf = the second object's width/2(center)、right = the second object's width/2(center)、Top = the second object's height/2(center)、Bottom = the second object's height/2(center)。
Title: Re: UIAnchor
Post by: ArenMook on August 04, 2014, 10:05:13 AM
That won't happen. This is a deprecated component and it should not be used anymore.