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

相关推荐
EatFan16 分钟前
Spring Boot 4.0 升级清单:JDK 基线、Jakarta 命名空间、Observation API 与第三方组件兼容一次讲清
java·spring boot·后端
辰海Coding42 分钟前
平台如何接入一台物联网设备?又该如何设计它的通信协议?
java·物联网
代码民工正电荷1 小时前
JAVA重要知识点和集合
java·开发语言
李少兄2 小时前
IDEA Apifox Helper 插件踩坑:2.0.15-243 版本“导出指定接口”参数描述丢失问题解析与修复方案
java·ide·intellij-idea
努力攀登的小k3 小时前
《新手练表:从建表到删表全操作|从零学会表操作》
java·数据库·mysql
Yetian_20003 小时前
Postman测试上传图片文件接口
java·postman
爪哇碎碎念4 小时前
【Java】从一次 cache.get(key) 进去:缓存击穿一瞬间打出 200 次 DB,JDK 17 实测三种解法
java·redis·缓存
csdn_aspnet4 小时前
Java 使总距离最短的最佳点位置(Optimum location of point to minimize total distance)
java·算法
源码_空间4 小时前
基于Springboot的在线编程练习判题平台(源码+文档)
java·spring boot·后端
小手cool5 小时前
二分查找:在有序数组中查找最接近 target 的元素索引
java·数据结构·算法