Setting Camera Depth Relative to UI

I’m trying to get a camera to render on top of the UI Canvas in Unity 2017.2.0f3. I have two cameras in the scene, a main camera (depth -1) and a depth-only camera (depth 1). The depth-only camera has a viewport rect in the top corner of the screen. It shows an overhead map view of the scene. The camera is orthographic. The canvas has a sort order of 0 (is that relative to camera depth?), and is set to Screen Space - Overlay.

On the canvas, I have a UI image that serve as a frame for the inset camera view, which means that camera needs to render on top of the UI frame. It renders behind the UI. It doesn’t matter what depth I set the two camera to (> 0).

I have an older project with the exact same camera and canvas set up that, when opened in Unity 5.6, works as expected – the depth-only camera renders on top of the UI.

I just checked with unity 5.6.3p4, doesn’t work either, but I think with Screenspace - Overlay this never worked.
Can you change your UI to Screenspace -Camera with the first camera attached or does this break your setup?

Otherwise you can use a rendertexture in your ui you render to with your camera

1 Like

I’ll take a look at the Screen Space - Camera mode, I’ve never tried it, but this used to work. It looks like with Camera mode I’ll need a separate canvas for each additional camera I want on top of the UI (I need 2).

I’m looking at another project in 5.6.1f1 right now, and it’s definitely working. As I said, the canvas and camera are exactly the same in that project, and the depth-only camera is being drawn on top of the Screen Space - Overlay UI. I don’t have Unity 5.6.3 to try.

Hm, very interesting. I’m using screenspace overlay for a long time now, and I never noticed that rendering on top of that was ever an option, maybe because I never tried.
As from my understand you have the two options with Screenspace camera or a rendertexture that you connect to your ui via rawimage. But it could be that I’m missing something… The renderpass for to overlay ui was always a blackbox to me.

Thanks. I switched to Screen Space - Camera mode, it took some playing around with multiple canvases, and it works. Now I’m not lookiing forward to updating that other project to Unity 2017.2.

I’ve learned that cameras don’t work the same way as canvases do with respect to culling masks. The canvas uses its render camera no matter the layer a particular object is in, setting a canvas object’s layer will not make it visible to another camera.

One other thing, with this arrangement, the image cannot extend outside the camera frame. Which means such an image cannot be used as a border to frame the camera view. So I still had to add a graphic in the main canvas with a transparent center to act as a border around the viewrect.