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

相关推荐
言慢行善9 分钟前
sqlserver模糊查询问题
java·数据库·sqlserver
专吃海绵宝宝菠萝屋的派大星14 分钟前
使用Dify对接自己开发的mcp
java·服务器·前端
大数据新鸟32 分钟前
操作系统之虚拟内存
java·服务器·网络
Tong Z34 分钟前
常见的限流算法和实现原理
java·开发语言
凭君语未可37 分钟前
Java 中的实现类是什么
java·开发语言
He少年39 分钟前
【基础知识、Skill、Rules和MCP案例介绍】
java·前端·python
克里斯蒂亚诺更新1 小时前
myeclipse的pojie
java·ide·myeclipse
迷藏4941 小时前
**eBPF实战进阶:从零构建网络流量监控与过滤系统**在现代云原生架构中,**网络可观测性**和**安全隔离**已成为
java·网络·python·云原生·架构
迷藏4941 小时前
**发散创新:基于Solid协议的Web3.0去中心化身份认证系统实战解析**在Web3.
java·python·web3·去中心化·区块链
qq_433502181 小时前
Codex cli 飞书文档创建进阶实用命令 + Skill 创建&使用 小白完整教程
java·前端·飞书