CinemachinePixelPerfect.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using UnityEngine;
  2. #if CINEMACHINE_URP || 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_URP
  35. #if UNITY_2023_2_OR_NEWER
  36. UnityEngine.Rendering.Universal.PixelPerfectCamera pixelPerfectCamera;
  37. #else
  38. UnityEngine.Experimental.Rendering.Universal.PixelPerfectCamera pixelPerfectCamera;
  39. #endif
  40. #elif CINEMACHINE_PIXEL_PERFECT_2_0_3
  41. UnityEngine.U2D.PixelPerfectCamera pixelPerfectCamera;
  42. #endif
  43. brain.TryGetComponent(out pixelPerfectCamera);
  44. if (pixelPerfectCamera == null || !pixelPerfectCamera.isActiveAndEnabled)
  45. return;
  46. #if UNITY_EDITOR
  47. if (!UnityEditor.EditorApplication.isPlaying && !pixelPerfectCamera.runInEditMode)
  48. return;
  49. #endif
  50. var lens = state.Lens;
  51. lens.OrthographicSize = pixelPerfectCamera.CorrectCinemachineOrthoSize(lens.OrthographicSize);
  52. state.Lens = lens;
  53. }
  54. }
  55. }
  56. #else
  57. // We need this dummy MonoBehaviour for Unity to properly recognize this script asset.
  58. namespace Cinemachine
  59. {
  60. /// <summary>
  61. /// An add-on module for Cinemachine Virtual Camera that tweaks the orthographic size
  62. /// of the virtual camera. It detects the presence of the Pixel Perfect Camera component and use the
  63. /// settings from that Pixel Perfect Camera to correct the orthographic size so that pixel art
  64. /// sprites would appear pixel perfect when the virtual camera becomes live.
  65. /// </summary>
  66. [AddComponentMenu("")] // Hide in menu
  67. [DisallowMultipleComponent]
  68. public class CinemachinePixelPerfect : MonoBehaviour {}
  69. }
  70. #endif