|
| 1 | +#if UNITY_EDITOR |
| 2 | +using System; |
| 3 | +using System.Reflection; |
| 4 | +using UnityEditor; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Found in https://discussions.unity.com/t/how-to-change-game-window-resoltuion-width-height-in-editor-mode-programmatically/861042/3 |
| 8 | +/// </summary> |
| 9 | +public static class GameViewUtils |
| 10 | +{ |
| 11 | + static object gameViewSizesInstance; |
| 12 | + static MethodInfo getGroup; |
| 13 | + |
| 14 | + static GameViewUtils() |
| 15 | + { |
| 16 | + // gameViewSizesInstance = ScriptableSingleton<GameViewSizes>.instance; |
| 17 | + var sizesType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizes"); |
| 18 | + var singleType = typeof(ScriptableSingleton<>).MakeGenericType(sizesType); |
| 19 | + var instanceProp = singleType.GetProperty("instance"); |
| 20 | + getGroup = sizesType.GetMethod("GetGroup"); |
| 21 | + gameViewSizesInstance = instanceProp.GetValue(null, null); |
| 22 | + } |
| 23 | + |
| 24 | + public enum GameViewSizeType |
| 25 | + { |
| 26 | + AspectRatio, FixedResolution |
| 27 | + } |
| 28 | + |
| 29 | + public static void SetSize(int index) |
| 30 | + { |
| 31 | + var gvWndType = typeof(Editor).Assembly.GetType("UnityEditor.GameView"); |
| 32 | + var selectedSizeIndexProp = gvWndType.GetProperty("selectedSizeIndex", |
| 33 | + BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); |
| 34 | + var gvWnd = EditorWindow.GetWindow(gvWndType); |
| 35 | + selectedSizeIndexProp.SetValue(gvWnd, index, null); |
| 36 | + } |
| 37 | + |
| 38 | + public static void AddAndSelectCustomSize(GameViewSizeType viewSizeType, GameViewSizeGroupType sizeGroupType, int width, int height, string text) |
| 39 | + { |
| 40 | + int idx = FindSize(GameViewSizeGroupType.Standalone, width, height); |
| 41 | + |
| 42 | + // Add if doesn't exists. |
| 43 | + if (idx == -1) |
| 44 | + { |
| 45 | + AddCustomSize(viewSizeType, sizeGroupType, width, height, text); |
| 46 | + idx = FindSize(GameViewSizeGroupType.Standalone, width, height); |
| 47 | + } |
| 48 | + |
| 49 | + SetSize(idx); |
| 50 | + } |
| 51 | + |
| 52 | + public static void AddCustomSize(GameViewSizeType viewSizeType, GameViewSizeGroupType sizeGroupType, int width, int height, string text) |
| 53 | + { |
| 54 | + var group = GetGroup(sizeGroupType); |
| 55 | + var addCustomSize = getGroup.ReturnType.GetMethod("AddCustomSize"); // or group.GetType(). |
| 56 | + var gvsType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSize"); |
| 57 | + string assemblyName = "UnityEditor.dll"; |
| 58 | + Assembly assembly = Assembly.Load(assemblyName); |
| 59 | + Type gameViewSize = assembly.GetType("UnityEditor.GameViewSize"); |
| 60 | + Type gameViewSizeType = assembly.GetType("UnityEditor.GameViewSizeType"); |
| 61 | + ConstructorInfo ctor = gameViewSize.GetConstructor(new Type[] |
| 62 | + { |
| 63 | + gameViewSizeType, |
| 64 | + typeof(int), |
| 65 | + typeof(int), |
| 66 | + typeof(string) |
| 67 | + }); |
| 68 | + var newSize = ctor.Invoke(new object[] { (int)viewSizeType, width, height, text }); |
| 69 | + addCustomSize.Invoke(group, new object[] { newSize }); |
| 70 | + } |
| 71 | + |
| 72 | + public static bool SizeExists(GameViewSizeGroupType sizeGroupType, string text) |
| 73 | + { |
| 74 | + return FindSize(sizeGroupType, text) != -1; |
| 75 | + } |
| 76 | + |
| 77 | + public static int FindSize(GameViewSizeGroupType sizeGroupType, string text) |
| 78 | + { |
| 79 | + // GameViewSizes group = gameViewSizesInstance.GetGroup(sizeGroupType); |
| 80 | + // string[] texts = group.GetDisplayTexts(); |
| 81 | + // for loop... |
| 82 | + |
| 83 | + var group = GetGroup(sizeGroupType); |
| 84 | + var getDisplayTexts = group.GetType().GetMethod("GetDisplayTexts"); |
| 85 | + var displayTexts = getDisplayTexts.Invoke(group, null) as string[]; |
| 86 | + for (int i = 0; i < displayTexts.Length; i++) |
| 87 | + { |
| 88 | + string display = displayTexts[i]; |
| 89 | + // the text we get is "Name (W:H)" if the size has a name, or just "W:H" e.g. 16:9 |
| 90 | + // so if we're querying a custom size text we substring to only get the name |
| 91 | + // You could see the outputs by just logging |
| 92 | + // Debug.Log(display); |
| 93 | + int pren = display.IndexOf('('); |
| 94 | + if (pren != -1) |
| 95 | + display = display.Substring(0, pren - 1); // -1 to remove the space that's before the prens. This is very implementation-depdenent |
| 96 | + if (display == text) |
| 97 | + return i; |
| 98 | + } |
| 99 | + return -1; |
| 100 | + } |
| 101 | + |
| 102 | + public static bool SizeExists(GameViewSizeGroupType sizeGroupType, int width, int height) |
| 103 | + { |
| 104 | + return FindSize(sizeGroupType, width, height) != -1; |
| 105 | + } |
| 106 | + |
| 107 | + public static int FindSize(GameViewSizeGroupType sizeGroupType, int width, int height) |
| 108 | + { |
| 109 | + // goal: |
| 110 | + // GameViewSizes group = gameViewSizesInstance.GetGroup(sizeGroupType); |
| 111 | + // int sizesCount = group.GetBuiltinCount() + group.GetCustomCount(); |
| 112 | + // iterate through the sizes via group.GetGameViewSize(int index) |
| 113 | + |
| 114 | + var group = GetGroup(sizeGroupType); |
| 115 | + var groupType = group.GetType(); |
| 116 | + var getBuiltinCount = groupType.GetMethod("GetBuiltinCount"); |
| 117 | + var getCustomCount = groupType.GetMethod("GetCustomCount"); |
| 118 | + int sizesCount = (int)getBuiltinCount.Invoke(group, null) + (int)getCustomCount.Invoke(group, null); |
| 119 | + var getGameViewSize = groupType.GetMethod("GetGameViewSize"); |
| 120 | + var gvsType = getGameViewSize.ReturnType; |
| 121 | + var widthProp = gvsType.GetProperty("width"); |
| 122 | + var heightProp = gvsType.GetProperty("height"); |
| 123 | + var indexValue = new object[1]; |
| 124 | + for (int i = 0; i < sizesCount; i++) |
| 125 | + { |
| 126 | + indexValue[0] = i; |
| 127 | + var size = getGameViewSize.Invoke(group, indexValue); |
| 128 | + int sizeWidth = (int)widthProp.GetValue(size, null); |
| 129 | + int sizeHeight = (int)heightProp.GetValue(size, null); |
| 130 | + if (sizeWidth == width && sizeHeight == height) |
| 131 | + return i; |
| 132 | + } |
| 133 | + return -1; |
| 134 | + } |
| 135 | + |
| 136 | + static object GetGroup(GameViewSizeGroupType type) |
| 137 | + { |
| 138 | + return getGroup.Invoke(gameViewSizesInstance, new object[] { (int)type }); |
| 139 | + } |
| 140 | + |
| 141 | + public static GameViewSizeGroupType GetCurrentGroupType() |
| 142 | + { |
| 143 | + var getCurrentGroupTypeProp = gameViewSizesInstance.GetType().GetProperty("currentGroupType"); |
| 144 | + return (GameViewSizeGroupType)(int)getCurrentGroupTypeProp.GetValue(gameViewSizesInstance, null); |
| 145 | + } |
| 146 | +} |
| 147 | +#endif |
0 commit comments