Author Topic: UIAnchor  (Read 27489 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
UIAnchor
« 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 and Panels have a new Anchor section.

Overview

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.

   

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 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.

Pro-Tip

You can combine anchors with stretch scripts to create modular UIs:
http://www.youtube.com/watch?v=q1C5NwZasGs

Class Documentation

http://tasharen.com/ngui/docs/class_u_i_anchor.html

If you have a question regarding this component or would like me to clarify something, just post a reply here.
« Last Edit: December 06, 2013, 03:30:57 PM by ArenMook »

palehawk

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: UIAnchor
« Reply #1 on: August 03, 2014, 11:39:43 PM »
Hi,Aren!
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)。

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIAnchor
« Reply #2 on: August 04, 2014, 10:05:13 AM »
That won't happen. This is a deprecated component and it should not be used anymore.