How to Mask Text ?

Hello, I am trying to Mask a Text Object. I would like to have a text object then apply some kind of overlay/underlay on that object and reveal a partial amount of the text and the part not revealed shown in black in the screenshot should be see through/transparent.

So to recap - The text should be visible when it’s outside of the overlay/underlay. But the overlay/underlay needs to be transparent instead of the black you see in the screenshot. Imgur: The magic of the Internet

If you have any suggestions on how I can achieve this please let me know.

The masking in Unity 4.6 is done by using a Stencil which means portions of the object are either masked (not visible) or not masked (visible). Stencils do not support alpha / softness. So only 2 states are possible.

The image above shows two objects affected by the same mask. The top object is only visible outside of the mask whereas the bottom object is only visible inside the mask.

P.S. The top object produces the same visual results as the button being drawn on top. Maybe this image is a better example.

This last image makes it easy to see how the transparency / softness doesn’t apply to a stencil.

1 Like

Thank you for your insight Stephan. It was helpful in getting a better understanding of the issue at hand. Would you have any suggestions on how I might be able to achieve the effects or at this point is it simply not an simple task given the structure of uGUI.

Which of those two masking type were you looking for?

I used the TextMeshPro UI Component instead of the UI Text Component to create the examples above because the Masking “_StencilComp” property is exposed in the material editor which allows me to set it manually or via code. The _StencilComp for normal masking is 3 and 5 for the inverted mask.

I believe you should be able to change the _StencilComp value when using the UI Text Component by getting a reference to the material however, given the masking material is controlled by Unity behind the scene, it is possible that they will keep overriding the changes you make.