4using System.Reflection;
15 public static class FullscreenGameView
17 static readonly Type GameViewType = Type.GetType(
"UnityEditor.GameView,UnityEditor");
18 private static readonly Type HostViewType = Type.GetType(
"UnityEditor.HostView,UnityEditor");
19 private static readonly Type ContainerWindowType = Type.GetType(
"UnityEditor.ContainerWindow,UnityEditor");
20 private static readonly PropertyInfo ShowToolbarProperty = GameViewType.GetProperty(
"showToolbar", BindingFlags.Instance | BindingFlags.NonPublic);
22 private readonly
static int DISPLAY_0 = 0;
23 private readonly
static int DISPLAY_7 = 7;
24 static EditorWindow instance;
27 private static PropertyInfo FindProperty(Type type,
string propertyName)
29 return type?.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
32 private static MethodInfo FindMethod(Type type,
string methodName, params Type[] args)
34 return args.Length == 0
35 ? type?.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
36 : type?.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
null, args,
null);
38 private static EditorWindow GetMainGameView()
40 Type gameViewType = typeof(EditorWindow).Assembly.GetType(
"UnityEditor.GameView");
41 if (gameViewType ==
null)
43 Debug.LogError(
"Unable to find the UnityEditor.GameView type.");
46 return EditorWindow.GetWindow(gameViewType);
49 private static void SetGameViewTargetDisplay(
int displayIndex)
51 EditorWindow gameView = GetMainGameView();
55 FindMethod(gameView.GetType(),
"SetTargetDisplay", typeof(
int))?.Invoke(gameView,
new object[] { displayIndex });
58 public static void EnterFullscreen()
60 if (GameViewType ==
null)
62 Debug.LogError(
"GameView type not found.");
66 if (ShowToolbarProperty ==
null)
68 Debug.LogWarning(
"GameView.showToolbar property not found.");
75 SetGameViewTargetDisplay(DISPLAY_0);
79 SetGameViewTargetDisplay(DISPLAY_7);
80 instance = (EditorWindow)ScriptableObject.CreateInstance(GameViewType);
81 var containerWindow = ScriptableObject.CreateInstance(ContainerWindowType);
82 var hostView = ScriptableObject.CreateInstance(HostViewType);
84 ShowToolbarProperty?.SetValue(instance,
false);
86 Vector2 position = Vector2.zero;
87 Vector2 resolution =
new Vector2(Screen.currentResolution.width, Screen.currentResolution.height) / EditorGUIUtility.pixelsPerPoint;
89 FindProperty(HostViewType,
"actualView")?.SetValue(hostView, instance);
91 var fullscreenRect =
new Rect(position, resolution);
92 FindProperty(ContainerWindowType,
"position")?.SetValue(containerWindow, fullscreenRect);
93 FindProperty(ContainerWindowType,
"rootView")?.SetValue(containerWindow, hostView);
95 var showMethod = ContainerWindowType?.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
96 .FirstOrDefault(m => m.Name ==
"Show" && m.GetParameters().Length is 3 or 4 or 5);
98 showMethod?.Invoke(containerWindow,
new object[] { 3,
false,
true,
true, 0 }.Take(showMethod.GetParameters().Length).ToArray());
100 FindProperty(ContainerWindowType,
"m_ShowMode")?.SetValue(containerWindow, 1);
101 FindProperty(ContainerWindowType,
"m_DontSaveToLayout")?.SetValue(containerWindow,
true);
103 FindMethod(ContainerWindowType,
"SetMinMaxSizes", typeof(Vector2), typeof(Vector2))?.Invoke(containerWindow,
new object[] { fullscreenRect.size, fullscreenRect.size });
107 public static void ExitFullscreen()
109 if (instance !=
null)
112 EditorApplication.delayCall += CloseFullscreenAfterDelay;
117 public static void SetFullscreen(
bool fullscreen)
119 if (instance ==
null && fullscreen)
123 else if (instance !=
null && !fullscreen)
129 private static void CloseFullscreenAfterDelay()
132 SetGameViewTargetDisplay(DISPLAY_0);
133 EditorApplication.delayCall -= CloseFullscreenAfterDelay;