Skip to content

Instantly share code, notes, and snippets.

@nicoplv
Last active February 27, 2024 03:15
Show Gist options
  • Save nicoplv/e6253677d5bb2841b0451d308923c232 to your computer and use it in GitHub Desktop.
Save nicoplv/e6253677d5bb2841b0451d308923c232 to your computer and use it in GitHub Desktop.
Full screen game view on Unity editor (works only with 2017.2 or +)
using UnityEditor;
using UnityEngine;
namespace FullScreenPlayModes
{
[InitializeOnLoad]
public class FullScreenPlayMode : Editor
{
//The size of the toolbar above the game view, excluding the OS border.
private static int toolbarHeight = 22;
static FullScreenPlayMode()
{
EditorApplication.playModeStateChanged -= PlayModeStateChanged;
EditorApplication.playModeStateChanged += PlayModeStateChanged;
}
static void PlayModeStateChanged(PlayModeStateChange _playModeStateChange)
{
if (PlayerPrefs.GetInt("PlayMode_FullScreen", 0) == 1)
{
// Get game editor window
EditorApplication.ExecuteMenuItem("Window/Game");
System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
System.Reflection.MethodInfo GetMainGameView = T.GetMethod("GetMainGameView", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
System.Object Res = GetMainGameView.Invoke(null, null);
EditorWindow gameView = (EditorWindow)Res;
switch (_playModeStateChange)
{
case PlayModeStateChange.EnteredPlayMode:
Rect newPos = new Rect(0, 0 - toolbarHeight, Screen.currentResolution.width, Screen.currentResolution.height + toolbarHeight);
gameView.position = newPos;
gameView.minSize = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height + toolbarHeight);
gameView.maxSize = gameView.minSize;
gameView.position = newPos;
break;
case PlayModeStateChange.EnteredEditMode:
gameView.Close();
break;
}
}
}
[MenuItem("Tools/Editor/Play Mode/Full Screen", false, 0)]
public static void PlayModeFullScreen()
{
PlayerPrefs.SetInt("PlayMode_FullScreen", 1);
}
[MenuItem("Tools/Editor/Play Mode/Full Screen", true, 0)]
public static bool PlayModeFullScreenValidate()
{
return PlayerPrefs.GetInt("PlayMode_FullScreen", 0) == 0;
}
[MenuItem("Tools/Editor/Play Mode/Window", false, 0)]
public static void PlayModeWindow()
{
PlayerPrefs.SetInt("PlayMode_FullScreen", 0);
}
[MenuItem("Tools/Editor/Play Mode/Window", true, 0)]
public static bool PlayModeWindowValidate()
{
return PlayerPrefs.GetInt("PlayMode_FullScreen", 0) == 1;
}
}
}
@nicoplv
Copy link
Author

nicoplv commented Aug 20, 2017

@Bian-Sh
Copy link

Bian-Sh commented Jan 7, 2019

can you make it work for unity 2018?

@Tymski
Copy link

Tymski commented Mar 16, 2021

Checked on 2020 - doesn't work at all.
Edit: For 2020 there is this https://gist.github.com/fnuecke/d4275087cc7969257eae0f939fac3d2f with some bugs but works

@Chillu1
Copy link

Chillu1 commented Jun 14, 2021

Checked on 2020 - doesn't work at all.
Edit: For 2020 there is this https://gist.github.com/fnuecke/d4275087cc7969257eae0f939fac3d2f with some bugs but works

I improved on that one with some automation QoL: https://gist.github.com/Chillu1/4c209308dc81104776718b1735c639f7

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