2
\$\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\$
3
  • \$\begingroup\$ Can you post more information about what you tried to do in the camera script? Include succinct code, if possible. \$\endgroup\$ Commented Oct 31, 2014 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\$ Commented Oct 31, 2014 at 18:11
  • \$\begingroup\$ For those who don't know letterbox: Read Here \$\endgroup\$
    – Emadpres
    Commented Oct 31, 2014 at 19:41

2 Answers 2

1
\$\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\$
3
  • \$\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\$ Commented Nov 1, 2014 at 16:03
  • \$\begingroup\$ Ah yes, just cast the float variable to an int. Answer updated. \$\endgroup\$
    – Entity
    Commented Nov 1, 2014 at 16:13
  • 1
    \$\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
    Commented Nov 1, 2014 at 17:55
1
\$\begingroup\$

After reading the answer above, I created a script which rescales the window every a fixed amount of frames, because changing every frame causes a lot of problems.

Just put it on the MainCamera:

using UnityEngine;

public class CameraAspectRatioManager : MonoBehaviour
{
    [SerializeField] private int widthAspect = 16;
    [SerializeField] private int heightAspect = 9;
    [Tooltip("After every how many frames to rescale the windows size.")]
    [SerializeField] private int updateAspectDelay = 100;

    private int frame = 0;

    private int lastWidth = 0;
    private int lastHeight = 0;

    private void Update()
    {
        frame++;

        if (frame % updateAspectDelay != 0) { return; }

        var width = Screen.width;
        var height = Screen.height;

        if (lastWidth != width) // if the user is changing the width
        {
            // update the height
            float heightAccordingToWidth = (float) width / widthAspect * heightAspect;
            Screen.SetResolution(width, (int)Mathf.Round(heightAccordingToWidth), false);
        }
        else if (lastHeight != height) // if the user is changing the height
        {
            // update the width
            float widthAccordingToHeight = (float) height / heightAspect * widthAspect;
            Screen.SetResolution((int)Mathf.Round(widthAccordingToHeight), height, false);
        }

        lastWidth = width;
        lastHeight = height;
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .