Search code examples
c#unity-game-engineunity-editor

How to change aspect ratio of Game View using c# script


How can I change the GameWindow aspect ratio using C# within the Unity editor

Like this:

Example

Thanks


Solution

  • You can do this using reflection:

    public static void ChangeResolution(int sizeIndex)
    {
        var assembly = typeof(Editor).Assembly;
        var gameView = assembly.GetType("UnityEditor.GameView");
        var instance = EditorWindow.GetWindow(gameView);
        gameView.GetMethod("SizeSelectionCallback")!.Invoke(instance, new object[] { sizeIndex, null });
    }
    

    sizeIndex is the index of the resolution in dropdown (0 - free aspect; 1 - 800x480)