Class CameraStacker
Registers the attached URP overlay camera into a base camera's stack at runtime, enabling HUD and UI cameras to composite over both 3D and XR scene cameras.
[RequireComponent(typeof(Camera))]
public class CameraStacker : MonoBehaviour
Inheritance
Remarks
In URP, overlay cameras must be explicitly added to a base camera's UnityEngine.Rendering.Universal.UniversalAdditionalCameraData.cameraStack at runtime when the camera is created dynamically (e.g. after the XR origin spawns). This component automates that registration so HUD designers can wire the relationship in the Inspector rather than in code.
Methods
AddToCameraStack(Camera)
Adds this camera as an overlay camera to the camera stack of the specified target base camera at runtime.
public void AddToCameraStack(Camera baseCamera)
Parameters
| Name | Type | Description |
|---|---|---|
baseCamera | Camera | The base camera to which this camera should be added as an overlay. |
Examples
Example usage from another MonoBehaviour:
public class StackController : MonoBehaviour
{
[SerializeField] private CameraStacker overlayCameraStacker;
[SerializeField] private Camera baseCamera;
void Start()
{
if (overlayCameraStacker != null && baseCamera != null)
{
overlayCameraStacker.AddToCameraStack(baseCamera);
}
}
}
Remarks
Warning: URP does not deduplicate the camera stack. If this method is called
more than once with the same baseCamera (e.g. because
Start fires and a caller also invokes this method manually), the
overlay camera will be added twice and URP will silently render it twice per frame,
doubling draw calls for that layer with no error or warning in the console. Always
guard external calls with a membership check, or ensure this method is invoked
exactly once per base camera.