GQ_Communicate/GQ_URP/GQ/Assets/script/UI/UIScalerScreen.cs

31 lines
622 B
C#

using UnityEngine;
using UnityEngine.UI;
public class UIScalerScreen : MonoBehaviour
{
private CanvasScaler canvasScaler;
void Start()
{
canvasScaler = GetComponent<CanvasScaler>();
AdaptToScreenSize();
}
void AdaptToScreenSize()
{
float aspectRatio = (float)Screen.width / Screen.height;
// 根据屏幕宽高比进行自适应调整
if (aspectRatio > 16.0f / 9.0f)
{
// 横屏
canvasScaler.matchWidthOrHeight = 1f;
}
else
{
// 竖屏
canvasScaler.matchWidthOrHeight = 0f;
}
}
}