-
Notifications
You must be signed in to change notification settings - Fork 5
/
CameraManager.cs
49 lines (40 loc) · 1.37 KB
/
CameraManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using Core;
using UnityEngine;
namespace Managers
{
[RequireComponent(typeof(Camera))]
public class CameraManager : MonoBehaviour
{
[Range(1f, 2f)]
[SerializeField] private float marginOffsetPercentage = 1.2f;
private Camera _mainCamera;
private void Awake()
{
_mainCamera = GetComponent<Camera>();
Field.OnUpdateFieldSetup += AdjustCameraHeight;
}
private void OnDestroy()
{
Field.OnUpdateFieldSetup -= AdjustCameraHeight;
}
private void AdjustCameraHeight(FieldSetup setup)
{
Vector2Int fieldSize = setup.size;
if (_mainCamera == null)
{
_mainCamera = Camera.main;
}
// Fit from inside
float fieldAspectRatio = fieldSize.x / (float)fieldSize.y;
if (fieldAspectRatio > _mainCamera.aspect)
{
var ratioOverRatio = fieldAspectRatio / _mainCamera.aspect;
_mainCamera.orthographicSize = (((fieldSize.x * marginOffsetPercentage)/ fieldAspectRatio) / 2f) * ratioOverRatio;
}
else
{
_mainCamera.orthographicSize = fieldSize.y / 2f * marginOffsetPercentage;
}
}
}
}