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

相关推荐
吴声子夜歌13 小时前
状态机——枚举实现简单状态机
java·枚举·状态机
Rick199313 小时前
【无标题】
java
yoyo_zzm13 小时前
四大编程技术对比:PHP、Java、Python与HTML
java·python·php
海兰13 小时前
【第54篇】Graph + Langfuse 可观测性实战
java·人工智能·spring boot·spring ai
游乐码13 小时前
Unity坦克案例疑难记录(二)
unity·游戏引擎
笨拙的老猴子13 小时前
JDK8 / JDK11 / JDK17 / JDK21 核心新特性对比,简单总结
java·jdk
江湖中的阿龙13 小时前
【无标题】
java·开发语言
JavaEdge在掘金13 小时前
06-LangChain Tool 加载与使用指南:预制工具、SerpAPI、edge-tts、GraphQL
java