Unity技术分享之Unity动态添加和选择游戏视图分辨率

在工作中经常遇到移动端适配不同屏幕,需要在在不同分辨率下预览显示效果 , 在game视图下手动切换实在是麻烦 , 如果切换的需求频率不高还好 , 我就在想有没有可以使用快捷键就可以实现快速切换game视图中分辨率设置呢? 最终在Unity官方论坛找到了答案 , 就是通过反射机制 , 调用Unity内部的方法 , 这些个方法名称都可以通过反编译看到 , 废话不多说 , 下面上代码 .


文章来源

可以自定义快捷键实现快速切换game视图的分辨率 ↓
public class GameViewUtils{
             static object gameViewSizesInstance;
             static MethodInfo getGroup;
             private static int screenIndex = 16; // Because have 16 indexes in my list.
             private static int gameViewProfilesCount;
         
             static GameViewUtils()
             {
                 // gameViewSizesInstance  = ScriptableSingleton.instance;
                 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);
             }
         
             private enum GameViewSizeType
             {
                 AspectRatio, FixedResolution
             }
         
             private static void SetSize(int index)
             {
                 var gvWndType = typeof(Editor).Assembly.GetType("UnityEditor.GameView");
                 var gvWnd = EditorWindow.GetWindow(gvWndType);
                 var SizeSelectionCallback = gvWndType.GetMethod("SizeSelectionCallback",
                     BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                 SizeSelectionCallback.Invoke(gvWnd, new object[] { index, null });
             }
         
             static object GetGroup(GameViewSizeGroupType type)
             {
                 return getGroup.Invoke(gameViewSizesInstance, new object[] { (int)type });
             }
         
             [MenuItem("Tools/GameViewSize/Previous %&Q")]
             private static void SetPrevious()
             {
                 GetViewListSize();
                 if (screenIndex - 1 >= 16)
                 {
                     screenIndex -= 1;
                 }
                 else
                 {
                     screenIndex = gameViewProfilesCount - 1;
                 }
         
                 SetSize(screenIndex);
             }
         
             [MenuItem("Tools/GameViewSize/Next  %&E")]
             private static void SetNext()
             {
                 GetViewListSize();
                 if (screenIndex + 1 < gameViewProfilesCount)
                 {
                     screenIndex += 1;
                 }
                 else
                 {
                     screenIndex = 16;
                 }
         
                 SetSize(screenIndex);
             }
         
             private static void GetViewListSize()
             {
                 var group = GetGroup(GameViewSizeGroupType.Android);
                 var getDisplayTexts = group.GetType().GetMethod("GetDisplayTexts");
                 gameViewProfilesCount = (getDisplayTexts.Invoke(group, null) as string[]).Length;
             }
         }

你可能感兴趣的:(Unity技术分享)