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

相关推荐
飞舞花下23 分钟前
MAVEN私有仓库配置-Nexus私有仓库
xml·java·maven
毕设源码-赖学姐27 分钟前
【开题答辩全过程】以 基于SpringBoot的健身房管理系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
Filotimo_1 小时前
EntityGraph的概念
java·开发语言·数据库·oracle
wregjru1 小时前
【读书笔记】Effective C++ 条款1~2 核心编程准则
java·开发语言·c++
CreasyChan1 小时前
Unity Shader 入门指南
unity·c#·游戏引擎·shader
heartbeat..2 小时前
Servlet 全面解析(JavaWeb 核心)
java·网络·后端·servlet
vx_bisheyuange2 小时前
基于SpringBoot的疗养院管理系统
java·spring boot·后端
怪我冷i2 小时前
Zed编辑器安装与使用Agent Servers(腾讯CodeBuddy、阿里百炼Qwen Code、DeepSeek Cli)
人工智能·编辑器·ai编程·ai写作·zed
漂视数字孪生世界2 小时前
Unity团结引擎的前世今生
unity·游戏引擎·数字孪生
村口曹大爷2 小时前
JDK 24 正式发布:性能压轴,为下一代 LTS 铺平道路
java·开发语言