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

相关推荐
七宝大爷3 分钟前
第一个CUDA程序:从向量加法开始
android·java·开发语言
煮粥侠_993 分钟前
做RPG游戏时在对话的excel配置表中使用富文本
游戏·unity
__万波__3 分钟前
二十三种设计模式(十一)--享元模式
java·设计模式·享元模式
有什么东东3 分钟前
redis实现店铺类型查看
java·开发语言·redis
hellotutu4 分钟前
IntelliJ IDEA 中引入自定义 JAR 包
java·bash·intellij-idea·jar
budingxiaomoli9 分钟前
初始网络原理
java·运维·服务器·网络
自己的九又四分之三站台10 分钟前
Maven的安装使用
java·maven
bank_dreamer10 分钟前
【ubuntu】vim作为默认编辑器
ubuntu·编辑器·vim
Luna-player15 分钟前
Spring整合MyBatis-Pluss 部分课堂学习笔记
java·开发语言·tomcat