RandomGFGoStop/Assets/Scripts/UI/Core/AutoCanvasScaler.cs

34 lines
983 B
C#

using UnityEngine;
using UnityEngine.UI;
public class AutoCanvasScaler : MonoBehaviour
{
// 기준 해상도
private static readonly Vector2 BASIC_RESOULUTION = new Vector2(1080, 1920);
private void Awake()
{
float basic_Ratio = BASIC_RESOULUTION.x / BASIC_RESOULUTION.y;
float curr_Device_Ratio = Screen.width / (float)Screen.height;
float resultRatio = curr_Device_Ratio / basic_Ratio;
// 1대1 비율에 가까움(탭, 아이패드 등)
if (resultRatio > 1)
{
float match = 0.5f + (resultRatio - 1f);
if (match < 0)
match = 0;
this.GetComponent<CanvasScaler>().matchWidthOrHeight = match;
}
// 한쪽 해상도가 더 길다.
else
{
float match = 0.5f - resultRatio;
if (match > 1)
match = 1;
this.GetComponent<CanvasScaler>().matchWidthOrHeight = match;
}
}
}