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

Any way to mask with UI Toolkit?

Discussion in 'UI Toolkit' started by PlayingKarrde, Apr 21, 2022.

  1. PlayingKarrde

    PlayingKarrde

    Joined:
    Aug 19, 2013
    Posts:
    38
    This seems like a fundamental feature but I can't for the life of me figure out how to mask objects in the UI.

    How would one even go about making a progress bar that isn't a rectangle without masking? Or avatar images without it? Surely this has to exist but I can't see any way to do it.
     
  2. magnetic_scho

    magnetic_scho

    Joined:
    Feb 2, 2020
    Posts:
    69
    It's only possible to use rectangular masks. You have to set overflow: hidden; on the masking element and define height and width. The child elements need to have position: absolute; so that their dimensions go beyond the parent element.

    For the progress bar:
    If the bar is just a simple rectangle with a background color (maybe with rounded corners), you can set its width in percent.
     
  3. Midiphony-panda

    Midiphony-panda

    Joined:
    Feb 10, 2020
    Posts:
    234
    PlayingKarrde likes this.
  4. AlexandreT-unity

    AlexandreT-unity

    Unity Technologies

    Joined:
    Feb 1, 2018
    Posts:
    323
    If everything goes as planned, we will land an API in 2022.2 that allows to create a VectorImage from a "detached" Painter2D. You will be able to assign it as a background, which can be used to mask with overflow=hidden. This should avoid the package requirement and will allow runtime-generated shapes.
     
  5. PlayingKarrde

    PlayingKarrde

    Joined:
    Aug 19, 2013
    Posts:
    38
    Interesting. Thanks for the suggestion. An SVG will work in the case of a bar, but unfortunately not in the case of an avatar profile (ie think a player's profile image inside a circle).

    -edit- oh wait I think this example is suggesting using the SVG as a mask correct? If so that's perfect
     
  6. Midiphony-panda

    Midiphony-panda

    Joined:
    Feb 10, 2020
    Posts:
    234
    Use a SVG as a mask, yes ;)

    In the screenshot above, the red parts are just red rectangles being masked by the parent VE.
    The first parent VE is a SVG without any filling, whereas the second VE is another SVG using the same shape, but with filling.

    For your use case, you should be OK by :
    - giving to Unity the SVG of a filled circle
    - make it the background of a VE
    - set the VE overflow to hidden (it will be the parent)
    - put children you need to mask inside this parent VE
     
  7. unity_emAfGUQMbV2-rw

    unity_emAfGUQMbV2-rw

    Unity Technologies

    Joined:
    Aug 26, 2020
    Posts:
    3
    For the round avatars, you mentioned you can also use the nifty radius style element :)
     
  8. PlayingKarrde

    PlayingKarrde

    Joined:
    Aug 19, 2013
    Posts:
    38
    The SVG approach works fine (although creates some aliasing which I don't love). The radius element was actually my first attempt but the issue with that is that it doesn't scale. I suppose it might if I used a transform scale instead but in my use case that's not really possible.
     
    Nexer8 likes this.
  9. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    400
    Did this happen?
     
  10. AlexandreT-unity

    AlexandreT-unity

    Unity Technologies

    Joined:
    Feb 1, 2018
    Posts:
    323
  11. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    485
  12. eggsamurai

    eggsamurai

    Joined:
    Oct 10, 2015
    Posts:
    99
    i give up and use rendertexture instead.