Author Topic: Scaling Tweens Based on Resolution  (Read 2456 times)

ShawnDing

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Scaling Tweens Based on Resolution
« on: February 28, 2015, 06:23:15 PM »
I'm having an odd problem and I'm hoping to get some help. I'm going to try my best to explain it, but let me know if this makes no sense.

I have a series of buttons that Tween Position at the start of my menu scene loading. I want them to be flush with the edge of the screen upon completion of the Tween. This is what I did:

1. Anchored the button to the outside edge of the screen
2. Run a "setup" method in Awake() that does the following for each Tween:

playButtonTween.SetStartToCurrentValue();
        playButtonTween.to = new Vector3(playButtonSprite.transform.localPosition.x - playButtonSprite.localSize.x,
            playButtonSprite.transform.localPosition.y,
            playButtonSprite.transform.localPosition.z);


This effectively tells the Button to only tween based off its current length.

3. Run the tween in a coroutine from Start().

Now, here is my issue. If this is the first scene I run, it works like a charm. The buttons move and the tweens get the proper values loaded.

However, if I have another scene (like a custom splash screen) load first, then load the scene with the above code, it doesn't work. The tweens use whatever resolution the scene was last saved at for the positions that they load to determine the from/to in the tween.

This means that if I last saved my scene in a higher resolution (say a width of 1080), but then try and test in a lower resolution (width of 800), the Tweens still get edited as though they are using the position for 1080, meaning that when they tween in, they go too far.

Hopeful my description makes sense. Any help that could be provided would be greatly appreciated.

Thank you,

Shawn

ShawnDing

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Scaling Tweens Based on Resolution
« Reply #1 on: February 28, 2015, 06:38:31 PM »
I've been doing some Debugging, and I have a bit more info to add to try and help point in the right direction:

If I do the following Debug line:
Debug.Log("Play Transform: " + playButtonSprite.transform.localPosition.ToString());

It displays Play Transform: (381.5, 0.0, 0.0) in the console (if I run the scene from the splash screen)
However, if I look at the object, the Transform is listed as: (395.5, 0.0, 0.0)

So it seems to be physically moving the button in the editor (which displays the proper position), but if I reference the transform's local position via code, it isn't updated.

Shawn

ShawnDing

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Scaling Tweens Based on Resolution
« Reply #2 on: February 28, 2015, 08:45:19 PM »
Okay, so I solved the problem (though it may not be ideal).

My Setup code in Awake() was running before NGUI had a chance to reposition everything properly. So I created a simple coroutine that waited for .1 seconds before calling the setup, and everything worked.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Scaling Tweens Based on Resolution
« Reply #3 on: March 01, 2015, 05:06:38 PM »
You can also just adjust the script execution order to have your code run after NGUI. Also... why Awake()? Awake is for local init only. Anything that accesses / modifies external scripts should be in Start(), not Awake().