Unity Runtime控制编辑器的一些操作

运行时修改Game窗口尺寸

cs 复制代码
//设置竖屏
public void ChangePortrait()
{
    Assembly assembly = typeof(UnityEditor.EditorWindow).Assembly;
    Type type = assembly.GetType("UnityEditor.GameView");
    var gameView= UnityEditor.EditorWindow.GetWindow(type);
    //可以反射出其他字段和方法,看看是否有可以修改Game视图字段或者方法   
    PropertyInfo targetProperty = type.GetProperty("selectedSizeIndex");
    targetProperty.SetValue(gameView,18);
    UIModule.Instance.UIRoot.GetComponent<CanvasScaler>().referenceResolution = new Vector2(768, 1720);
}

//设置横屏
public void ChangeLandscape()
{
    Assembly assembly = typeof(UnityEditor.EditorWindow).Assembly;
    Type type = assembly.GetType("UnityEditor.GameView");
    var gameView= UnityEditor.EditorWindow.GetWindow(type);
    //可以反射出其他字段和方法,看看是否有可以修改Game视图字段或者方法   
    PropertyInfo targetProperty = type.GetProperty("selectedSizeIndex");
    targetProperty.SetValue(gameView, 19);
    UIModule.Instance.UIRoot.GetComponent<CanvasScaler>().referenceResolution = new Vector2(1920, 1080);
}

请注意selectedSizeIndex属性修改的是下图的尺寸编号

相关推荐
on the way 1238 分钟前
day10 - Spring 之配置类源码解析
java·后端·spring
行者-全栈开发13 分钟前
JDK 17 + Spring Boot 3.5.8:企业级开发技术栈全景
java·开发语言·spring boot·系统架构·技术栈·系统架构全景分析·springboot技术栈
WHS-_-202214 分钟前
mCore: Achieving Sub-millisecond Scheduling for 5G MU-MIMO Systems
java·算法·5g
Jin、yz15 分钟前
黑马苍穹外卖项目收获
java
#空城22 分钟前
VSCode搭建LVGL9的模拟器
ide·vscode·编辑器
panzer_maus23 分钟前
Java多线程介绍
java·开发语言
AMoon丶28 分钟前
Golang--多种控制结构详解
java·linux·c语言·开发语言·后端·青少年编程·golang
indexsunny29 分钟前
互联网大厂Java面试实战:微服务与Spring Boot在电商场景下的应用解析
java·spring boot·redis·docker·微服务·kubernetes·oauth2
薛定谔之死猫30 分钟前
Ruby简单粗暴把图片合成PDF文档
java·pdf·ruby
moxiaoran575331 分钟前
Spring Bean线程安全性分析
java·spring