0
\$\begingroup\$

I want a scalable game window that when resized stays at 16:9 aspect ratio. The goal is for the user to be able grab the window on the right or bottom and resize it. The game should adjust the corresponding side of the view-port to lock the aspect ratio. I've tried forcing certain values in the update line of a script on the camera, but it did not work. the game is 2d and most of the elements are scripted using the gui functions. I'm having a very hard time finding exactly what I'm looking for as it is, most of what I've found is in an attempt to create black bars and keep the game from stretching, but that doesn't help ongui using matrices.

here are the resources I've tried so far

http://goo.gl/8FuyRC - this one is a script someone wrote that I attached to the main scene camera, I then built it, however that didn't work.

http://goo.gl/qZ5DAl - this is something from the unity website regarding camera aspect ratio. I put a line of code in the update function of my camera object

#pragma strict

function Start () {

}

function Update () {
Camera.main.orthographicSize = 3.596306;
}

as well as

#pragma strict

function Start () {

}

function Update () {
Camera.aspect = 1;
}

none of the above worked.

http ://goo.gl/MbTDcz - was the last thing I tried, and it just made some more black bars...

so far nothing has worked. not sure why. to reiterate, the goal I'm trying to accomplish is that, when the user grabs the edge of a windowed player, and drags it, the corresponding x or y axis will follow in suit to keep the aspect ratio the same.

\$\endgroup\$
  • \$\begingroup\$ Can you post more information about what you tried to do in the camera script? Include succinct code, if possible. \$\endgroup\$ – Seth Battin Oct 31 '14 at 18:02
  • \$\begingroup\$ Also I was pretty heavy-handed when editing your question, so feel free to correct my changes if I misinterpreted your meaning. If you do, try to highlight the most relevant parts and leave out any extraneous info. Shorter, more specific questions are easier to read and thus more likely to get helpful answers. \$\endgroup\$ – Seth Battin Oct 31 '14 at 18:11
  • \$\begingroup\$ For those who don't know letterbox: Read Here \$\endgroup\$ – Emadpres Oct 31 '14 at 19:41
0
\$\begingroup\$

From the script you're posting, it looks like you're changing variables on the camera, which won't affect the screen size. This is pseudocode (and untested), but this should be the right concept:

private var lastWidth : int = 0;
private var lastHeight : int = 0;

function Start () {

}

function Update ()
{
    var width = Screen.width;
    var height = Screen.height;

    if(lastWidth != width) // if the user is changing the width
    {
        // update the height
        var heightAccordingToWidth = width / 16.0 * 9.0;
        Screen.SetResolution(width, Mathf.Round(heightAccordingToWidth), false, 0);
    }
    else if(lastHeight != height) // if the user is changing the height
    {
        // update the width
        var widthAccordingToHeight = height / 9.0 * 16.0;
        Screen.SetResolution(Mathf.Round(widthAccordingToHeight), height, false, 0);
    }

    lastWidth = width;
    lastHeight = height;
}

Again, changing the camera height and width isn't the same as changing the window width/height.

\$\endgroup\$
  • \$\begingroup\$ so far it makes sense however i'm having an issue where it says "no appropriate version of unityengine.screen.setresolution for the argument list '(int,float)' was found.". could that be because one variable is an integer where the other is a float? \$\endgroup\$ – user2620255 Nov 1 '14 at 16:03
  • \$\begingroup\$ Ah yes, just cast the float variable to an int. Answer updated. \$\endgroup\$ – Entity Nov 1 '14 at 16:13
  • \$\begingroup\$ @user2620255 Updated my code again, this time it is tested and working. Make sure you enable "Resizable Window" in the player settings. \$\endgroup\$ – Entity Nov 1 '14 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.