├── .gitignore ├── Editor └── GameViewSizeHelper.cs ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | [Ll]ibrary/ 2 | [Tt]emp/ 3 | [Oo]bj/ 4 | 5 | # Autogenerated VS/MD solution and project files 6 | *.csproj 7 | *.unityproj 8 | *.sln 9 | ProjectSettings 10 | -------------------------------------------------------------------------------- /Editor/GameViewSizeHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using UnityEditor; 5 | using UnityEngine; 6 | 7 | namespace Kyusyukeigo.Helper 8 | { 9 | public class GameViewSizeHelper 10 | { 11 | #region public enum 12 | 13 | public enum GameViewSizeType 14 | { 15 | FixedResolution, 16 | AspectRatio 17 | } 18 | 19 | #endregion public enum 20 | 21 | #region private Fiald 22 | 23 | 24 | 25 | static BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance; 26 | 27 | #endregion private Fiald 28 | 29 | #region private Class 30 | 31 | private static GameViewSize _gameViewSize; 32 | public class GameViewSize 33 | { 34 | public GameViewSizeType type; 35 | public int width; 36 | public int height; 37 | public string baseText; 38 | } 39 | 40 | #endregion private Class 41 | 42 | #region public Method 43 | 44 | public static void AddCustomSize(GameViewSizeGroupType groupType, GameViewSize gameViewSize) 45 | { 46 | _gameViewSize = gameViewSize; 47 | object sizeType = Enum.Parse(Types.gameViewSizeType, gameViewSize.type.ToString()); 48 | 49 | ConstructorInfo ctor = Types.gameViewSize.GetConstructor(new Type[] 50 | { 51 | Types.gameViewSizeType, 52 | typeof(int), 53 | typeof(int), 54 | typeof(string) 55 | }); 56 | 57 | object instance_gameViewSize = ctor.Invoke(new object[] 58 | { 59 | sizeType, 60 | gameViewSize.width, 61 | gameViewSize.height, 62 | gameViewSize.baseText 63 | }); 64 | 65 | object instance_gameViewSizeGroup = GetGroup(groupType, instance); 66 | 67 | if (!Contains(instance_gameViewSizeGroup)) 68 | { 69 | AddCustomSize(instance_gameViewSizeGroup, instance_gameViewSize); 70 | } 71 | } 72 | 73 | public static void AddCustomSize(GameViewSizeGroupType groupType, GameViewSizeType type, int width, int height, string baseText) 74 | { 75 | AddCustomSize(groupType, new GameViewSize { type = type, width = width, height = height, baseText = baseText }); 76 | } 77 | 78 | public static bool RemoveCustomSize(GameViewSizeGroupType groupType, GameViewSizeType type, int width, int height, string baseText) 79 | { 80 | _gameViewSize = new GameViewSize { type = type, width = width, height = height, baseText = baseText }; 81 | return Remove(GetGroup(groupType, instance)); 82 | } 83 | public static bool RemoveCustomSize(GameViewSizeGroupType groupType, GameViewSize gameViewSize) 84 | { 85 | _gameViewSize = gameViewSize; 86 | return Remove(GetGroup(groupType, instance)); 87 | } 88 | public static bool Contains(GameViewSizeGroupType groupType, GameViewSizeType type, int width, int height, string baseText) 89 | { 90 | _gameViewSize = new GameViewSize { type = type, width = width, height = height, baseText = baseText }; 91 | return Contains(GetGroup(groupType, instance)); 92 | } 93 | public static bool Contains(GameViewSizeGroupType groupType, GameViewSize gameViewSize) 94 | { 95 | _gameViewSize = gameViewSize; 96 | return Contains(GetGroup(groupType, instance)); 97 | } 98 | 99 | 100 | public static void ChangeGameViewSize(GameViewSizeGroupType groupType, GameViewSizeType type, int width, int height, string baseText) 101 | { 102 | ChangeGameViewSize(groupType, new GameViewSize { type = type, width = width, height = height, baseText = baseText }); 103 | } 104 | 105 | public static void ChangeGameViewSize(GameViewSizeGroupType groupType, GameViewSize gameViewSize) 106 | { 107 | _gameViewSize = gameViewSize; 108 | EditorWindow gameView = EditorWindow.GetWindow(Types.gameView); 109 | PropertyInfo currentSizeGroupType = Types.gameView.GetProperty("currentSizeGroupType", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static); 110 | GameViewSizeGroupType currentType = (GameViewSizeGroupType)currentSizeGroupType.GetValue(gameView, null); 111 | if (groupType != currentType) 112 | { 113 | Debug.LogError(string.Format("GameViewSizeGroupType is {0}. but Current GameViewSizeGroupType is {1}.", groupType, currentType)); 114 | return; 115 | } 116 | object group = GetGroup(groupType, instance); 117 | int totalCount = GetTotalCount(group); 118 | int gameViewSizeLength = GetCustomCount(group); 119 | int index = -1; 120 | for (int i = totalCount - gameViewSizeLength; i < totalCount; i++) 121 | { 122 | object other_gameViewSize = GetGameViewSize(group, i); 123 | if (GameViewSize_Equals(_gameViewSize, other_gameViewSize)) 124 | { 125 | index = i; 126 | break; 127 | } 128 | } 129 | if (index != -1) 130 | { 131 | PropertyInfo selectedSizeIndex = Types.gameView.GetProperty("selectedSizeIndex", BindingFlags.Instance | BindingFlags.NonPublic); 132 | selectedSizeIndex.SetValue(gameView, index, null); 133 | } 134 | } 135 | 136 | #endregion public Method 137 | 138 | #region private Method 139 | 140 | static bool Remove(object instance_gameViewSizeGroup) 141 | { 142 | int gameViewSizeLength = GetCustomCount(instance_gameViewSizeGroup); 143 | int totalCount = GetTotalCount(instance_gameViewSizeGroup); 144 | for (int i = totalCount - gameViewSizeLength; i < totalCount; i++) 145 | { 146 | object other_gameViewSize = GetGameViewSize(instance_gameViewSizeGroup, i); 147 | if (GameViewSize_Equals(_gameViewSize, other_gameViewSize)) 148 | { 149 | RemoveCustomSize(instance_gameViewSizeGroup, i); 150 | return true; 151 | } 152 | } 153 | return false; 154 | } 155 | 156 | static bool Contains(object instance_gameViewSizeGroup) 157 | { 158 | int gameViewSizeLength = GetCustomCount(instance_gameViewSizeGroup); 159 | int totalCount = GetTotalCount(instance_gameViewSizeGroup); 160 | for (int i = totalCount - gameViewSizeLength; i < totalCount; i++) 161 | { 162 | if (GameViewSize_Equals(_gameViewSize, GetGameViewSize(instance_gameViewSizeGroup, i))) 163 | { 164 | return true; 165 | } 166 | } 167 | return false; 168 | } 169 | 170 | private static bool GameViewSize_Equals(GameViewSize a, object b) 171 | { 172 | int b_width = (int)GetGameSizeProperty(b, "width"); 173 | int b_height = (int)GetGameSizeProperty(b, "height"); 174 | string b_baseText = (string)GetGameSizeProperty(b, "baseText"); 175 | GameViewSizeType b_sizeType = (GameViewSizeType)Enum.Parse(typeof(GameViewSizeType), GetGameSizeProperty(b, "sizeType").ToString()); 176 | 177 | return a.type == b_sizeType && a.width == b_width && a.height == b_height && a.baseText == b_baseText; 178 | } 179 | 180 | static object GetGameSizeProperty(object instance, string name) 181 | { 182 | return instance.GetType().GetProperty(name).GetValue(instance, new object[0]); 183 | } 184 | 185 | static object m_instance; 186 | 187 | static object instance 188 | { 189 | get 190 | { 191 | if (m_instance == null) 192 | { 193 | PropertyInfo propertyInfo_gameViewSizes = Types.gameViewSizes.GetProperty("instance"); 194 | m_instance = propertyInfo_gameViewSizes.GetValue(null, new object[0]); 195 | } 196 | return m_instance; 197 | } 198 | } 199 | 200 | static object GetGroup(GameViewSizeGroupType groupType, object instance_gameViewSizes) 201 | { 202 | Type[] returnTypes = new Type[] { groupType.GetType() }; 203 | object[] parameters = new object[] { groupType }; 204 | return instance_gameViewSizes.GetType().GetMethod("GetGroup", 205 | bindingFlags, 206 | null, 207 | returnTypes, 208 | null).Invoke(instance_gameViewSizes, parameters); 209 | } 210 | 211 | static object GetGameViewSize(object instance_gameViewSizeGroup, int i) 212 | { 213 | Type[] returnTypes = new Type[] { typeof(int) }; 214 | object[] parameters = new object[] { i }; 215 | return instance_gameViewSizeGroup.GetType().GetMethod("GetGameViewSize", 216 | bindingFlags, 217 | null, 218 | returnTypes, 219 | null).Invoke(instance_gameViewSizeGroup, parameters); 220 | } 221 | 222 | static int GetCustomCount(object instance_gameViewSizeGroup) 223 | { 224 | return (int)instance_gameViewSizeGroup.GetType().GetMethod("GetCustomCount", 225 | bindingFlags, 226 | null, 227 | new Type[0], 228 | null).Invoke(instance_gameViewSizeGroup, new object[0]); 229 | } 230 | 231 | static int GetTotalCount(object instance_gameViewSizeGroup) 232 | { 233 | return (int)instance_gameViewSizeGroup.GetType().GetMethod("GetTotalCount", 234 | bindingFlags, 235 | null, 236 | new Type[0], 237 | null).Invoke(instance_gameViewSizeGroup, new object[0]); 238 | } 239 | 240 | static void AddCustomSize(object instance_gameViewSizeGroup, object instance_gameViewSize) 241 | { 242 | Type[] returnTypes = new Type[] { Types.gameViewSize }; 243 | object[] parameters = new object[] { instance_gameViewSize }; 244 | instance_gameViewSizeGroup.GetType().GetMethod("AddCustomSize", 245 | bindingFlags, 246 | null, 247 | returnTypes, 248 | null).Invoke(instance_gameViewSizeGroup, parameters); 249 | } 250 | 251 | static void RemoveCustomSize(object instance_gameViewSizeGroup, int index) 252 | { 253 | Type[] returnTypes = new Type[] { typeof(int) }; 254 | object[] parameters = new object[] { index }; 255 | instance_gameViewSizeGroup.GetType().GetMethod("RemoveCustomSize", 256 | bindingFlags, 257 | null, 258 | returnTypes, 259 | null).Invoke(instance_gameViewSizeGroup, parameters); 260 | } 261 | 262 | #endregion private Method 263 | 264 | #region types 265 | 266 | private static class Types 267 | { 268 | private static string assemblyName = "UnityEditor.dll"; 269 | private static Assembly assembly = Assembly.Load(assemblyName); 270 | 271 | public static Type gameView = assembly.GetType("UnityEditor.GameView"); 272 | 273 | public static Type gameViewSizeType = assembly.GetType("UnityEditor.GameViewSizeType"); 274 | public static Type gameViewSize = assembly.GetType("UnityEditor.GameViewSize"); 275 | public static Type gameViewSizes = assembly.GetType("UnityEditor.ScriptableSingleton`1").MakeGenericType(assembly.GetType("UnityEditor.GameViewSizes")); 276 | } 277 | 278 | #endregion types 279 | 280 | } 281 | } 282 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 kyusyukeigo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | GameViewSizeHelper 2 | ================== 3 | 4 | ScriptからGameViewSizeを作成、また設定するヘルパークラス 5 | 6 | ## API 7 | 8 | 9 | 10 | ### AddCustomSize 11 | 12 | GameViewサイズを追加します 13 | 14 | ```cs 15 | public static void AddCustomSize (GameViewSizeGroupType groupType, GameViewSize gameViewSize) 16 | public static void AddCustomSize (GameViewSizeGroupType groupType, GameViewSizeType type, int width, int height, string baseText) 17 | ``` 18 | 19 | 名前|説明 20 | :---|:--- 21 | groupType|追加したいプラットフォーム 22 | gameViewSize|サイズやアスペクト比など必要な情報を格納したGameViewSizeオブジェクト 23 | type|アスペクト比かピクセルサイズか 24 | width|幅 25 | height|高さ 26 | baseText|この設定の名前 27 | 28 | ### RemoveCustomSize 29 | 30 | GameViewサイズを削除します 31 | 32 | ```cs 33 | public static bool RemoveCustomSize (GameViewSizeGroupType groupType, GameViewSize gameViewSize) 34 | public static bool RemoveCustomSize (GameViewSizeGroupType groupType, GameViewSizeType type, int width, int height, string baseText) 35 | ``` 36 | 37 | ### Contains 38 | 39 | 設定しようとしているGameViewサイズが既に設定済みか確認します 40 | 41 | ```cs 42 | public static bool Contains (GameViewSizeGroupType groupType, GameViewSize gameViewSize) 43 | public static bool Contains (GameViewSizeGroupType groupType, GameViewSizeType type, int width, int height, string baseText) 44 | ``` 45 | 46 | ### ChangeGameViewSize 47 | 48 | 指定のGameViewサイズに変更します
49 | 必ずUnityEditorをgroupTypeと同じプラットフォームに指定しておかなければいけません 50 | 51 | ```cs 52 | public static void ChangeGameViewSize (GameViewSizeGroupType groupType, GameViewSize gameViewSize) 53 | public static void ChangeGameViewSize (GameViewSizeGroupType groupType, GameViewSizeType type, int width, int height, string baseText) 54 | ``` 55 | 56 | ## LICENSE 57 | 58 | [MIT](https://github.com/anchan828/unity-GameViewSizeHelper/blob/master/LICENSE) 59 | --------------------------------------------------------------------------------