反射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 Calendar设置上个月时,日期不存在或错误提示问题及解决

《JAVACalendar设置上个月时,日期不存在或错误提示问题及解决》在使用Java的Calendar类设置上个月的日期时,如果遇到不存在的日期(如4月31日),默认会自动调整到下个月的相应日期(... 目录Java Calendar设置上个月时,日期不存在或错误提示java进行日期计算时如果出现不存在的

Java利用Spire.XLS for Java自动化设置Excel的文档属性

《Java利用Spire.XLSforJava自动化设置Excel的文档属性》一个专业的Excel文件,其文档属性往往能大大提升文件的可管理性和可检索性,下面我们就来看看Java如何使用Spire... 目录Spire.XLS for Java 库介绍与安装Java 设置内置的 Excel 文档属性Java

idea设置快捷键风格方式

《idea设置快捷键风格方式》在IntelliJIDEA中设置快捷键风格,打开IDEA,进入设置页面,选择Keymap,从Keymaps下拉列表中选择或复制想要的快捷键风格,点击Apply和OK即可使... 目录idea设www.chinasem.cn置快捷键风格按照以下步骤进行总结idea设置快捷键pyth

MyBatis配置文件中最常用的设置

《MyBatis配置文件中最常用的设置》文章主要介绍了MyBatis配置的优化方法,包括引用外部的properties配置文件、配置外置以实现环境解耦、配置文件中最常用的6个核心设置以及三种常用的Ma... 目录MyBATis配置优化mybatis的配置中引用外部的propertis配置文件⚠️ 注意事项X

Java JAR 启动内存参数配置指南(从基础设置到性能优化)

《JavaJAR启动内存参数配置指南(从基础设置到性能优化)》在启动Java可执行JAR文件时,合理配置JVM内存参数是保障应用稳定性和性能的关键,本文将系统讲解如何通过命令行参数、环境变量等方式... 目录一、核心内存参数详解1.1 堆内存配置1.2 元空间配置(MetASPace)1.3 线程栈配置1.

C#借助Spire.XLS for .NET实现Excel工作表自动化样式设置

《C#借助Spire.XLSfor.NET实现Excel工作表自动化样式设置》作为C#开发者,我们经常需要处理Excel文件,本文将深入探讨如何利用C#代码,借助强大的Spire.XLSfor.N... 目录为什么需要自动化工作表样式使用 Spire.XLS for .NET 实现工作表整体样式设置样式配置

Java实现为PDF设置背景色和背景图片

《Java实现为PDF设置背景色和背景图片》在日常的文档处理中,PDF格式因其稳定性和跨平台兼容性而广受欢迎,本文将深入探讨如何利用Spire.PDFforJava库,以简洁高效的方式为你的PDF文档... 目录库介绍与安装步骤Java 给 PDF 设置背景颜色Java 给 PDF 设置背景图片总结在日常的

C#中通过Response.Headers设置自定义参数的代码示例

《C#中通过Response.Headers设置自定义参数的代码示例》:本文主要介绍C#中通过Response.Headers设置自定义响应头的方法,涵盖基础添加、安全校验、生产实践及调试技巧,强... 目录一、基础设置方法1. 直接添加自定义头2. 批量设置模式二、高级配置技巧1. 安全校验机制2. 类型

python库pydantic数据验证和设置管理库的用途

《python库pydantic数据验证和设置管理库的用途》pydantic是一个用于数据验证和设置管理的Python库,它主要利用Python类型注解来定义数据模型的结构和验证规则,本文给大家介绍p... 目录主要特点和用途:Field数值验证参数总结pydantic 是一个让你能够 confidentl

Java利用Spire.XLS for Java设置Excel表格边框

《Java利用Spire.XLSforJava设置Excel表格边框》在日常的业务报表和数据处理中,Excel表格的美观性和可读性至关重要,本文将深入探讨如何利用Spire.XLSforJava库... 目录Spire.XLS for Java 简介与安装Maven 依赖配置手动安装 JAR 包核心API介