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

相关推荐
Memory_荒年8 分钟前
自定义 Spring Boot Starter:手搓“轮子”,但要搓出兰博基尼!
java·后端
栈外20 分钟前
我是IDEA重度用户,试了4款AI编程插件:有一款有并发Bug,有一款越用越香
java·后端
架构师沉默29 分钟前
为什么说 Go 做游戏服务器就有人皱眉?
java·后端·架构
a56299161931 分钟前
【springboot】Spring 官方抛弃了 Java 8!新idea如何创建java8项目
java·spring boot·spring
秃了也弱了。40 分钟前
ElasticSearch:优化案例实战解析(持续更新)
android·java·elasticsearch
一叶落4381 小时前
LeetCode 54. 螺旋矩阵(C语言详解)——模拟 + 四边界收缩
java·c语言·数据结构·算法·leetcode·矩阵
最初的↘那颗心1 小时前
Prompt 工程实战:五要素框架与 Spring AI 模板化落地
java·大模型·prompt工程·spring ai·ai应用开发
东离与糖宝2 小时前
Java 21 虚拟线程与 AI 推理结合的最新实践
java·人工智能
菜鸟小九3 小时前
hot100(71-80)
java·数据结构·算法