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

相关推荐
Mr YiRan16 分钟前
C++语言类中各个重要函数原理
java·开发语言·c++
chilavert31818 分钟前
技术演进中的开发沉思-370:final 关键字(上)
java·开发语言
一切顺势而行18 分钟前
python 文件目录操作
java·前端·python
砚边数影34 分钟前
智慧校园后端演进:如何处理每日亿级传感器数据的“存、压、查”?
java·数据库·时序数据库·kingbase·数据库平替用金仓·金仓数据库
zlpzpl1 小时前
Java总结进阶之路 (基础二 )
java·开发语言·python
冬夜戏雪1 小时前
线性池java demo
java·开发语言
追随者永远是胜利者1 小时前
(LeetCode-Hot100)5. 最长回文子串
java·算法·leetcode·职场和发展·go
小灵不想卷1 小时前
LangChain4j 多模态
java·langchain4j
wuqingshun3141592 小时前
String、StringBuffer、StringBuilder的应用场景
java·开发语言·jvm
日月云棠2 小时前
JDK 17 特性详解
java