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

How to set UGUI Sprites in world space to render both in front and behind 3D game objects.

Discussion in 'UGUI & TextMesh Pro' started by Galactic_Muffin, Feb 26, 2015.

  1. Galactic_Muffin

    Galactic_Muffin

    Joined:
    Aug 14, 2013
    Posts:
    67
    Ok so the look I need is something that we all see very commonly in in games and even within Unity's own user interface so i assume UGUI has considered a way to do this (could be as simple as a toggle feature, but I don't know).

    Essentially here is the effect I'm looking for: I have a model that players can rotate and they are able to place hot spots on the model. each hotspot they create is an instantiated UGUI Canvas with some simple UI sprites attached with a billboarding script component. When the player rotates the camera or model, these UGUI sprites rotate along with the model and will eventually end up behind it. Currently the model does as it should and covers up the sprites that are behind it but i want the UGUI sprites to instead show through the model when moved behind and set to 50% alpha instead of 100%. This will eventually be used to indicate to the user that hot spots behind the model are inactive and cannot be clicked but allows the user to easily find hot spots that are hidden.

    How would I go about creating this common effect that we see so much in modern game UI and 3D oriented user interfaces?

    Thanks for any help! :)
     
    Last edited: Feb 26, 2015
  2. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    Later on you'll want to detect from your input code whether a hotspot should be clickable or not. So you might as well write that code first, using a raycast from the camera or something like that, and let that result drive both the alpha setting and the clickability of the hotspot.
     
  3. Galactic_Muffin

    Galactic_Muffin

    Joined:
    Aug 14, 2013
    Posts:
    67
    Well not quite answering the question i had but yes. :p
    I will need to do that eventually. Ill be shooting a raycast from the hotspot to the main camera to see if it is behind a model or not. if it is it will become unclickable and lowered to 50% alpha.

    But this part isn't really the problem.
     
  4. Galactic_Muffin

    Galactic_Muffin

    Joined:
    Aug 14, 2013
    Posts:
    67
    ok so i figured out a solution to get the Hotspot or Spatial UI to render through the models even if they are behind the models. It has nothing to do with code, in fact this effect can simply be done through these steps:

    ~ Create a second camera called "SpatialUICamera".

    ~ parent it under the main camera

    ~ Set the culling mask under SpatialUICamera's properties in the Inspector to render UI only.

    ~ then set Clear Flags to Depth only under SpatialUICamera properties.

    ~ Make sure the main camera is set to Mixed Culling Mask and Solid Color under Clear Flags.

    ~ Also make sure the SpatialUICamera is at least at the same depth or higher than the main camera under "Depth" in the Inspector.

    Then done! :D All the elements you have under "UI" will have an effect that's similar to this:
     
  5. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550