Author Topic: Bug report and feature request regarding Anchors  (Read 1893 times)

DEngland

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Bug report and feature request regarding Anchors
« on: January 17, 2014, 02:45:27 PM »
The Anchor system in NGUI is very nice but I need a bit more flexibility to get the layouts I need. In particular, I noticed that the Unified anchor doesn't play nice when there are modifications to the Scale part of the Transform.

Bug report: Unified Anchor does not account for Transform Scale.
If a Sprite has an X and Y scale of 2, then it will expand and contract at twice the rate that the anchor expects. This is likely because the anchor is just looking at the dimensions and not the transform. There is also a problem in the editor when switching a scaled Sprite from no anchor to the unified anchor; it gets blown out of size and out of position, and doesn't return when you set the anchor back to "none".

You might ask why I'm using Transform Scale on a Sprite that is anchored (since the anchor can resize the dimensions). It's true that anchors themselves can take care of the scaling in most cases, but this isn't always what you want for sliced sprites. In my project the UIRoot is set to a FixedSize Scaling Style with a manual height of 1536 in order to target the highest resolution tablets while also accommodating the lowest. This is necessary for crisp fonts etc at high res, but it also means exporting our UI textures at those resolutions, which takes up a lot of space in the atlas and isn't often necessary for those textures to look good. My solution is to store them at medium resolution in the atlas and adjust the X and Y scale in the Transform to bring them up to the proper size. This even works for sliced assets and I think is a sensible process but unfortunately it confuses the anchors.
I'm hoping this will not be too hard to address with some modifications to UIAnchor.cs but I wanted to post here before digging into that. Has anyone attempted this before?

Also I have a closely related feature request, unless there's some way to do this that I haven't thought of. I would like to be able to anchor to a position identified by a percentage of the target's width or height, instead of an absolute offset. For example, I have a horizontal divider and I want the left side to be anchored to the left side of its parent, but at a position of 25% of its width. This means that visually it will appear at the quarter-way mark whether it is viewed on a 16:9 screen or a 4:3 screen.

These two changes together can help a lot for developers who want the UI to be as consistent as possible across devices. Again, I'm willing to contribute to implementing/testing these changes.

Thanks
-Devan

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Bug report and feature request regarding Anchors
« Reply #1 on: January 17, 2014, 03:01:47 PM »
My question is... why are you modifying the transform scale instead of changing the Pixel Size on your atlas?

DEngland

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Bug report and feature request regarding Anchors
« Reply #2 on: January 17, 2014, 03:34:41 PM »
Thanks for the fast response, ArenMook.

I was unaware of the Pixel Size setting for the Atlas, but it does appear to have similar results to modifying the Transform Scale. This should allow me to work around the immediate issue although it's less flexible as every texture in an atlas must have the same pixel size and it can only scale in multiples of 100% (although this does cover most cases).

I still think that Anchors should be updated to account for scale in Transforms since currently scale and anchors are simply incompatible, and I'm sure there are other valid cases for wanting to use them together.

Do you have any feedback on the percentage-anchor suggestion? Or do you have any pointers if I want to dig in and try it myself?

Thanks
-Devan

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Bug report and feature request regarding Anchors
« Reply #3 on: January 17, 2014, 11:23:17 PM »
To get an anchor that's a percentage of something, set the anchor type to "Custom" instead of "Left", "Center", "Top", etc. The floating point value is your percent. 0 meaning 0%, 1 meaning 100%, 1.5 meaning 150%, etc. Check the documentation page for UIRect: http://www.tasharen.com/forum/index.php?topic=7013