forked from UnityCommunity/UnityLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPixelPerfectCamera.cs
39 lines (34 loc) · 897 Bytes
/
PixelPerfectCamera.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
// 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 PixelPerfectCamera : MonoBehaviour
{
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()
{
cam.orthographicSize = Screen.height / pixelsToUnits / 2;
}
}
}