Skip to content

Instantly share code, notes, and snippets.

@fnuecke
Created March 27, 2020 16:23
Show Gist options
  • Save fnuecke/d4275087cc7969257eae0f939fac3d2f to your computer and use it in GitHub Desktop.
Save fnuecke/d4275087cc7969257eae0f939fac3d2f to your computer and use it in GitHub Desktop.
#if UNITY_EDITOR
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
/// <summary>
/// Little utility for opening a "Game" view in fullscreen. Will be opened on whatever Unity thinks is the "main"
/// monitor at the moment. The hotkey will toggle the window; however, if for some reason this breaks, fullscreen
/// windows can be closed via Alt+F4 as long as the editor is not in play mode.
/// </summary>
/// <remarks>
/// Confirmed to work in Unity 2019 and 2020. May work in earlier and later versions. No promises.
/// </remarks>
public static class FullscreenGameView
{
static readonly Type GameViewType = Type.GetType("UnityEditor.GameView,UnityEditor");
static readonly PropertyInfo ShowToolbarProperty = GameViewType.GetProperty("showToolbar", BindingFlags.Instance | BindingFlags.NonPublic);
static readonly object False = false; // Only box once. This is a matter of principle.
static EditorWindow instance;
[MenuItem("Window/General/Game (Fullscreen) %#&2", priority = 2)]
public static void Toggle()
{
if (GameViewType == null)
{
Debug.LogError("GameView type not found.");
return;
}
if (ShowToolbarProperty == null)
{
Debug.LogWarning("GameView.showToolbar property not found.");
}
if (instance != null)
{
instance.Close();
instance = null;
}
else
{
instance = (EditorWindow) ScriptableObject.CreateInstance(GameViewType);
ShowToolbarProperty?.SetValue(instance, False);
var desktopResolution = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);
var fullscreenRect = new Rect(Vector2.zero, desktopResolution);
instance.ShowPopup();
instance.position = fullscreenRect;
instance.Focus();
}
}
}
#endif
@donsledzion
Copy link

Awesome dude! Works perfectly. I've needed it to record my VR application gameplay (before building it) with the highest resollution possible. Thank You!

@IAMAdamN
Copy link

for other users

using ctrl+shift+alt+F as shortcutkey

watch this youtube video for more info: https://youtu.be/ebRRYUawPZk

#if UNITY_EDITOR

using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;

/// <summary>
/// Little utility for opening a "Game" view in fullscreen. Will be opened on whatever Unity thinks is the "main"
/// monitor at the moment. The hotkey will toggle the window; however, if for some reason this breaks, fullscreen
/// windows can be closed via Alt+F4 as long as the editor is not in play mode.
/// </summary>
/// <remarks>
/// Confirmed to work in Unity 2019 and 2020. May work in earlier and later versions. No promises.
/// </remarks>
public static class FullscreenGameView
{
    static readonly Type GameViewType = Type.GetType("UnityEditor.GameView,UnityEditor");
    static readonly PropertyInfo ShowToolbarProperty = GameViewType.GetProperty("showToolbar", BindingFlags.Instance | BindingFlags.NonPublic);
    static readonly object False = false; // Only box once. This is a matter of principle.

    static EditorWindow instance;

    [MenuItem("Window/General/Game (Fullscreen) %#&f", priority = 2)]
    public static void Toggle()
    {
        if (GameViewType == null)
        {
            Debug.LogError("GameView type not found.");
            return;
        }

        if (ShowToolbarProperty == null)
        {
            Debug.LogWarning("GameView.showToolbar property not found.");
        }

        if (instance != null)
        {
            instance.Close();
            instance = null;
        }
        else
        {
            instance = (EditorWindow)ScriptableObject.CreateInstance(GameViewType);

            ShowToolbarProperty?.SetValue(instance, False);

            var desktopResolution = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height);
            var fullscreenRect = new Rect(Vector2.zero, desktopResolution);
            instance.ShowPopup();
            instance.position = fullscreenRect;
            instance.Focus();
        }
    }
}

#endif

@sagifalek
Copy link

how to get out of the fullscreen

@KrolPower
Copy link

Tip, if you set the keybind as something that already exists in the engine like "%p" (ctrl+p), then on first trigger unity will prompt that you have duplicate commands set, allowing you to rekey in editor to anything you want like "F11" key, ect.

@josessin
Copy link

josessin commented Sep 24, 2024

Hi! thanks for this! I've just added same little checks to make sure the resolution is set automatically and to prevent entering in fullscreen mode if the app is not running, I've also included the @StevenLightning code, just in case

