forked from UnityCommunity/UnityLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScaleCamera.cs
43 lines (36 loc) · 1017 Bytes
/
ScaleCamera.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
// pixel perfect camera helpers, from old unity 2D tutorial videos
// source: https://www.youtube.com/watch?v=rMCLWt1DuqI
using UnityEngine;
namespace UnityLibrary
{
[ExecuteInEditMode]
public class ScaleCamera : MonoBehaviour
{
public int targetWidth = 640;
public float pixelsToUnits = 100;
Camera cam;
void Start()
{
cam = GetComponent<Camera>();
if (cam == null)
{
Debug.LogError("Camera not found..", gameObject);
this.enabled = false;
return;
}
SetScale();
}
// in editor need to update in a loop, in case of game window resizes
#if UNITY_EDITOR
void Update()
{
SetScale();
}
#endif
void SetScale()
{
int height = Mathf.RoundToInt(targetWidth / (float)Screen.width * Screen.height);
cam.orthographicSize = height / pixelsToUnits / 2;
}
}
}