Skip to content

Instantly share code, notes, and snippets.

@dyguests
Last active May 6, 2022 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyguests/7962575e30611239e013b6102593af39 to your computer and use it in GitHub Desktop.
Save dyguests/7962575e30611239e013b6102593af39 to your computer and use it in GitHub Desktop.
SizePerfectCamera. adapter size.
using UnityEngine;
namespace Tools
{
[ExecuteAlways]
public class SizePerfectCamera : MonoBehaviour
{
[SerializeField] private new Camera camera;
[Space] [Tooltip("摄像机的尺寸范围(最小值,最大值)")] [SerializeField]
private Vector2 sizeRange = new(3.6f, 6.4f);
private Vector2Int screenSize;
private void Start()
{
RefreshCamera();
}
private void Update()
{
RefreshCamera();
}
private void RefreshCamera()
{
if (screenSize.x == Screen.width && screenSize.y == Screen.height)
{
return;
}
screenSize = new Vector2Int(Screen.width, Screen.height);
if (Screen.width * sizeRange.y < sizeRange.x * Screen.height)
{
// Debug.DrawLine(-Vector3.one, Vector3.one, Color.red);
camera.orthographicSize = sizeRange.x * Screen.height / Screen.width / 2;
}
else if (Screen.width * sizeRange.x > sizeRange.y * Screen.height)
{
// Debug.DrawLine(-Vector3.one, Vector3.one, Color.blue);
camera.orthographicSize = sizeRange.x / 2;
}
else if (Screen.width > Screen.height)
{
// Debug.DrawLine(-Vector3.one, Vector3.one, Color.yellow);
camera.orthographicSize = sizeRange.y * Screen.height / Screen.width / 2;
}
else
{
// Debug.DrawLine(-Vector3.one, Vector3.one, Color.green);
camera.orthographicSize = sizeRange.y / 2;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment