一:前言
例如通过OnGUI绘制一个按钮,在不同分辨率下的大小比例会不同
二:代码实现
using UnityEngine;
public class TestGUI : MonoBehaviour
{
private void OnGUI()
{
Resize();
GUILayout.Button("this is a button");
}
Vector2 nativeResolution = new Vector2(750, 1334);
Vector2 ratio;
void Resize()
{
ratio = new Vector2(Screen.width * 1f / nativeResolution.x, Screen.height * 1f / nativeResolution.y);
Matrix4x4 m = Matrix4x4.identity;
m.SetTRS(Vector3.zero, Quaternion.identity, new Vector3(ratio.x, ratio.y, 1));
GUI.matrix = m;
}
}