28 Feb 2020
3029字
11分
次
Unity3D
BY-SA 4.0(除特别声明或转载文章外)
<!DOCTYPE html>
UGUI屏幕自适应(多分配率适配)
1、Canvas的属性配置
2、Canvas Scaler的属性配置
3、根据不同的屏幕的比例动态修改缩放基准
-
void Start ()
-
{
-
float standard_width = 960f;
-
float standard_height = 640f;
-
float device_width = 0f;
-
float device_height = 0f;
-
float adjustor = 0f;
-
-
device_width = Screen.width;
-
device_height = Screen.height;
-
-
float standard_aspect = standard_width / standard_height;
-
float device_aspect = device_width / device_height;
-
-
if (device_aspect < standard_aspect)
-
{
-
adjustor = standard_aspect / device_aspect;
-
}
-
-
CanvasScaler canvasScalerTemp = transform.GetComponent<CanvasScaler>();
-
if (adjustor == 0)
-
{
-
canvasScalerTemp.matchWidthOrHeight = 1;
-
}
-
else
-
{
-
canvasScalerTemp.matchWidthOrHeight = 0;
-
}
-
}
将脚本挂在画布控件上。
效果