Adam's Lair Forum

Scaling image procedurally to full screen
Page 1 of 1

Author:  Siggi05 [ 2016/12/10, 22:46 ]
Post subject:  Scaling image procedurally to full screen

Hi there,
I'm trying to scale an image procedurally to fullscreen while using a flat camera.
I've written the following code in a plugin to do this:
                Rect rct = GameObj.GetComponent<SpriteRenderer>().Rect;
                Vector2 res = DualityApp.TargetResolution;
                if (rct.W / rct.H > res.X / res.Y)
                    GameObj.Transform.Scale = res.Y * 2 / rct.H;
                    GameObj.Transform.Scale = res.X * 2 / rct.W;

The problem is, that this code will only work correctly, if i debug the game in Visual Studio. If i start the game in the Dualitor with navigating to Run -> Run Game, the image won't be displayed correctly.
Has anyone experiences with this problem, so that he can help me?
That would be very nice.

Author:  Adam [ 2016/12/10, 23:52 ]
Post subject:  Re: Scaling image procedurally to full screen

Scaling an image to fit a rectangular area while maintaining aspect ratio is one of those problems that I end up googling every single time I do it, and get it wrong a few times until it works. So I'm not exactly in the best position to help you by looking at that code and spotting something xD

However, I can provide some questions that you can check to find out what's wrong and help us get an idea of the situation:

  • Did you debug-step through the code? Does it behave as expected?
  • Did you print out the target resolution? Is it different in editor and launcher?
  • What exactly does "is not displayed correctly" mean?
  • When you instead of the target resolution use a Vector2 that you can specify via property (default to non-zero, so you don't divide by zero), how does it behave?

Note: Moved this to the programming subforum.

Author:  Siggi05 [ 2016/12/11, 16:18 ]
Post subject:  Re: Scaling image procedurally to full screen

I've solved my problem by myself:
It's about the given

My screen has the resolution of 1920 x 1080.
In VS the TargetResolution is half as big as the real resolution: 960 x 540.
To make sure my calculation is correct, i have to get the real resolution. Thats why I wrote
res.Y * 2
res.X * 2

While running the game in VS, it works, but the TargetResolution will be the real resolution, if I run this in the Dualitor. In consequence of that the scaling of the image is twice as big as it should be.
I've fixed the code for the duality launcher (without * 2), because I won't play my game in VS, if it's finished. ;)
I hope you can ignore my bad grammar and understand my solution nevertheless xD

Page 1 of 1 All times are UTC + 1 hour [ DST ]
Powered by phpBB® Forum Software © phpBB Group