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

相关推荐
贫民窟的勇敢爷们41 分钟前
Java 与 Python 如何选型与融合
java·开发语言·python
小宇的天下1 小时前
Calibre DESIGNrev 单元(Cell)操作核心指南
java·前端·javascript
阿苟1 小时前
spring重点详解
java·后端·面试
Stream_Silver1 小时前
【JNA实战:Java无缝调用Windows API模拟键盘输入】
java·开发语言·windows
Sam_Deep_Thinking1 小时前
为什么选微服务而不是动态扩容单体
java·jvm·微服务
焦糖玛奇朵婷2 小时前
回收小程序开发案例分享
java·开发语言
成旭先生2 小时前
【2026年可用】企业信息查询API接口
java·大数据·模糊查询·企业信息
小清兔2 小时前
Addressable的设置打包流程
笔记·游戏·unity·c#
l软件定制开发工作室2 小时前
Spring开发系列教程(35)——使用Actuator
java·后端·spring
Liangwei Lin2 小时前
LeetCode 155. 最小栈
java·javascript·算法