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

相关推荐
咬人喵喵几秒前
告别无脑 <div>:HTML 语义化标签入门
前端·css·编辑器·html·svg
小付爱coding几秒前
MCP官方调试工具
java·人工智能
一瓢西湖水7 分钟前
浅谈eclipse memory analyzer的应用
java·eclipse
nnsix8 分钟前
Unity WebGL端调用Windows窗口选择文件
unity·游戏引擎·webgl
愚公移码13 分钟前
蓝凌EKP产品:理解连接池、理解Hikari和Druid 区别
java·oracle·hibernate
天远Date Lab14 分钟前
构建金融级风控中台:Java Spring Boot 集成天远借贷风险探查 API 实战
java·大数据·spring boot·金融
向宇it19 分钟前
【unity游戏开发——网络】使用Unity+PurrNet+Heathens+Steam,在 Unity 中通过 Steam与你的朋友建立联系
网络·游戏·unity·c#·游戏引擎·steam
若数19 分钟前
vscode常用插件
ide·vscode·编辑器
Han.miracle23 分钟前
数据结构与算法-012
java·开发语言
计算机毕设指导627 分钟前
基于微信小程序+django连锁火锅智慧餐饮管理系统【源码文末联系】
java·后端·python·mysql·微信小程序·小程序·django