#if UNITY_EDITOR

using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;

public static class FullscreenGameView
{
    static readonly Type GameViewType = Type.GetType("UnityEditor.GameView,UnityEditor");
    static readonly PropertyInfo ShowToolbarProperty = GameViewType.GetProperty("showToolbar", BindingFlags.Instance | BindingFlags.NonPublic);
    static readonly MethodInfo SetSizeProperty = GameViewType.GetMethod("SizeSelectionCallback", BindingFlags.Instance | BindingFlags.NonPublic);
    static readonly object False = false;

    static EditorWindow instance;

    // Update the shortcut to F12
    [MenuItem("Window/General/Game (Fullscreen) _F12", priority = 2)]
    public static void Toggle()
    {
        if (!EditorApplication.isPlaying)
        {
            Debug.LogWarning("You can only enter fullscreen mode while the game is running.");
            return;
        }

        if (GameViewType == null)
        {
            Debug.LogError("GameView type not found.");
            return;
        }

        if (ShowToolbarProperty == null)
        {
            Debug.LogWarning("GameView.showToolbar property not found.");
        }

        if (instance != null)
        {
            instance.Close();
            instance = null;
        }
        else
        {
            instance = (EditorWindow) ScriptableObject.CreateInstance(GameViewType);

            ShowToolbarProperty?.SetValue(instance, False);

            var gameViewSizesInstance = GetGameViewSizesInstance();
            int monitorWidth = Screen.currentResolution.width;
            int monitorHeight = Screen.currentResolution.height;

            if (SetSizeProperty != null)
            {
                int sizeIndex = FindResolutionSizeIndex(monitorWidth, monitorHeight, gameViewSizesInstance);
                SetSizeProperty.Invoke(instance, new object[] { sizeIndex, null });
            }

            var desktopResolution = new Vector2(monitorWidth, monitorHeight);
            var fullscreenRect = new Rect(Vector2.zero, desktopResolution);
            instance.ShowPopup();
            instance.position = fullscreenRect;
            instance.Focus();
        }
    }

    private static object GetGameViewSizesInstance()
    {
        var sizesType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizes");
        var singleType = typeof(ScriptableSingleton<>).MakeGenericType(sizesType);
        var instanceProp = singleType.GetProperty("instance");
        return instanceProp.GetValue(null, null);
    }

    private static int FindResolutionSizeIndex(int width, int height, object gameViewSizesInstance)
    {
        var groupType = gameViewSizesInstance.GetType().GetMethod("GetGroup");
        var currentGroup = groupType.Invoke(gameViewSizesInstance, new object[] { (int)GameViewType.GetMethod("GetCurrentGameViewSizeGroupType").Invoke(instance, null) });

        var getBuiltinCount = currentGroup.GetType().GetMethod("GetBuiltinCount");
        var getCustomCount = currentGroup.GetType().GetMethod("GetCustomCount");
        var getGameViewSize = currentGroup.GetType().GetMethod("GetGameViewSize");

        int totalSizes = (int)getBuiltinCount.Invoke(currentGroup, null) + (int)getCustomCount.Invoke(currentGroup, null);

        for (int i = 0; i < totalSizes; i++)
        {
            var size = getGameViewSize.Invoke(currentGroup, new object[] { i });
            var widthProp = size.GetType().GetProperty("width");
            var heightProp = size.GetType().GetProperty("height");

            int w = (int)widthProp.GetValue(size, null);
            int h = (int)heightProp.GetValue(size, null);

            if (w == width && h == height)
            {
                return i;
            }
        }

        Debug.LogWarning("Resolution not found. Defaulting to index 0.");
        return 0;
    }

    [MenuItem("Window/LayoutShortcuts/Default %q", false, 2)]
    static void DefaultLayout()
    {
        EditorApplication.ExecuteMenuItem("Window/Layouts/Default");
    }
}

#endif

@PalaNolho
Copy link

PalaNolho commented Sep 28, 2024

I'm using 2 monitors and when I use this script it extends the fullscreen partially to the other monitor.

  • I'm using @josessin version but the same happens with the original one
  • I have the resolution set to match my screen aspect ratio
  • I start the full screen only after playmode is running

Any ideas?

@Roland09
Copy link

Roland09 commented Oct 11, 2024

I'm using 2 monitors and when I use this script it extends the fullscreen partially to the other monitor.

Any ideas?

@PalaNolho I had the same. Dividing the resolution by EditorGUIUtility.pixelsPerPoint worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment