4
votes

I have this HUD on the top of my app which shows the score (top left corner), an about button (top right corner) and a timeline in the middle. The timeline "scrolls" as time passes.

I want to draw some numbers over the timeline to show the years that it is representing. Im currently using GuiText to do it. Unfortunately I have noticed that, while the timeline bar goes behind the top left object in the scene. The GUIText remains over this object.

Is GUIText always drawn on top of everything else or is there a way to hide it behind a game object?

Update:

After some testing/research I have come to the conclusion that there is probably only one way to get GUIText to not appear in front of certain objects.

If one wishes for their Text (or for that matter any type of GUI stuff) to go behind certain objects, one must use at least one extra camera.

The second camera should be set as "depth only" and have a depth value lower than the main camera. This way, everything that is drawn by the main camera will be on top everything drawn by the second camera.

Use the Culling mask and set the objects you wish to appear/hide in different layers.

2
You won't be able to render guiText under other gameObjects, not without using another camera, at least. If you want the GUI to be 3d elements of your scene, you'll have to look at alternative GUI solutions like NGUI.Max Yankov
I think the problem is the order in which you draw your items. The text on the timeline is drawn over a button? This is because the text is rendered after the button. Try putting that code before the button.LightStriker
I think the issue has to do with the text being GUI and the button/icon a gameobject (cube). I believe that GUI stuff is always drawn last. Is this correct?kUr4m4
any other way I can draw text other than GUIText?kUr4m4

2 Answers

2
votes

You should be able to do this using shaders.

Simply pass the value of the object's depth from the vertex shader on to the pixel shader of the gui. Use this value determining the alpha value of the pixel.

There are numerous ways to do this when using your own game engine and can access the code but Unity is going to restrict you a bit here. I'm not sure if Unity allows you to apply pixel shaders to the GUI. If it doesn't, consider using billboards for this particular part of the gui and doing similar.

0
votes

After some testing/research I have come to the conclusion that there is probably only one way to get GUIText to not appear in front of certain objects.

If one wishes for their Text (or for that matter any type of GUI stuff) to go behind certain objects, one must use at least one extra camera.

The second camera should be set as "depth only" and have a depth value lower than the main camera. This way, everything that is drawn by the main camera will be on top everything drawn by the second camera.

Use the Culling mask and set the objects you wish to appear/hide in different layers.