CinemachinePixelPerfect.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using UnityEngine;
  2. #if CINEMACHINE_LWRP_7_1_3 || CINEMACHINE_PIXEL_PERFECT_2_0_3
  3. namespace Cinemachine
  4. {
  5. /// <summary>
  6. /// An add-on module for Cinemachine Virtual Camera that tweaks the orthographic size
  7. /// of the virtual camera. It detects the presence of the Pixel Perfect Camera component and use the
  8. /// settings from that Pixel Perfect Camera to correct the orthographic size so that pixel art
  9. /// sprites would appear pixel perfect when the virtual camera becomes live.
  10. /// </summary>
  11. [AddComponentMenu("")] // Hide in menu
  12. [ExecuteAlways]
  13. [DisallowMultipleComponent]
  14. [HelpURL(Documentation.BaseURL + "manual/CinemachinePixelPerfect.html")]
  15. public class CinemachinePixelPerfect : CinemachineExtension
  16. {
  17. /// <summary>Callback to tweak the orthographic size</summary>
  18. /// <param name="vcam">The virtual camera being processed</param>
  19. /// <param name="stage">The current pipeline stage</param>
  20. /// <param name="state">The current virtual camera state</param>
  21. /// <param name="deltaTime">The current applicable deltaTime</param>
  22. protected override void PostPipelineStageCallback(
  23. CinemachineVirtualCameraBase vcam,
  24. CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
  25. {
  26. // This must run during the Body stage because CinemachineConfiner also runs during Body stage,
  27. // and CinemachinePixelPerfect needs to run before CinemachineConfiner as the confiner reads the
  28. // orthographic size. We also altered the script execution order to ensure this.
  29. if (stage != CinemachineCore.Stage.Body)
  30. return;
  31. var brain = CinemachineCore.Instance.FindPotentialTargetBrain(vcam);
  32. if (brain == null || !brain.IsLive(vcam))
  33. return;
  34. #if CINEMACHINE_LWRP_7_1_3
  35. UnityEngine.Experimental.Rendering.Universal.PixelPerfectCamera pixelPerfectCamera;
  36. #elif CINEMACHINE_PIXEL_PERFECT_2_0_3
  37. UnityEngine.U2D.PixelPerfectCamera pixelPerfectCamera;
  38. #endif
  39. brain.TryGetComponent(out pixelPerfectCamera);
  40. if (pixelPerfectCamera == null || !pixelPerfectCamera.isActiveAndEnabled)
  41. return;
  42. #if UNITY_EDITOR
  43. if (!UnityEditor.EditorApplication.isPlaying && !pixelPerfectCamera.runInEditMode)
  44. return;
  45. #endif
  46. var lens = state.Lens;
  47. lens.OrthographicSize = pixelPerfectCamera.CorrectCinemachineOrthoSize(lens.OrthographicSize);
  48. state.Lens = lens;
  49. }
  50. }
  51. }
  52. #else
  53. // We need this dummy MonoBehaviour for Unity to properly recognize this script asset.
  54. namespace Cinemachine
  55. {
  56. /// <summary>
  57. /// An add-on module for Cinemachine Virtual Camera that tweaks the orthographic size
  58. /// of the virtual camera. It detects the presence of the Pixel Perfect Camera component and use the
  59. /// settings from that Pixel Perfect Camera to correct the orthographic size so that pixel art
  60. /// sprites would appear pixel perfect when the virtual camera becomes live.
  61. /// </summary>
  62. [AddComponentMenu("")] // Hide in menu
  63. [DisallowMultipleComponent]
  64. public class CinemachinePixelPerfect : MonoBehaviour {}
  65. }
  66. #endif