Last active
August 7, 2019 21:45
-
-
Save erineccleston/97a91f8fbefe90e45e0e42c1bc97b421 to your computer and use it in GitHub Desktop.
Allows 2D Pixel Perfect to work with Cinemachine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Cinemachine; | |
using UnityEngine; | |
using UnityEngine.U2D; | |
using System.Reflection; | |
/// <summary> | |
/// Add this component to a camera that has PixelPerfectCamera and CinemachineBrain | |
/// components to prevent the active CinemachineVirtualCamera from overwriting the | |
/// correct orthographic size as calculated by the PixelPerfectCamera. | |
/// </summary> | |
[RequireComponent(typeof(PixelPerfectCamera), typeof(CinemachineBrain))] | |
class OrthographicOverride : MonoBehaviour | |
{ | |
CinemachineBrain CB; | |
object Internal; // PixelPerfectCameraInternal | |
FieldInfo OrthoInfo; | |
void Start() | |
{ | |
CB = GetComponent<CinemachineBrain>(); | |
Internal = typeof(PixelPerfectCamera).GetField("m_Internal", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(GetComponent<PixelPerfectCamera>()); | |
OrthoInfo = Internal.GetType().GetField("orthoSize", BindingFlags.NonPublic | BindingFlags.Instance); | |
} | |
void LateUpdate() | |
{ | |
var cam = CB.ActiveVirtualCamera as CinemachineVirtualCamera; | |
if (cam) | |
cam.m_Lens.OrthographicSize = (float)OrthoInfo.GetValue(Internal); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, I added it to the CM virtual camera, and it fixated the Orthographic size so I can't change it, however I still get a warning about "rendering on odd-numbered resolution, probably when my window is at a weird size. What I'm really trying to do is zoom out my game, but still keep it pixel perfect. How should I to do that?