Author Topic: Where to use anchors best?  (Read 3905 times)

c-Row

  • Guest
Where to use anchors best?
« on: May 04, 2013, 10:34:15 AM »
Is there a performance difference in where you put your anchors in NGUI? If I want to labels, one in the top left and the other in the top right corner, should I add them to the same panel and add an anchor to each of them, or should I create a panelTopLeft and a panelTopRight, each with an anchor on its own? Or does it come down to personal preference?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Where to use anchors best?
« Reply #1 on: May 04, 2013, 07:32:30 PM »
Anchor a panel, make labels a child of that panel. Moving a panel is always faster than moving widgets, and the fewer anchors you have, the better the performance will be.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Where to use anchors best?
« Reply #2 on: May 04, 2013, 08:09:02 PM »
The Unity profiler may be throwing you some red herrings if you're looking to optimize here, since the actual UIAnchor.Update isn't necessarily heavy if you anchor individual elements in a UIPanel, but UIPanel.LateUpdate will be very heavy, because the moving of widgets inside a UIPanel has the sideeffect of making the panel redraw everything it is responsible for drawing.

Put anchors as high as you can in your hierarchy and do your best to have UIPanels below them, so any given UIpanel doesn't need to be rebuilt every update.

geo

  • Guest
Re: Where to use anchors best?
« Reply #3 on: May 14, 2013, 10:47:42 AM »
Looking at the UIAnchor class, it looks like the anchored object is repositioned every frame - is this necessary?

I have a mobile app and generally I am anchoring widgets to sides of the main window, and since my orientation is locked to landscape the main window never changes size. If repositioning only occurred in the Start() function, would this combat the performance issue you mentioned above (relating to anchoring widgets rather than panels)?

Thanks!

geo

  • Guest
Re: Where to use anchors best?
« Reply #4 on: May 14, 2013, 10:54:04 AM »
Oops I just saw the line at the end of UIAnchor that says the transform is not applied if it isn't changed! It's still recalculated but I suppose the overhead of this is relatively small.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Where to use anchors best?
« Reply #5 on: May 16, 2013, 11:00:54 AM »
I'm considering making a option in the anchor so it can "one shot" the anchoring and then turn itself off. We tend to have to have something else turn the anchor off, if we want to animate things and such so it could be useful. Plus, for all mobiles, the resolution never changes, so it wouldn't hurt to only have the overhead once.