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

Question Is there way or how can I flatten a UI underneath an object, like in the tank example?

Discussion in 'UI Toolkit' started by Slarti-42, Feb 20, 2021.

  1. Slarti-42

    Slarti-42

    Joined:
    Jul 1, 2019
    Posts:
    49
    Hi folks,

    maybe some of you have seen the Tank tutorial, where they turn the canvas beneath a tank, as a healthbar. Is this possible with the new UI system (yet?).

    Thanks,
    Slarti
     
  2. Mrbeardy

    Mrbeardy

    Joined:
    Jun 30, 2014
    Posts:
    13
    As a workaround for having this natively supported by the UI Document component, you can use a combination of UI Document with a custom Panel Settings asset that has a Target Texture, and then render that target texture on a world-space canvas with RawImage:
    1. On your UI Document component, assign a custom Panel Settings asset
    2. Inside the Panel Settings asset, assign a custom Render Texture asset
    3. Create a World-Space canvas
    4. Add a RawImage within the canvas
    5. Assign the Render Texture asset from earlier into the Texture field.
    You'll also need to tick the first checkbox under the Clear Settings section of the Panel Settings asset:

    upload_2021-2-22_13-17-5.png

    For some reason mine doesn't have a label, but hovering over it shows that it will clear the color buffer before rendering.

    This will prevent the UI being rendered on top of the previous Render Texture contents.

    With all of that done, you should end up with something like this, and any updates to the UI Document will be updated in the render texture:

    upload_2021-2-22_13-20-56.png


    Why are shadows not displaying on top of the UI?
    I'm using the default UI material for the RawImage component which is unlit, which is why shadows aren't displaying over the texture. If you want shadows to affect the texture, you'll have to get a lit material working.

    Why are my changes not refreshing in the scene view?
    This may be a side-effect of the way render textures are handled, as it seems like the render texture is only redrawn when the Game view is visible.

    To get the texture to refresh in the scene view, you'll need to either switch to the Game view and back, or have the game view visible as a docked panel.

    This will also affect play mode, and the same fix will apply.
     
    Last edited: Feb 22, 2021
    Ruchir likes this.