54 lines
1.3 KiB
C#
54 lines
1.3 KiB
C#
using UnityEngine;
|
|
|
|
[ExecuteAlways]
|
|
public class CameraAspectSetter : MonoBehaviour
|
|
{
|
|
public float baseWidth = 1080f;
|
|
public float baseHeight = 1920f;
|
|
public float baseOrthoSize = 20.6f;
|
|
|
|
private Camera cam;
|
|
private float baseAspect;
|
|
|
|
void Awake()
|
|
{
|
|
cam = GetComponent<Camera>();
|
|
baseAspect = baseWidth / baseHeight;
|
|
UpdateCamera();
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
void Update()
|
|
{
|
|
if (Application.isPlaying)
|
|
UpdateCamera();
|
|
}
|
|
#endif
|
|
|
|
void UpdateCamera()
|
|
{
|
|
if (cam == null || !cam.orthographic)
|
|
return;
|
|
|
|
float currentAspect = (float)Screen.width / Screen.height;
|
|
float scale = currentAspect / baseAspect;
|
|
|
|
// 기본 카메라 사이즈
|
|
cam.orthographicSize = baseOrthoSize / scale;
|
|
|
|
// Y 보정 (세로가 길수록 아래로 살짝 이동)
|
|
float yOffset = 0f;
|
|
if (scale < 1f)
|
|
{
|
|
// 세로가 길다 (ex. 1440x3088)
|
|
yOffset = -(1f - scale) * 27.1f;
|
|
}
|
|
else if (scale > 1f)
|
|
{
|
|
// 세로가 짧다 (ex. 2048x2732 태블릿)
|
|
yOffset = (scale - 1f) * (5f * 0.5f);
|
|
}
|
|
|
|
cam.transform.position = new Vector3(0, yOffset, cam.transform.position.z);
|
|
}
|
|
} |