反射UnityEditor.GameView设置GamePlayMode分辨率

现在很有游戏考虑横屏适配、竖屏适配、阿拉伯语适配(横竖屏)导致拼界面变得越来越繁琐。

有很多时候需要记录各个控件的状态。

为了减少操作,特意制作了这个工具,点击用x配置可以自动切换到 宽高分辨率,如果当前没有则会自动添加到编辑器中。这里特别感谢开源社区https://github.com/akof1314/Unity-EditorInternalsVisibleDemo/blob/main/EditorInternalsVisibleDemo/PackagesCustom/com.wuhuan.internalsvisibledemo/Editor/PickViewSize/PickGameViewSizeWindow.csusing System;

using System.Reflection;
using UnityEditor;
using UnityEngine;

public class GameViewTools
{
    public enum GameViewSizeType
    {
        AspectRatio,
        FixedResolution
    }
    public static int devWidth = 1624;
    public static int devHeight = 750;
    private static object gameViewSizesInstance; // GameViewSizes的引用
    private static bool isInited = false;
    public static int  landScapeIndex = 1;
    public static int portraitIndex = 1;
    private static MethodInfo getGroup; 
    public static object curGroup;
    public static MethodInfo curGroupFunc;
    public static MethodInfo getGameViewSizeFunc; // 获取索引x的分辨率
    private static MethodInfo addCustomSize; //添加方法
    private static MethodInfo removeCustomSize; //添加方法
    private static MethodInfo getCustomCount; //引用获取当前自定义数量
    private static MethodInfo getTotakCount; //获取所以的数量
    private static MethodInfo getBuiltinCount; //获取基础数量

    public static bool IsHorizonal()
    {
        var vec = GameViewSize();
        return vec.x > vec.y;
    }
    public  static Vector2 GameViewSize()
    {
        var mouseOverWindow = UnityEditor.EditorWindow.mouseOverWindow;
        System.Reflection.Assembly assembly = typeof(UnityEditor.EditorWindow).Assembly;
        System.Type type = assembly.GetType("UnityEditor.PlayModeView");

        Vector2 size = (Vector2)type.GetMethod(
            "GetMainPlayModeViewTargetSize",
            System.Reflection.BindingFlags.NonPublic |
            System.Reflection.BindingFlags.Static
        ).Invoke(mouseOverWindow, null);

        return size;
    }

public static void ChangeResolution(int index)
{
    var type = typeof(Editor).Assembly.GetType("UnityEditor.GameView");
    var window = EditorWindow.GetWindow(type);

      

    var SizeSelectionCallback = type.GetMethod("SizeSelectionCallback",
        System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
    SizeSelectionCallback.Invoke(window, new object[] { index, null });
}

public static void InitiaLized()
{
    if (isInited) return;
    isInited = true;
    var gvSize = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizes");
    var singleType = typeof(ScriptableSingleton<>).MakeGenericType(gvSize);
    var instacnceProp = singleType.GetProperty("instance");
    getGroup = gvSize.GetMethod("GetGroup");
    curGroupFunc = gvSize.GetMethod("get_currentGroup");
    curGroup = curGroupFunc.Invoke(instacnceProp.GetValue(null, null), null);       
    gameViewSizesInstance = instacnceProp.GetValue(null, null); //获取单例
    getGameViewSizeFunc = curGroup.GetType().GetMethod("GetGameViewSize");
    getCustomCount = getGroup.ReturnType.GetMethod("GetCustomCount");
    getTotakCount = getGroup.ReturnType.GetMethod("GetTotalCount");
    getBuiltinCount = getGroup.ReturnType.GetMethod("GetBuiltinCount");
    addCustomSize = getGroup.ReturnType.GetMethod("AddCustomSize");
    removeCustomSize = getGroup.ReturnType.GetMethod("RemoveCustomSize");
    CheckCustomSizeEveryOnce();
}
public static void CheckCustomSizeEveryOnce()
{
    var buildInCount = (int)getBuiltinCount.Invoke(curGroup, null);//内建分辨率数量
    int count = (int)getCustomCount.Invoke(curGroup, null);  //自定义分辨率数量
    int totalCount = (int)getTotakCount.Invoke(curGroup, null); //累计分辨率数量 
    landScapeIndex = 0;
    portraitIndex = 0;
    for (int i = buildInCount; (landScapeIndex == 0 || portraitIndex == 0) && i < totalCount; i++)
    {
        var viewSize = getGameViewSizeFunc.Invoke(curGroup, new object[] { i });
        var w = (int)viewSize.GetType().GetProperty("width").GetValue(viewSize, null);
        var h = (int)viewSize.GetType().GetProperty("height").GetValue(viewSize, null);
        if (w == devWidth && h == devHeight)
        {
            landScapeIndex = i;
        }
        else if (w == devHeight && h == devWidth)
        {
            portraitIndex = i;
        }
    }
    if (landScapeIndex == 0 || portraitIndex == 0)
    {
        AddCustomSize(GameViewSizeType.FixedResolution, GameViewSizeGroupType.Android, devWidth, devHeight, "(code added)");
        AddCustomSize(GameViewSizeType.FixedResolution, GameViewSizeGroupType.Android, devHeight, devWidth, "(code added)");
    }
}
    public static void AddCustomSize(GameViewSizeType viewSizeType, GameViewSizeGroupType gameViewSizeGroupType,
        int width, int height, string text)
    {
           
        //获取类GameViewSize
        var gvsType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSize");

        //找到 构造方法 public GameViewSize(GameViewSizeType type, int width, int height, string baseText)
        var ctor = gvsType.GetConstructor(new Type[]
        {
            typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizeType"),
            typeof(int),
            typeof(int),
            typeof(string)
        });

        //找到enum 类型 GameViewSizeType
        var newGvsType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizeType");

        int enumGvsType = 0;

        // //获取enum中的类型
        if (viewSizeType == GameViewSizeType.AspectRatio)
        {
            var aspectRatio = newGvsType.GetField("AspectRatio", BindingFlags.Static | BindingFlags.Public);
            //newGvsType =aspectRatio.GetType() ; //typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizeType.AspectRatio");
            enumGvsType = (int)aspectRatio.GetValue(null);
        }
        else
        {
            var fixedResolution = newGvsType.GetField("FixedResolution", BindingFlags.Static | BindingFlags.Public);
            // newGvsType = fixedResolution.GetType(); //typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizeType.FixedResolution");
            enumGvsType = (int)fixedResolution.GetValue(null);
        }

        var newSize = ctor.Invoke(new object[] { enumGvsType, width, height, text });


        // //执行 添加到 group 中 GameViewSizeType m_SizeType;
        var sizetype = gvsType.GetField("m_SizeType", BindingFlags.NonPublic | BindingFlags.Instance);

        //获取 GameViewSizeGroup
        var gameViewSizes = getGroup.Invoke(gameViewSizesInstance, new object[] { (int)gameViewSizeGroupType });

        addCustomSize.Invoke(gameViewSizes, new object[] { newSize });
    }
}
 

  • 21
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值