GUI on one camera visible to other camera

I’ll try to make this as simple as possible. I have two cameras and I want to attach a 2D GUI to one of them, but see the GUI on the other. It’s a gun with its own HUD, but the player camera can see it.

I’m new to Unity- I tried mounting a plane to the gun, amd then texturing the plane- but the plane only takes materials (any way to ‘texture’ a plane?) and it was affected by lighting. I want to draw 2D texture/GUI elements, so I thought maybe I should be using a GUI layer. But I believe these are invisible to other cameras- right?

Any ideas?

Thanks!

First I’ll answer the plane question. Materials can contain textures if their assigned shaders support textures. Most materials (but not all) use textures. If you click on a game object in the editor, select the material, you should see a texture there if it’s one of the commonly used ones like Diffuse, Specular, etc. You can set the texture of the plane at runtime by assigning a Texture2D (most likely) to it, but that requires that you have said texture to assign, which in your case sounds like something that would be dynamically changing while playing. If that’s the case, then you would have to Render to Texture (A Unity Pro only feature). But lucky for you, you don’t need or want that. It’s way too excessive. However, for future reference, you can access the texture (most of the time) with mainTexture from the material. See: Unity - Scripting API: Material.mainTexture.

Cameras in Unity are a bit more complex than just “Render something to this area from these layers”. They are ordered and cumulative. They are ordered based on the Depth value you set in the Inspector for a camera. Unity will render 1 first and then the next. If the 1st camera clears the depth and color buffers (another option in the inspector for a camera), and the 2nd camera clears just the depth buffer, you end up doing something like this:

Camera A renders the full scene for whatever layers you have selected in the Cull mask.

Camera B clears the depth buffer (basically allows camera B to render everything on top of what’s already in the color buffer).

Camera B renders whatever layer is selected in the Cull mask.

So, this is a common technique to render UI in its own layer that is applied after the world is rendered by the 1st camera. So far, so good. If you just wanted to render the UI in the same place on the screen all the time, you could set camera B at the origin of the world, then make the UI components children of camera B, and even if you moved camera B, the UI would always be in the same relative places on the screen, regardless of where camera A is.

You want your UI to move though. I haven’t tried this specifically, but using the information above, you should be able to child camera B to camera A if the gun HUD remains fairly constant in the world while your camera looks around (i.e. the HUD doesn’t move with camera A). The other key is to child the UI components to the gun’s HUD, and then camera B should always be rendering the UI in the proper place. With those two things, it seems like it would work.