using System; using System.Collections; using System.Collections.Generic; using System.Reflection; using UltraCombos; #if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; [ExecuteInEditMode] public class DisplayControl : MonoBehaviour { [AutoUI] public bool hideCursorInStandalone = true; public bool fullScreen = true; public int preferredRefreshRate = 60; public List resolutions = new List(); object gameViewSizesInstance; MethodInfo getGroup; public enum GameViewSizeType { AspectRatio, FixedResolution } void Start() { #if UNITY_EDITOR var sizesType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizes"); var singleType = typeof(ScriptableSingleton<>).MakeGenericType(sizesType); var instanceProp = singleType.GetProperty("instance"); getGroup = sizesType.GetMethod("GetGroup"); gameViewSizesInstance = instanceProp.GetValue(null, null); getGroup = sizesType.GetMethod("GetGroup"); gameViewSizesInstance = instanceProp.GetValue(null, null); #endif int num = Mathf.Min(resolutions.Count, Display.displays.Length); int width, height; if (num == 1) { width = (int)resolutions[0].x; height = (int)resolutions[0].y; string input; if (Misc.GetCommandLineArgument("-width", out input)) { int res; if (int.TryParse(input, out res)) { Misc.Verbose("Display Control", "get arg -width " + res); width = res; } } if (Misc.GetCommandLineArgument("-height", out input)) { int res; if (int.TryParse(input, out res)) { Misc.Verbose("Display Control", "get arg -height " + res); height = res; } } if (Misc.GetCommandLineArgument("-fullscreen", out input)) { bool res; if (bool.TryParse(input, out res)) { Misc.Verbose("Display Control", "get arg -fullscreen " + res); fullScreen = res; } } Screen.SetResolution(width, height, fullScreen, preferredRefreshRate); #if UNITY_EDITOR SetGameViewResolution(width, height); /* UnityEditorInternal.InternalEditorUtility.RepaintAllViews(); System.Reflection.Assembly assembly = typeof(UnityEditor.EditorWindow).Assembly; System.Type type = assembly.GetType("UnityEditor.GameView"); EditorWindow gameview = EditorWindow.GetWindow(type); EditorApplication.Step(); EditorApplication.update(); */ #endif } else if (num > 1) { for (int i = 0; i < num; i++) { var display = Display.displays[i]; width = (int)resolutions[i].x; height = (int)resolutions[i].y; display.Activate(width, height, preferredRefreshRate); display.SetParams(width, height, 0, 0); } } if (Application.isEditor == false) Cursor.visible = !hideCursorInStandalone; } #if UNITY_EDITOR void SetGameViewResolution(int width,int height) { int idx = FindSize(GameViewSizeGroupType.Standalone, width, height); if(idx == -1) { AddCustomSize(GameViewSizeType.AspectRatio, GameViewSizeGroupType.Standalone, width, height, ""); } SetSize(idx); } public int FindSize(GameViewSizeGroupType sizeGroupType, int width, int height) { // goal: // GameViewSizes group = gameViewSizesInstance.GetGroup(sizeGroupType); // int sizesCount = group.GetBuiltinCount() + group.GetCustomCount(); // iterate through the sizes via group.GetGameViewSize(int index) var group = GetGroup(sizeGroupType); var groupType = group.GetType(); var getBuiltinCount = groupType.GetMethod("GetBuiltinCount"); var getCustomCount = groupType.GetMethod("GetCustomCount"); int sizesCount = (int)getBuiltinCount.Invoke(group, null) + (int)getCustomCount.Invoke(group, null); var getGameViewSize = groupType.GetMethod("GetGameViewSize"); var gvsType = getGameViewSize.ReturnType; var widthProp = gvsType.GetProperty("width"); var heightProp = gvsType.GetProperty("height"); var indexValue = new object[1]; for (int i = 0; i < sizesCount; i++) { indexValue[0] = i; var size = getGameViewSize.Invoke(group, indexValue); int sizeWidth = (int)widthProp.GetValue(size, null); int sizeHeight = (int)heightProp.GetValue(size, null); if (sizeWidth == width && sizeHeight == height) return i; } return -1; } public void AddCustomSize(GameViewSizeType viewSizeType, GameViewSizeGroupType sizeGroupType, int width, int height, string text) { // GameViewSizes group = gameViewSizesInstance.GetGroup(sizeGroupTyge); // group.AddCustomSize(new GameViewSize(viewSizeType, width, height, text); var group = GetGroup(sizeGroupType); var addCustomSize = getGroup.ReturnType.GetMethod("AddCustomSize"); // or group.GetType(). var gvsType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSize"); var ctor = gvsType.GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(string) }); var newSize = ctor.Invoke(new object[] { (int)viewSizeType, width, height, text }); addCustomSize.Invoke(group, new object[] { newSize }); } object GetGroup(GameViewSizeGroupType type) { return getGroup.Invoke(gameViewSizesInstance, new object[] { (int)type }); } public void SetSize(int index) { var gvWndType = typeof(Editor).Assembly.GetType("UnityEditor.GameView"); var selectedSizeIndexProp = gvWndType.GetProperty("selectedSizeIndex", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); var gvWnd = EditorWindow.GetWindow(gvWndType); selectedSizeIndexProp.SetValue(gvWnd, index, null); } #endif }