Skip to content

Instantly share code, notes, and snippets.

@erineccleston
Last active August 7, 2019 21:45
Show Gist options
  • Save erineccleston/97a91f8fbefe90e45e0e42c1bc97b421 to your computer and use it in GitHub Desktop.
Save erineccleston/97a91f8fbefe90e45e0e42c1bc97b421 to your computer and use it in GitHub Desktop.
Allows 2D Pixel Perfect to work with Cinemachine
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);
}
}
@nicmar
Copy link

nicmar commented Dec 20, 2018

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment