Hi again,

This one has been stumping me for a long time, also back when I was using a previous UI/tweening tool.  I'm not entirely sure if it's something I'm not doing right, or if it's a limitation, but anyway I'll try to explain and hopefully you may be able to help me.

I am writing a phone game for iOS where the user can change the phone orientation from portrait to landscape and the autorotate kicks in.  There are three "HUD" panels at the top edge of the screen (top left, top right, and top center) which tween by sliding out when the pause menu panel opens, and sliding back in with a bounce when the menu closes.  This *mostly* works fine using the anchors to keep it all consistent, but there is an issue.

Let's say I start in portrait mode.  I am using a relative Move tween to for example move my top left panel from its usual offscreen Pos X of -50 to a relative Move X of 50.  The panels slide in and out fine when triggered and all looks good.  Also, if I start in Landscape mode, it works as well.  So I am speculating that perhaps DOTween (or Unity?) could be recording the "at start" resolution somehow and basing calculations off that.

Anyway once the panels are currently onscreen in Portrait and I tilt the phone to landscape, Unity adjusts it fine via the anchors and the panels stay in their correct positions at the top corners and top center, adjusted proportionally for the change in resolution.  But then - disaster!  When I trigger my tween to slide the panels back out, the panels do not leave properly but instead jump off towards the middle of the screen as if the anchor was somehow still set to the Portrait X sizing.  Also when triggering the panels to slide back in, they also clump up on the left and the top right panel ends up in the middle - again I am speculating that somehow the calcs are still thinking the anchor edge is the one from Portrait mode, and that something wasn't refreshed when the phone orientation changed.

Any ideas?  This can also be reproduced in the editor by changing the resolution from say iPhone tall to iPhone wide during play.  I'm really hoping it's just something I haven't done properly or that there's a workaround for it, so any advice you might have will be appreciated.

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Hi again :)

First of all, consider that a relative tween sets its final endValue as "relative to its start value", but after that is done, both the startValue and the endValue are "fixed", meaning that if you restart the same tween after moving your target, it will jump to its original startValue and tween to its original endValue.

That said, you shouldn't move a Transform when working with the UI, because changing the transform's position of a UGUI object can hit performance quite a lot, since Unity has to "rearrange" a lot of variables to then keep correct anchors, raycasts, etc. You should instead change a RectTransform's anchoredPosition, which is much more performant.

You can animate that by using the RectTransform shortcuts (specifically DOAnchorPos). Also, if you do that, TADAH! Everything will still work correctly after your screen changes from landscape to portrait and viceversa, because while a UGUI's Transform position actually changes when the screen layout changes (and thus the "fixed in stone" relative tween will behave as I explained in the intro to this post), a RectTransform's anchoredPosition doesn't :)
« Last Edit: April 21, 2015, 08:42:27 PM by Daniele »

Ok thanks, I will try that.  I don't see DOAnchorPos in the dropdown list in the visual editor however, so does that mean these can only be done via code?

EDIT: Actually I just discovered what the problem was - I've installed the latest downloads area patch and noticed that this was the fix you applied in 0.9.180, to have Move use anchor pos for UI objects.  I had been still running off the stock install before that.  Works perfectly now!
« Last Edit: April 23, 2015, 10:14:35 AM by cynicalwanderer »

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Oh sorry, I thought you were actually using script and not the visual editor. And yes, the new update uses anchorPosition by default when applying a Move to UGUI objects. Glad it's solved :)