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

相关推荐
沉默金鱼22 分钟前
Unity实用技能-UI进度条
ui·unity·游戏引擎
xixixiLucky42 分钟前
IDEA中MAVEN项目找依赖的快捷插件
java·maven·intellij-idea
嘻嘻嘻开心1 小时前
Java IO流
java·开发语言
JIngJaneIL1 小时前
基于java+ vue家庭理财管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
老华带你飞2 小时前
电商系统|基于java + vue电商系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
菠菠萝宝2 小时前
从传统后端到AI智能驱动:Java + AI 生态深度实战技术总结
java·人工智能·ai·llm·知识图谱·ai编程·rag
陈佳梁2 小时前
java--对象的引用
java·开发语言
wadesir2 小时前
Java实现遗传算法(从零开始掌握智能优化算法)
java·开发语言·算法
程序媛徐师姐2 小时前
Java基于SpringBoot的智能城市管理平台,附源码+文档说明
java·spring boot·java springboot·智能城市管理平台·java智能城市管理平台·java智能城市管理·智能城市管理
tryxr2 小时前
Java抽象类特点、使用方式和应用场景
java·开发语言·向上转型·抽象类·向下转型