Author Topic: Anchor offsets changing inexplicably at runtime  (Read 2850 times)

charliehelman

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 2
  • Posts: 16
    • View Profile
Anchor offsets changing inexplicably at runtime
« on: November 10, 2014, 08:29:51 PM »
Recently upgraded from version 3.0.9 to 3.7.6 and found that one of our UI elements has some very strange behavior during runtime.

Here's what the anchoring and sprite look like in editor. I've tried anchoring it a few different ways (normal offset or based off current position, for example).


However, at runtime this is what happens. Note how the battery is skewed. It's caused by the offsets you see in the inspector. If I reset those all back to the in-editor values, it looks fine.


I'm observing this in Unity 4.5.4f1.

It's worth noting that (as far as I can tell) we're not modifying anything related to this sprite's position / scale, or its parent's scale, besides calling MakePixelPerfect during initialization.

What could be causing this? As I mentioned above, this only started happening after upgrading to the latest version.

I would be *extremely* grateful for any help tracking down and solving this issue.

charliehelman

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 2
  • Posts: 16
    • View Profile
Re: Anchor offsets changing inexplicably at runtime
« Reply #1 on: November 10, 2014, 09:57:52 PM »
I found another example of this and was able to fix it in this new case by removing any MakePixelPerfect calls affecting the Widget.

So it seems like with the latest version of nGUI, MakePixelPerfect will override/affect a widget's anchoring?

Maybe that's your intended functionality (I think it makes sense for most use-cases). I'm not sure why we're doing it in some of these cases; I'll work to remove these erroneous MakePixelPerfect calls as I find them, but in the original example posted above I haven't had any luck figuring out what is doing it to the battery (where we're calling MPP to it in the code).

Is there a way to work around this? Perhaps the Execute settings for the battery's anchor (OnUpdate, OnEnable, etc.) are the key?

Thanks for your time, Aren.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Anchor offsets changing inexplicably at runtime
« Reply #2 on: November 10, 2014, 10:10:44 PM »
MakePixelPerfect() changes width and height, and changing width and height now updates anchors, so yes, that's your culprit.

If you are not using native sprite size you shouldn't be using MakePixelPerfect() to begin with though. Even if you remove all anchoring then call MakePixelPerfect(), it will effectively change the size of your sprite to its native size. Current version of NGUI simply keeps it consistently so, whether it's anchored or not.

charliehelman

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 2
  • Posts: 16
    • View Profile
Re: Anchor offsets changing inexplicably at runtime
« Reply #3 on: November 10, 2014, 11:38:52 PM »
Awesome, thanks for the clarification. I'll be hunting down our poorly used MakePixelPerfect calls with a vengeance. (Some of this code is 2+ years old.)

Thanks Aren.