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

相关推荐
leoufung11 小时前
LeetCode 373. Find K Pairs with Smallest Sums:从暴力到堆优化的完整思路与踩坑
java·算法·leetcode
阿蒙Amon11 小时前
C#每日面试题-委托和事件的区别
java·开发语言·c#
宋情写11 小时前
java-IDEA
java·ide·intellij-idea
最贪吃的虎11 小时前
Git: rebase vs merge
java·运维·git·后端·mysql
资生算法程序员_畅想家_剑魔12 小时前
Java常见技术分享-12-多线程安全-锁机制
java·开发语言
一叶飘零_sweeeet12 小时前
吃透 Spring 体系结构
java·spring
2401_8370885012 小时前
简要总结 HashSet 和 HashMap(Java)
java·开发语言
毕设源码-钟学长12 小时前
【开题答辩全过程】以 基于Java的家政服务管理系统的设计与实现为例,包含答辩的问题和答案
java·开发语言
小白学大数据12 小时前
Java 爬虫对百科词条分类信息的抓取与处理
java·开发语言·爬虫
Coder_Boy_13 小时前
Spring 核心思想与企业级最佳特性(实践级)事务相关
java·数据库·spring