- Home /
Scale viewport/game by width instead of height?
Hello unitycommunity!
I've noticed that by default, when I make a build of my game and I change the screen WIDTH, the window will crop away parts of the game.
But when I change the screen HEIGHT of my game, all the game content zooms in and becomes bigger.
I need the exact opposite effect, so that changing width will make the game change in size, and changing screen height will crop or show more of the game.
Here's a picture:
Answer by oStaiko · Oct 28, 2016 at 08:00 PM
So, for an aspect ratio, you are using free aspect. This allows players to size the screen however they want. With cameras in free aspect, this is the default of how it sizes things up, and after a bit of research there's no easy way to do this. If you want to switch it as you've said above, then you'll have to script it in yourself. You can do that With a combination of screen resolution, and camera projection matrix. This gets tough and requires some non-default camera options, which you can read more on at the Screen API and the Projection Matrix API.
function Start ()
{
wScaleFactor=cam.projectionMatrix.m00; //Sets base scale
hScaleFactor=cam.projectionMatrix.m11; //Otherwise it'll look flat
}
function Update ()
{
cam.ResetProjectionMatrix();
var m = cam.projectionMatrix;
m.m00 = wScaleFactor; //width scale remains constant
m.m11 = (Screen.width/Screen.height) * hScaleFactor; //height scale changes to match screens height
cam.projectionMatrix = m;
}
Now this script works fine on it's own, however in my tests it was full of unexplainable bugs. Use it as a basis to get you started, and along with the APIs I've included you should do just fine. The bugs for me were getting a black screen when width was less than height, and some odd jumps between scales. This probably has to do with the matrix being a large trig function but I'll leave that to you to figure out...
Another (unprobable) lead I found for that is to actually rotate the camera 90 degrees. Then when you drag the height, you're actually dragging the width which makes it how you want. I suppose if you rotate all of your UI and your monitor 90 degrees it'll be perfect, right?
Your answer
Follow this Question
Related Questions
How to realize Image.fillAmount with Sprites in game? 0 Answers
Make long distance objects visible on camera 0 Answers
Camera picture stretching when width changed in script? 1 Answer
Is it better to enlarge an image by width/height or by scale? 0 Answers
Is it possible to quit the game before the window appears? 0 Answers