Skip to content

Commit 46a2cb2

Browse files
authored
add helpboxes to PixelPerfect inspector (#1075)
1 parent 1c87696 commit 46a2cb2

2 files changed

Lines changed: 39 additions & 13 deletions

File tree

com.unity.cinemachine/Editor/Editors/CinemachinePixelPerfectEditor.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ public override VisualElement CreateInspectorGUI()
1313

1414
#if CINEMACHINE_URP || CINEMACHINE_PIXEL_PERFECT_2_0_3
1515
this.AddMissingCmCameraHelpBox(ux);
16+
17+
var infoBox = ux.AddChild(new HelpBox(
18+
"This component is driving the Pixel Perfect Camera component on the Unity Camera.",
19+
HelpBoxMessageType.Info));
20+
var helpBox = ux.AddChild(new HelpBox(
21+
"This component requires an active Pixel Perfect Camera component on the Unity Camera.",
22+
HelpBoxMessageType.Warning));
23+
24+
ux.TrackAnyUserActivity(() =>
25+
{
26+
var pp = target as CinemachinePixelPerfect;
27+
bool isValid = pp.HasValidPixelPerfectCamera();
28+
infoBox.SetVisible(isValid && pp.enabled);
29+
helpBox.SetVisible(!isValid);
30+
});
1631
#else
1732
ux.Add(new HelpBox("This component is only valid within URP projects", HelpBoxMessageType.Warning));
1833
#endif

com.unity.cinemachine/Runtime/Behaviours/CinemachinePixelPerfect.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
#if CINEMACHINE_URP || CINEMACHINE_PIXEL_PERFECT_2_0_3
44

5+
#if CINEMACHINE_URP
6+
using PixelPerfectCamera = UnityEngine.Rendering.Universal.PixelPerfectCamera;
7+
#else
8+
using PixelPerfectCamera = UnityEngine.U2D.PixelPerfectCamera;
9+
#endif
10+
511
namespace Unity.Cinemachine
612
{
713
/// <summary>
@@ -35,28 +41,33 @@ protected override void PostPipelineStageCallback(
3541
if (brain == null || !brain.IsLiveChild(vcam))
3642
return;
3743

38-
#if CINEMACHINE_URP
39-
#if UNITY_2023_2_OR_NEWER
40-
UnityEngine.Rendering.Universal.PixelPerfectCamera pixelPerfectCamera;
41-
#else
42-
UnityEngine.Experimental.Rendering.Universal.PixelPerfectCamera pixelPerfectCamera;
43-
#endif
44-
#elif CINEMACHINE_PIXEL_PERFECT_2_0_3
45-
UnityEngine.U2D.PixelPerfectCamera pixelPerfectCamera;
46-
#endif
47-
brain.TryGetComponent(out pixelPerfectCamera);
48-
if (pixelPerfectCamera == null || !pixelPerfectCamera.isActiveAndEnabled)
44+
var pixelPerfectCamera = GetPixelPerfectCamera(vcam, true);
45+
if (pixelPerfectCamera == null)
4946
return;
50-
5147
#if UNITY_EDITOR
5248
if (!UnityEditor.EditorApplication.isPlaying && !pixelPerfectCamera.runInEditMode)
5349
return;
5450
#endif
55-
5651
var lens = state.Lens;
5752
lens.OrthographicSize = pixelPerfectCamera.CorrectCinemachineOrthoSize(lens.OrthographicSize);
5853
state.Lens = lens;
5954
}
55+
56+
PixelPerfectCamera GetPixelPerfectCamera(CinemachineVirtualCameraBase vcam, bool liveOnly)
57+
{
58+
var brain = CinemachineCore.FindPotentialTargetBrain(vcam);
59+
if (brain == null || (liveOnly && !brain.IsLiveChild(vcam)))
60+
return null;
61+
var camera = brain.OutputCamera;
62+
if (camera == null || !camera.TryGetComponent(out PixelPerfectCamera pixelPerfectCamera)
63+
|| !pixelPerfectCamera.isActiveAndEnabled)
64+
return null;
65+
return pixelPerfectCamera;
66+
}
67+
68+
// Used by inspector
69+
internal bool HasValidPixelPerfectCamera()
70+
=> TryGetComponent<CinemachineVirtualCameraBase>(out var vcam) && GetPixelPerfectCamera(vcam, false) != null;
6071
}
6172
}
6273
#else

0 commit comments

Comments
 (0)