I’m trying to figure out how to set up a script to detect the aspect ratio of the device the game is playing on and set the aspect ratio of the game itself to fit the screen.
public class SetAspectRatio : MonoBehaviour {
private float screenAspect;
// Use this for initialization
void Start () {
screenAspect = Screen.width/Screen.height;
//screenAspect = (float)Screen.width/(float)Screen.height;
}
// Update is called once per frame
void Update () {
//Debug.Log ("ScreenAspect = " + screenAspect);
Debug.Log ("ScreenAspect = " + screenAspect.ToString());
//Debug.Log (string.Format ("ScreenAspect = {0}", screenAspect));
}
}
I believe I would be able to use a switch statement to switch the aspect ratio but the problems are, I have no idea how to actually force a change in aspect ratio (I checked the script reference for ApectRatio but I don’t know if those simply set the ratio or something else), and I’m having trouble actually detecting the aspect ratio of the device.
The code above is the start of the code with a basic Debug.Log just so I can see whether aspect ratio; I don’t have multiple systems to test this on so that’s the only way I would be able to know if it worked. The thing is, this always returns AspectRatio = 0 no matter which of the Debug.Log’s I use (found them in a thread on how to print a variable in a Debug.Log).
Any help with the current coding problems or how to figure out the rest of the problems would be greatly appreciated.
You want to “set the aspect ratio of the game itself”? I take it you don’t mean stretch the image in one direction. The first question is if you are doing 2D and you’re trying to account for the sprites being drawn to only some predefined width on a predefined aspect ratio.
If you’re talking 3D, you need do nothing for the 3D environment itself. You just need to worry about UI or GUI at that point. But basically to explain it better, if you ran the 3D game (no UI) on a 3:4 screen, then ran it on a 16:9 screen, you would just see more on the left/right side of the image on the 16:9 screen. It’s like a picture frame on the 3:4 screen. As stated earlier, Unity will render the same world space from top to bottom on both monitors (you can expect the monitors to look identical as far as scaling up/down, and it just shows more “stuff” on the left right as you increase the width of the monitor (or device).
If you’re talking about 2D, and you only draw your sprites in a specific rectangular area, you do have to consider the impact to various aspect ratios then, because wider screens will show the black of the world at the edges. The key is to pull the Game window off of the Unity and view it by itself. Stretch the window around in Free Aspect Ratio mode, and whatever you see there is what you’ll see on a device similarly shaped. Also, you can select various aspect ratios from the pulldown in the top left corner of the Game window. If set to Mobile, it provides mobile resolutions as well.
In regard to your script, you would not want to implement any kind of switch statement. First, aspect ratio is a floating point number, so you could never have a switch statement that would work. Secondly, you really want to take the aspect ratio value and multiply it by other parameters in your game (i.e. you WILL want to do this for UI most likely if you expect a ton of different aspect ratios (i.e. android)).