1.Canvas的屬性配置:
2.Canvas Scaler的屬性配置:
3.根據不同的屏幕比例動態寫改碩放基準:
1 public float standard_width = 800f; //初始宽度 2 public float standard_height = 480f; //初始高度 3 float device_width = 0f; //当前设备宽度 4 float device_height = 0f; //当前设备高度 5 float adjustor = 0f; //屏幕矫正比例 6 void Start() 7 { 8 9 //获取设备宽高 10 device_width = Screen.width; 11 device_height = Screen.height; 12 //计算宽高比例 13 float standard_aspect = standard_width / standard_height; 14 float device_aspect = device_width / device_height; 15 //计算矫正比例 16 if (device_aspect < standard_aspect) 17 { 18 adjustor = standard_aspect / device_aspect; 19 } 20 21 CanvasScaler canvasScalerTemp = transform.GetComponent<CanvasScaler>(); 22 if (adjustor == 0) 23 { 24 canvasScalerTemp.matchWidthOrHeight = 1; 25 } 26 else 27 { 28 canvasScalerTemp.matchWidthOrHeight = 0; 29 } 30 }
4.將脚本挂在畫布上:
效果如圖: