namespace PixelCamera { using UnityEngine; /// /// The canvas view camera. This smoothes the view on the pixelated game camera by adjusting its position relative to the upscaled display. /// [ExecuteInEditMode] public class CanvasViewCamera : MonoBehaviour { Camera canvasCamera; public float Aspect => this.canvasCamera.aspect; public float Zoom { get ; private set; } void OnEnable() { this.Zoom = -1; this.Initialize(); } /// /// Initializes this component. /// void Initialize() { if (!this.TryGetComponent(out this.canvasCamera)) { Debug.LogError("A camera component is required!"); } else if (this.canvasCamera.orthographic == false) { Debug.LogWarning("The pixel camera system only works in orthographic camera mode. Changing the view camera to orthographic!"); this.canvasCamera.orthographic = true; } } /// /// Adjusts the position on the display canvas to smooth movement. /// public void AdjustSubPixelPosition(Vector2 targetViewPosition, Vector2 canvasLocalScale) { var localPosition = (targetViewPosition - new Vector2(0.5f, 0.5f)) * canvasLocalScale; this.transform.localPosition = new Vector3(localPosition.x, localPosition.y, -1f); } /// /// Sets the orthographic zoom of this camera. /// public void SetZoom(float inputZoom, float halfCanvasHeight) { this.canvasCamera.orthographicSize = inputZoom * halfCanvasHeight; this.Zoom = inputZoom; } /// /// Sets the near and far clip planes of the view camera. /// public void SetClipPlanes(float near, float far) { this.canvasCamera.nearClipPlane = near; this.canvasCamera.farClipPlane = far; } } }