UI Mask not working in Camera Space overlay UI?

I am trying to mask my in-game minimap. It is proving much harder than anticipated. Here is the basic set-up. (I’ve tried adding Image objects with Mask components throughout the hierarchy, with no success):

:UI Holder

  • Screen Space - Overlay UI (with various button and text objects)

  • ScreenSpace -Camera UI (linked to a UI-only camera which is parented to the main camera)

  • Minimap Panel element (with line renderer to draw the main camera’s frustrum on to the map)

I made the minimap panel Camera ScreenSpace because Line renderer won’t seem to work in Overlay, but now the UI mask doesn’t work in Camera Space, so my frustrum line draws across the entire screen, which I obviously don’t want. The manual says nothing about UI mask in Camera space, and the only other relevant reference I can find on the net is from another guy who was saying that UI Mask doesn’t work except in Canvas Overlay.

How do I mask the minimap panel while keeping my line? Am I going about this the wrong way or just doing something wrong?
The minimap panel itself has a texture which I update at run time with an orthographic camera’s render texture snapshot.

I had it working previously in Overlay mode using GLDraw on PostRender, but it seems like it would be better to use the native Unity line renderer? GL Draw was a bit limited when it came to drawing icons on the minimap.

Just in case another newbie encounters this, the problem seems to be that, frustratingly, this incredibly basic functionality (drawing a maskable line in the UI) is simply not provided by Unity.

It also isn’t mentioned in any tutorials, or the manual. I’ve wasted several days now on on this, which would never have been a problem in the first place if Unity simply added a UILineRenderer component.

Anyway…The fix is to copy the code provided by firagon here: https://forum.unity3d.com/threads/new-ui-and-line-drawing.253772/

into it’s own script, and then add it as a line renderer component.

You can try using RectMask2D.


###RectMask2D

A RectMask2D is a masking control similar to the Mask control. The mask restricts the child elements to the rectangle of the parent element. Unlike the standard Mask control it has some limitations, but it also has a number of performance benefits.