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属性修改的是下图的尺寸编号

相关推荐
luanma150980几秒前
Spring 框架——@Retryable 注解与 @Recover 注解
java·前端·spring
阿Y加油吧1 分钟前
力扣打卡——day01
java·算法·leetcode
码路飞5 分钟前
Java 25 发了但更让我兴奋的是这个:Spring AI 让 Java 调大模型终于不用手写 HTTP 了
java·人工智能·spring
sinat_2554878113 分钟前
transient 修饰符·学习笔记
java·开发语言·spring
jwn99913 分钟前
SQL Server2019下载及安装教程
java
虚拟世界AI28 分钟前
Java服务器开发:零基础实战指南
java·servlet·tomcat
码界奇点1 小时前
基于模块化架构的Unity游戏开发框架设计与实现
java·c++·unity·架构·毕业设计·源代码管理
后端AI实验室1 小时前
同一个需求,我先出技术方案,再让AI出方案——差距让我沉默了
java·ai
xyyaihxl1 小时前
springboot与springcloud对应版本
java·spring boot·spring cloud
爱滑雪的码农1 小时前
Java基础五:运算符与循环结构
java·开发语言