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

相关推荐
西安邮电大学2 小时前
分治算法详细讲解
java·后端·其他·算法·面试
摇滚侠3 小时前
Mybatis 入门到项目实战 搭建 MyBatis 框架 01-14
java·tomcat·mybatis
码不停蹄的玄黓3 小时前
SpringBoot 全局异常处理器实现
java·spring boot·后端
小高学习java4 小时前
事务的边界问题,如何判断数据回滚时机。
java·数据库·后端
何极光4 小时前
Maven安装与配置
java·maven
Ting.~4 小时前
在java中接入百度地图
java·开发语言·dubbo
敲个大西瓜4 小时前
加密算法小解
java
阿维的博客日记4 小时前
怎么样才算是用到了反射呢?有什么关键特征吗
java
wuminyu4 小时前
Java世界中StringTable源码剖析
java·linux·c语言·jvm·c++
Mediary4 小时前
Unity is running with Administrator privileges, which isnot supported...
unity