#if UNITY_EDITOR
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
public static class GameViewUtils
{
private static MethodInfo getGroup;
/// <summary>
/// 根据平台不同,设置屏幕分辨率 - 使用此方法
/// GameViewUtils.GetSize(UnityEditor.GameViewSizeGroupType.Android, 1080, 1920);
/// </summary>
/// <param name="sizeGroupType"></param>
/// <param name="width"></param>
/// <param name="height"></param>
public static void GetSize(GameViewSizeGroupType sizeGroupType, int width, int height)
{
int dex = FindSize(sizeGroupType, width, height);
if (dex != -1)
{
SetSize(dex);
}
else
{
Debug.Log("未找到符合条件的分辨率!");
}
}
public static int FindSize(GameViewSizeGroupType sizeGroupType, int width, int height)
{
var group = GetGroup(sizeGroupType);
var groupType = group.GetType();
var getBuiltinCount = groupType.GetMethod("GetBuiltinCount");
var getCustomCount = groupType.GetMethod("GetCustomCount");
int sizesCount = (int)getBuiltinCount.Invoke(group, null) + (int)getCustomCount.Invoke(group, null);
var getGameViewSize = groupType.GetMethod("GetGameViewSize");
var gvsType = getGameViewSize.ReturnType;
var widthProp = gvsType.GetProperty("width");
var heightProp = gvsType.GetProperty("height");
var indexValue = new object[1];
for (int i = 0; i < sizesCount; i++)
{
indexValue[0] = i;
var size = getGameViewSize.Invoke(group, indexValue);
int sizeWidth = (int)widthProp.GetValue(size, null);
int sizeHeight = (int)heightProp.GetValue(size, null);
if (sizeWidth == width && sizeHeight == height)
return i;
}
return -1;
}
public static void SetSize(int index)
{
var gvWndType = typeof(Editor).Assembly.GetType("UnityEditor.GameView");
var selectedSizeIndexProp = gvWndType.GetProperty("selectedSizeIndex",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
var gvWnd = EditorWindow.GetWindow(gvWndType);
selectedSizeIndexProp.SetValue(gvWnd, index, null);
}
}
#endif
Unity Editor修改分辨率
最新推荐文章于 2024-05-27 17:40:15 发布