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

相关推荐
IKUN家族5 小时前
Spring MVC(一)
java·spring·mvc
派葛穆6 小时前
Unity-UI 输入框功能
ui·unity·游戏引擎
玖玥拾7 小时前
Unity3D RPG 入门项目(一)开场动画/人物移动/摄像机跟随场景切换
unity·游戏引擎
老马识途2.07 小时前
关于跨域问题的总结
java·前端
都叫我大帅哥8 小时前
Java日期时间三十年战争:从Date考古到LocalDateTime革命,以及数据库与前端的那点事儿
java
Muscleheng8 小时前
SpringBoot 集成 DeepSeek 实现 RAG 文档问答
java·spring boot·ai·springai
2401_894915538 小时前
GEO 搜索优化完整源码从零部署:环境配置、集群搭建全流程
开发语言·python·tcp/ip·算法·unity
2501_936415698 小时前
可变参数&综合练习&斗地主游戏
java·windows·游戏
极客先躯9 小时前
高级java每日一道面试题-2026年05月03日-实战篇[Docker]-如何实现容器化环境的数据加密?
java·运维·docker·容器·金融·加解密·高级面试
WarPigs10 小时前
PC端加载安卓AB包资源显示紫色的问题
unity