Unity Runtime控制编辑器的一些操作
运行时修改Game窗口尺寸
//设置竖屏
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属性修改的是下图的尺寸编号