using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UICameraScale : MonoBehaviour { public float standard_width; //初始宽度 public float standard_height; //初始高度 private float ScreenWith; //当前设备宽度 private float ScreenHeight; //当前设备高度 void Start() { AutoScale(); } private void AutoScale() { Debug.Log(Screen.currentResolution); //屏幕矫正比例 float adjustor = 0f; //获取设备宽高 ScreenWith = Screen.width; ScreenHeight = Screen.height; //计算宽高比例 float standard_aspect = standard_width / standard_height; float device_aspect = ScreenWith / ScreenHeight; //计算矫正比例 if (device_aspect < standard_aspect) { adjustor = standard_aspect / device_aspect; } CanvasScaler canvasScalerTemp = GetComponent(); if (adjustor == 0) { canvasScalerTemp.matchWidthOrHeight = 1; } else { canvasScalerTemp.matchWidthOrHeight = 0; } } void Update() { if (ScreenWith != Screen.width || ScreenHeight != Screen.height) { AutoScale(); } } }