Files

33 lines
725 B
C#
Raw Permalink Normal View History

using UnityEngine;
public class CameraZoom : MonoBehaviour
{
public Camera targetCamera;
private float lastCameraSize;
void Start()
{
if(targetCamera == null)
targetCamera = Camera.main;
lastCameraSize = targetCamera.orthographicSize;
UpdateCanvasScale();
}
void LateUpdate()
{
if (Mathf.Abs(targetCamera.orthographicSize - lastCameraSize) > 0.01f) {
lastCameraSize = targetCamera.orthographicSize;
UpdateCanvasScale();
}
}
void UpdateCanvasScale()
{
float scaleFactor = 10f / targetCamera.orthographicSize;
transform.localScale = Vector3.one * scaleFactor;
}
}