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

Gameobject in front of Interface (How to) [UNSOLVED]

Discussion in 'Scripting' started by DarkEcho, Jan 15, 2015.

  1. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    How would I go about setting up my interface so that I can have a gameobject in front of it, as if your previewing a weapon, your looks etc

    Example:

    Cheers!
     
  2. SevenHams

    SevenHams

    Joined:
    Dec 27, 2012
    Posts:
    67
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Yep, I'd initially approach it as multiple cameras with different depths.
     
  4. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    Cheers both, is there an example of how to put this together, looked up the suggested answer with interface and no results, this is a new field for me.
     
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    It's not user interface specific. If you make a second camera in the scene, and start playing with its "Depth" and "Viewport Rect" values you should get an idea of how to start. You'll also want to play with the culling mask after you've got it in position.
     
  6. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    Ok I had a play with it, no results

    Anyway just so were on the same page here, in terms of the interface I have the GUI.Window blah blah blah (So code) is this what you mean by "It's not user interface specific". Because as far as I am aware, there are 2 types of interface, the interface you code directly and the other gameobject interface stuff...
     
    Last edited: Jan 15, 2015
  7. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
  8. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    What I mean is that picture-in-picture style rendering (one camera on top of another) has nothing specific to do with your GUI code. It's camera functionality. Once you've got that going the code to control it will be more or less the same. I'm on a tablet at the moment, so I can't give you more specific help.

    Did you try getting a picture-in-picture going with a second camera, just using the scene and Inspector? That's your starting point, don't worry about code until you've got that.
     
  9. SevenHams

    SevenHams

    Joined:
    Dec 27, 2012
    Posts:
    67
    The basic idea is that you'll have two cameras; one will have priority thanks to depth. On the the one with priority you'll render the 3D game objects so they'll be in front of the GUI. The GUI gets rendered onto the one that renders behind that.
     
    angrypenguin likes this.
  10. parviz

    parviz

    Joined:
    Jan 16, 2015
    Posts:
    9
  11. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    Yes, I have 2 cameras and played with the Mask settings. I have also found and followed the following tutorial, making the object appear as an interface worked, however half of the tutorial is out of date due to the new UI which im playing with, these are my results so far...

     
  12. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    You're using the World mode for your canvas right? If you use Overlay mode absolutely nothing that isn't also in an Overlay mode canvas will be rendered in front of it.
     
  13. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    The UI just vanishes....


    Is there a tutorial on this subject, this is really hard to understand and there are just to many variables...I have no idea what im doing, I had a play with everything on the forum and....yeah...also looked online, nope

    Ok, I have 2 cameras, first camera is on 1 depth and will only render X layer. Its also set on depth only (This is the camera to render the gameobject)
    Then on the second camera, depth is 0 and will render everything apart from X and has a GUI Layer.

    The UI is still overlapping the gameobject
     
    Last edited: Jan 18, 2015
  14. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    It's confusing because you're mixing together like 3 entirely different systems here. If you're using a canvas, then you're using the new 4.6 GUI, which is a completely separate system from the old GUI and most of the comments and documentation for the old system isn't going to be of much use. When you switch over to RenderMode World instead of RenderMode Overlay for the canvas, you need to set it to use a specific camera and you'll essentially have a "distance to UI" setting for the camera -> UI, and anything in between those two points will render in front of the UI you've made. Actually, though....

    Nevermind the lengthy explanations, just go check out this page on the documentation, and go through the whole UI section. There aren't a whole lot of scripting guides using the new UI system because 90% of the system is visual- there's simply no need to use scripts to do the great majority of the things that you want- things that required scripts before now (so it's like using prefabs versus pure code-creation of assets, or "the reason why we use Unity instead of just a C# editor and a compiler").

    I'm very fond of the new UI system, and once you have a good grasp on it I think you'll really love how easy it is. You will have to do VERY little in code if you set things up right- mostly just swapping out some of your GUI assets with some of your other GUI assets and enabling/disabling gameobjects. It's sort of fantastic. As to your specific question regarding putting a gameobject in front of GUI elements in a canvas, the easy solution might be to simply make your gameobject a child of one of the canvas objects. The only thing I've actually had to put in front of GUI in this manner has been a particle generator, but it was definitely possible- just be sure to use World mode on the canvas rendering and not Overlay, because again, Overlay is made to be rendered absolutely last and there's no changing it that I know of.
     
  15. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    You shouldn't be using the old GUI at all. Don't do anything for this in OnGUI(). Any tute that tells you to use that is out of date and should probably be ignored now.

    The Camera component lets you render to any rectangle on the screen. In your GUI, you'll have to figure out the rectangle of the place where you want to draw, and then use those values in the camera.

    Alternatively, if you have Pro, use a RenderTexture instead.
     
  16. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    Ah I think I see what your getting at. The RenderMode World LITERALLY is a in world gameobject, but its really a interactable interface. So in unity it would be good for interactable, Digital Clocks, PCs, KeyPads etc. However in real life, its the equivilent to a interactive hologram (Like in the Hollywood movies)

    So all I have to do, is put said interface where I want and put the gameobject in front of it. (Shall I make it a child of the Canvas or leave it?)

    So am I on the right track?

    Also would I still need 2 cameras? Still confused on that part...
     
  17. qqqbbb

    qqqbbb

    Joined:
    Jan 13, 2016
    Posts:
    113
    Create new canvas. Set its render mode to "screen space - camera". Make your background image a child of this canvas.
     
  18. clamum

    clamum

    Joined:
    May 14, 2017
    Posts:
    61
    If anyone's reading this later, you can put world stuff in front of your GUI by doing:

    1.) Set your Canvas, that contains your GUI, to have a "Render Mode" of "Screen Space - Camera"
    2.) Drag your Main Camera to the "Render Camera" slot on your Canvas
    3.) Make sure your world space object is in front of your Main Camera, or that it can see it at some point if the object is moving (click the Main Camera and it'll show the little Preview window)
    4.) You can move your world space object forward and back to make it in front of the GUI, or behind it (personally I'm having an object repeatedly fly in front of, then go behind, my GUI menu)

    That should do it I think. If you have multiple cameras then you'll have to tweak this, and I don't have experience with multiple cameras, but I think the general idea would be the same. Hope that helps.
     
    FoxRaider likes this.