Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Draw 3D Object over the UI

Discussion in 'UGUI & TextMesh Pro' started by scottlaforge, Jan 8, 2015.

  1. scottlaforge

    scottlaforge

    Joined:
    Mar 12, 2013
    Posts:
    42
    Maybe it is simple but I cannot seem to figure out how to draw a 3D object over the GUI using Unity's built-in system in 4.6, whether the Render Mode is set as "Screen Space - Overlay" or "Screen Space - Camera."

    In 4.5.5, I used NGUI and I had direct access to the camera draw order. Is there anything I can do in 4.6?

    I've attached two images. One shows the GUI drawing correctly behind the 3D object ("correct-draw-order") and the other shows it drawing in front of the 3D object ("gui-draws-over-3d-object").
     

    Attached Files:

  2. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Overlay will draw over everything all the time.

    Camera there is a plane distance you can set to move the canvas further away from the camera. its all about the distance from the camera that will determine the order.
     
    RSin11 likes this.
  3. scottlaforge

    scottlaforge

    Joined:
    Mar 12, 2013
    Posts:
    42
    Do both the GUI and the 3D object need to be parented to the same camera? Currently, we have the on screen 3D controller linked to one camera, and the UI as a "Screen Space - Camera" (as you recommended). Then there are additional cameras that are linked to various button presses: Orbit camera, in-vehicle camera, FPS camera, etc. I notice that the GUI disappears and does not maintain being always in the viewable area. We have the UI camera set with a depth of -1.

    This was all out of the box before with NGUI. Any thoughts or do you need more details? Any and all help is super appreciated.
     
  4. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    Nothing needs to parented to a camera. The camera you set on the Canvas (with Render Mode set to Screen Space - Camera) renders its content using the 'Plane Distance' setting. So if you set 'Plane Distance' to 100 then that Canvas will render at 100 unit in front of the camera. Obviously the far clip plane will need to be >100.
     
  5. scottlaforge

    scottlaforge

    Joined:
    Mar 12, 2013
    Posts:
    42
    Thanks for the response. We ported this project from 4.5.5 to 4.6 and from NGUI to the new Unity GUI system. I believe we've got something else going on. The camera set on the canvas is orthographic and we set the plane distance to 100 simply as a starting point. That being said, on Screen Space - Camera, it does not draw on the screen. On Screen Space -Overlay, though, it does.

    I will post a solution when I find it in case anyone else is experiencing this. Open to any more thoughts on this and will post more details as I have them.
     
  6. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    what do you mean that screen space -camera does not draw on the screen? Like it doesnt draw anything at all? If so might be a layer issue (camera not able to draw that layer). Where Overlay ignore the layer and just draws.
     
    BikramKumar likes this.
  7. scottlaforge

    scottlaforge

    Joined:
    Mar 12, 2013
    Posts:
    42
    Correct. In the editor window, when I have that camera selected in the hierarchy, I can see the little camera preview window and it displays as I would expect it to. However, when playing the project in the editor, none of the UI set on Screen Space - Camera displays in the Game Window. When set to Screen Space - Overlay, it displays correctly (minus the 3D game object problem).

    We have the Canvas Sorting Layer set to Default and the Order in Layer set to -1.

    Here's the Canvas settings:
    canvas-settings.JPG

    Here's the Canvas and the Camera in the Hierarchy:
    hierarchy.JPG

    Here's how the game starts in the editor (no GUI):
    no-gui.JPG
    Here's what it should look like (minus the 3D object under the GUI):
    with-gui.JPG
     
  8. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    so specifying -1 on the order in layer will make it render first from everything. aka i think the GUI is rendering just your ground is drawing after so it is covering the UI. try setting it to 0.
     
  9. scottlaforge

    scottlaforge

    Joined:
    Mar 12, 2013
    Posts:
    42
    Just tried setting the Order in Layer to zero and then 1000. Made no difference. Exactly as displayed above. Thanks for the thought though. Any other details that you could think of that I can get for you that we might have set incorrectly? Otherwise, we may need to start from scratch with the camera system.
     
  10. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    could you tell me the recttransform setting on the canvas? (your image cropped that). But i just noticed your hierarchy the UI camera is under the canvas. I *think* this doesnt work as it kind of does a infinite loop thing moving the canvas and camera ever further.

    Also is your UI drawn by a different camera than the one that draw the scene (guessing cased of the "UI Camera" name)? If so what are the depths of the cameras. the UI camera depth would need to be higher than the 3d camera.
     
    Jushik likes this.
  11. scottlaforge

    scottlaforge

    Joined:
    Mar 12, 2013
    Posts:
    42
    Fantastic! You helped me get the whole problem resolved!
    1. I moved the camera outside of the canvas. That made it show up in the camera preview, but it still would not draw to the game screen when played in the editor.
    2. I had the UI Camera depth set to -1. In NGUI, this was necessary, but with the new Unity UI, it is the opposite. As soon as it was a greater value than the other cameras, it drew over the top. I set it to zero and the other cameras that get swapped out remained at -1.
    3. The 3D controller was still being drawn over the top. As soon as I changed it's depth to 1, it drew on top of the GUI.
    Thank you so very much for your help! Problem solved and now I understand the proper way to set this up when there are many cameras.
     
  12. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    glad its solved.
     
  13. AdeelAbbas

    AdeelAbbas

    Joined:
    Jul 18, 2014
    Posts:
    22
    Can Anyone please guide me I am using Unity 4.6 UI Canvas. The problem is that when I put my 3d Objects in canvas They does not show in game view and when I scale my 3d object then It shows in game view but they look too small in mobile build.
    I am using the hierarchy of UI is
    1 Canvas-parent
    1.1 Panel - Child
    1.1.1 My 3d Object
     
  14. SnowCat

    SnowCat

    Joined:
    May 27, 2013
    Posts:
    5
    Two possible situation:
    1. When your Canvas' Render Mode set to Screen Space - Camera
    Try change your 3d Object larger about scale = {100,100,100}
    Because the Canvas' Scale will very small like 0.02

    2. When your Canvas' Render Mode set to Screen Space - Overlay
    Your object's size is OK, but Canvas is so large! You can find the object and move it into the camera's view
     
    AdeelAbbas likes this.
  15. RealitySims

    RealitySims

    Joined:
    Mar 4, 2014
    Posts:
    28
    I have another (obscure) reason why your 3d objects might appear too small in the mobile build, while they look fine in Unity. Make sure the 3d object is not marked static. That is what I had and was super confused - some 3d objects were correctly scaled while others appeared too tiny on the device. Turns out the tiny ones were static, the others not...

    I guess it makes sense, the canvas scaler might try to scale up your objects, but when they are static it will render the pre-scaled meshes, which were smaller.