- Home /
Is there a setting to default the Editor Game View to a specific aspect ratio?
I am trying to get the game view fixed to portrait mode. While this of course works on my computer, I can't find a way to set this for colleagues so that other people who access the repository will automatically see the game in portrait mode as well.
I don't know if there is a way to do that. I assume the set resolution or apsect ratio in the editor is only for your editor exclusively. What you can do for now is, to check if the aspect ratio of the game matches your desired ratio, if that is not the case, throw a Debug.Log Message that informs your colleagues that they have to add the ratio to their game view.
Thanks for your reply! Could you tell me how I can check the aspect ratio of the game in the editor view?
Answer by Vivien_Lynn · Aug 21, 2021 at 11:13 AM
I looked further into it and I found this link that might help you: https://answers.unity.com/questions/956123/add-and-select-game-view-resolution.html
I also tried to work out a simpler solution, that does not require actual Editor scripts. This solution grew quite big, since it needs a lot of commentary because it is not so straight forward what is happening under the hood. I attached my script below.
What my script essentially does is, it looks for the current aspect ratio of the game view, and if it is not set to 9:16, a Debug Message informs the programmer that they have to set the game view to 9:16.
It seems that Unity has some rounding issues when it comes to calculating the aspect ratio of the game view. So instead of checking if the current ratio is exactly 9:16 (=0.5625), I rather check if it is close to that number.
I can imagine that Unity returns different results on different computers, that's why I also added a debug mode, that allows to see what ratio Unity finds on your machine. I hope that my code or the link delivers a valid solution for you.
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
// Description:
// Attach this script to a GameObject in your Project
// This script will ensure that the game view window is always set to an aspect ratio of 9:16
// It checks the current aspect ratio and informs the programmer with a debug message if the aspect ratio is not set to 9:16
//
//
//
// Summary:
// When the game view ratio is set to 9:16, it is expected that the calculation '(float)Screen.width / (float)Screen.height' returns 9:16 = 0.5625
// This is not always the case (I am unsure why).
// When you set your game view ratio to 9:16 and you squeeze the view horizontally (left/right), to the point that Unity draws letter boxes (gray bars
// on top and bottom to maintain a ratio of 9:16), then the calculation returns values that are close to 0.5625, but not exact.
// In my tests it returns values between 0.5611111 and 0.5638298
// That is why I check for a ratio range in Update(), instead of just looking for (float)Screen.width / (float)Screen.height == 9f/16f
//
// If you want to find out what values your Editor returns for (float)Screen.width / (float)Screen.height, first set your game view window to 9:16.
// Enable the debugMode bool in the inspector and play around with the width of your game view
// Slowly stretch the windows enough so that Unity draws Pillar boxes (gray bars left and right), and squeeze it enough to force Unity to draw letter boxes
// (gray bars on top and bottom)
// After you have done this, rightclick on the script in the inspector and select "Print MinMax" to print the smallest and biggest ratio that Unity detected
// Use these numbers to set your min max values in the if-statment.
// For me that is 0.5611111 and 0.5638298
// So to make sure I dont trigger that if-statmen accidentally, I set my range from 0.560 to 0.564
[ExecuteInEditMode]
public class AspectRatioChecker : MonoBehaviour
{
[Tooltip("Debug Mode: Use this to determent a better ratio range for your game view (Read Summary inside this script for more information).")]
[SerializeField] private bool debugMode = false;
private List<float> ratios = new List<float>();
private void Update()
{
// Only do this in Editor and while not in Playmode
if (Application.isPlaying) return;
Debug.Log("Is Running!");
float ratio = (float)Screen.width / (float)Screen.height;
// Check if Aspect ratio is 9:16 (or very close to it)
if (0.560 <= ratio
&& ratio <= 0.564)
{
// We are in desired Range (very close to 9:16)
}
else
{
// Not in desired Range
Debug.Log($"Wrong aspect ratio deteced. Please set the aspect ratio of the game view to 9:16");
}
// Debug Mode
if (debugMode)
{
// To prevent the list from growing too big
if (ratios.Count < 1000)
{
ratios.Add(ratio);
Debug.Log($"Ratio: {ratio} " + '\n' +
$"List length: {ratios.Count}/1.000");
}
else
{
Debug.Log($"Ratio List already holds 1000 values. Please clear the List or disable DebugMode.");
}
}
else
{
ratios.Clear();
}
}
[ContextMenu("Print MinMax")]
private void PrintMinMax()
{
Debug.Log($"##### Elements in List: {ratios.Count} #####");
Debug.Log($"Min: {ratios.AsQueryable().Min()}");
Debug.Log($"Max: {ratios.AsQueryable().Max()}");
}
[ContextMenu("Empty List")]
private void EmptyList()
{
Debug.Log($"List cleared!");
ratios.Clear();
}
}
Thanks a lot! I ended up checking the ratio as you suggested but I only call it when the Editor enters the Playmode. Here is the code for that for anyone who stumbles upon the same problem.
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public static class AspectRatioChecker
{
// register an event handler when the class is initialized
static AspectRatioChecker()
{
EditorApplication.playModeStateChanged += CheckGameView;
}
private static void CheckGameView(PlayModeStateChange state)
{
if (state == PlayModeStateChange.EnteredPlayMode)
{
float ratio = (float)Screen.width / (float)Screen.height;
...
}
}
}
Answer by qsp18 · Aug 20, 2021 at 05:49 PM
In Game-View one centimeter below there is the word "Display 1". Right of that there is the word "Free Aspect". Click on it and select Aspect Ratio
Unfortunately, that's only a local change that isn't represented by the settings. I also didn't find a way to set the Aspect Ratio from the code. As stated in the question, I would like to transfer exactly this setting to colleagues automatically.