Search Unity

(Overlay) Camera Stacking / Clear Depth Via Script

Discussion in 'Editor & General Support' started by Gullie667, Jun 11, 2021.

  1. Gullie667

    Gullie667

    Joined:
    Apr 21, 2021
    Posts:
    17
    In URP, overlay cameras have a 'Clear Depth' Toggle.
    How can this be set via script?

    It can be read from UnityEngine.Rendering.Universal.UniversalAdditionalCameraData.clearDepth but not set?!

    Thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Standard cameras have a clearFlags property on them. Are URP cameras different? Look in their API... it won't be at a global universal level, it will be at a per-camera level if it exists.
     
  3. Gullie667

    Gullie667

    Joined:
    Apr 21, 2021
    Posts:
    17
    The is a clear flag but it seems to be unrelated to the clear depth flag :/
     
  4. imajor

    imajor

    Joined:
    Aug 21, 2020
    Posts:
    1
    I think this is a bug. Indeed changing clearFlags won't affect Clear Depth. The way I workarounded this is that I manually created an overlay camera in unity editor, unchecked the Clear Depth field in the inspector, and then saved this object as a prefab. Later then instead of creating a new camera from script using the new operator, I just instantiated this prefab. It is a very dirty solution, but it works.
     
  5. Mallninja

    Mallninja

    Joined:
    Apr 23, 2021
    Posts:
    1
    Code (CSharp):
    1. private FieldInfo floatingUIFieldInfo;
    2.  
    3. floatingUIFieldInfo = currentPlayer.Camera.FloatingUICamera.GetUniversalAdditionalCameraData()?.GetType().GetField("m_ClearDepth",
    4. BindingFlags.Instance | BindingFlags.NonPublic);
    5.  
    6. floatingUIFieldInfo.SetValue(currentPlayer.Camera.FloatingUICamera.GetUniversalAdditionalCameraData(), false);
    7.