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

相关推荐
小兔崽子去哪了1 小时前
Java 自动化部署
java·后端
ma_king1 小时前
入门 java 和 数据库
java·数据库·后端
后端AI实验室1 小时前
我用Cursor开发了3个月,整理出这套提效4倍的工作流
java·ai
码路飞6 小时前
GPT-5.3 Instant 终于学会好好说话了,顺手对比了下同天发布的 Gemini 3.1 Flash-Lite
java·javascript
SimonKing6 小时前
OpenCode AI编程助手如何添加Skills,优化项目!
java·后端·程序员
Seven978 小时前
剑指offer-80、⼆叉树中和为某⼀值的路径(二)
java
怒放吧德德19 小时前
Netty 4.2 入门指南:从概念到第一个程序
java·后端·netty
雨中飘荡的记忆21 小时前
大流量下库存扣减的数据库瓶颈:Redis分片缓存解决方案
java·redis·后端
心之语歌1 天前
基于注解+拦截器的API动态路由实现方案
java·后端
华仔啊1 天前
Stream 代码越写越难看?JDFrame 让 Java 逻辑回归优雅
java·后端