Mask Component on Gameobject with Text

Hey !

Just one quick question, how can you create a text-mask. When I add the mask component to my gameobject with the text component, it doesn’t work. If I replace the text component with an image component, it works as it should but I’d like to use a changeable text as a mask directly instead of having to use multiple images of text…

Thanks in advance !!

here’s an example of the wanted effect

I’ve found a solution that works, but take note that it might not be the most optimal way to do it.

  1. Create a Canvas with a Text object that will serve as the source for your mask.
  2. Create a Camera and position it so the Text object is visible.
  3. Create a new layer, assign the Canvas and Text objects to this layer.
  4. Set your new Camera’s “Culling Mask” to this new layer only. You may also want to remove that layer from all your other cameras’ culling masks.
  5. Create a Render Texture (Menu “Assets” => “Create” => “Render Texture”. In your camera, set the “Target texture” field to your new render texture.
  6. Use the following code to copy your render texture into a new sprite and set it as a mask for your Sprite Mask.
public RenderTexture renderTexture; // reference to your Render Texture
public SpriteMask spriteMask; // reference to your Sprite Mask

public void SetTextMask() {
  RenderTexture.active = renderTexture;
  Texture2D tex = new Texture2D(renderTexture.width, renderTexture.height);
  tex.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
  tex.Apply();
  Sprite sprite = Sprite.Create(tex, new Rect(0, 0, renderTexture.width, renderTexture.height), new Vector2(0.5f, 0.5f));
  spriteMask.sprite = sprite;
}

EDIT

Oops, I’ve just noticed you want to do that for a Mask component, not a SpriteMask. Instead of changing the sprite of your SpriteMask, change the Image.sprite on the GameObject with the Mask component.

2020, I finally found out how to do this. 
Two things are necessary. 
On the Image, on the Canvas you are using Add these two things: 

 - **First** a **Rect Mask 2D** (any mask I think). select Mask Interaction. 
 - **Second** thing needed, is **Sprite Renderer** on the Image that will act like a "Mask".

On my example: 

**Game Object** > **Canvas** > **Image** (Mask : Image>RectMask2D>SpriteRenderer) > **Text**.

The image on my Canvas IS-THE mask. The text-child will only reveal if it's in the Image container.

No code, just careful logic.

So I wanted to the do the same thing and this thread was the only thing I had found. Unfortunately, neither answer helped me. And the 2nd one looks like it just made it so that text would only be visible when in front of an image but that’s not what the OP or I wanted.

But what worked for me without needing a script or anything was this:
Canvas

-Text: Has the text component and a mask component.

–Image: Has the image component which of course you add the image you want

You’ll of course have to adjust the size of the image to overlap over the text. But you can set the image to stretch and then you don’t always have to resize anyways.

This ended up being way simpler I think.alt text