反射UnityEditor.GameView设置GamePlayMode分辨率

2024-01-07 05:52

本文主要是介绍反射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 });
    }
}
 

这篇关于反射UnityEditor.GameView设置GamePlayMode分辨率的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/578919

相关文章

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

linux hostname设置全过程

《linuxhostname设置全过程》:本文主要介绍linuxhostname设置全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录查询hostname设置步骤其它相关点hostid/etc/hostsEDChina编程A工具license破解注意事项总结以RHE

Python设置Cookie永不超时的详细指南

《Python设置Cookie永不超时的详细指南》Cookie是一种存储在用户浏览器中的小型数据片段,用于记录用户的登录状态、偏好设置等信息,下面小编就来和大家详细讲讲Python如何设置Cookie... 目录一、Cookie的作用与重要性二、Cookie过期的原因三、实现Cookie永不超时的方法(一)

Qt 设置软件版本信息的实现

《Qt设置软件版本信息的实现》本文介绍了Qt项目中设置版本信息的三种常用方法,包括.pro文件和version.rc配置、CMakeLists.txt与version.h.in结合,具有一定的参考... 目录在运行程序期间设置版本信息可以参考VS在 QT 中设置软件版本信息的几种方法方法一:通过 .pro

PostgreSQL 默认隔离级别的设置

《PostgreSQL默认隔离级别的设置》PostgreSQL的默认事务隔离级别是读已提交,这是其事务处理系统的基础行为模式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录一 默认隔离级别概述1.1 默认设置1.2 各版本一致性二 读已提交的特性2.1 行为特征2.2

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

mtu设置多少网速最快? 路由器MTU设置最佳网速的技巧

《mtu设置多少网速最快?路由器MTU设置最佳网速的技巧》mtu设置多少网速最快?想要通过设置路由器mtu获得最佳网速,该怎么设置呢?下面我们就来看看路由器MTU设置最佳网速的技巧... 答:1500 MTU值指的是在网络传输中数据包的最大值,合理的设置MTU 值可以让网络更快!mtu设置可以优化不同的网

MySQL 设置AUTO_INCREMENT 无效的问题解决

《MySQL设置AUTO_INCREMENT无效的问题解决》本文主要介绍了MySQL设置AUTO_INCREMENT无效的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录快速设置mysql的auto_increment参数一、修改 AUTO_INCREMENT 的值。

详解Linux中常见环境变量的特点与设置

《详解Linux中常见环境变量的特点与设置》环境变量是操作系统和用户设置的一些动态键值对,为运行的程序提供配置信息,理解环境变量对于系统管理、软件开发都很重要,下面小编就为大家详细介绍一下吧... 目录前言一、环境变量的概念二、常见的环境变量三、环境变量特点及其相关指令3.1 环境变量的全局性3.2、环境变

安装centos8设置基础软件仓库时出错的解决方案

《安装centos8设置基础软件仓库时出错的解决方案》:本文主要介绍安装centos8设置基础软件仓库时出错的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录安装Centos8设置基础软件仓库时出错版本 8版本 8.2.200android4版本 javas