Get Aspect Ratio

Author: Luka Kotar (LukaKotar)

Description
This simple class allows you to quickly find out an aspect ratio between two values. Example: if you pass 1280 and 720 (or 1280 x 720 as Vector2) into the function, it will return 16 x 9 as Vector2.

Note: Providing a resolution with an invalid aspect ratio will provide incorrect results, or in rare cases even cause your game/Unity to freeze! Setting the aspect ratio in the "Game" view inside the Unity editor will usually work, but it will sometimes provide incorrect results. For example: setting the aspect ratio to 16:9 in the Game view provides a resolution to 1060 x 569, and the result provided by the script is 265:149. If used to calculate the aspect ratio of the user's screen resolution, this script is only useful for fullscreen usage, and windowed with disabled resizing. (Edit > Project Settings > Player > Resizable Window) Also note that the 16:10 aspect ratio will return 8:5. (16/2=8 & 10/2=5, therefore 16:10=8:5)

Usage
You do not need to attach the script to any game object.

Allowed syntaxes:
 * AspectRatio.GetAspectRatio (int x, int y)
 * AspectRatio.GetAspectRatio (int x, int y, bool debug)
 * AspectRatio.GetAspectRatio (Vector2 xy)
 * AspectRatio.GetAspectRatio (Vector2 xy, bool debug)

Here is a few examples:

JavaScript version:

AspectRatioCalculator.cs (Editor Window)
This editor window was intended for testing purposes, but it can be useful if you want to find out an aspect ratio of a resolution.

Note: Once this window is closed or Unity is re-opened, it will be reset to the original values.

Usage:
 * 1) The script has to be located in the Editor folder (Assets/Editor), and it requires the AspectRatio script from above to be located in the same project. (C# or the JavaScript version, both will work)
 * 2) Locate the editor window under Window > Aspect Ratio Calculator
 * 3) Enter the values and click "Calculate Aspect Ratio"

Download as Unity package
Click here to download GetAspectRatioCS.unitypackage (2.4 KB)

Click here to download GetAspectRatioJS.unitypackage (2.4 KB)