- Home /
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
Answer by LCStark · Sep 17, 2018 at 03:16 PM
I've found a solution that works, but take note that it might not be the most optimal way to do it.
Create a Canvas with a Text object that will serve as the source for your mask.
Create a Camera and position it so the Text object is visible.
Create a new layer, assign the Canvas and Text objects to this layer.
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.
Create a Render Texture (Menu "Assets" => "Create" => "Render Texture". In your camera, set the "Target texture" field to your new render texture.
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 aMask
component, not aSpriteMask
. Instead of changing the sprite of your SpriteMask, change theImage.sprite
on the GameObject with theMask
component.
Thanks a lot for the answer !! Though, it seems really complicated for something that easy :/, and I would rather use images with the text in it for the performance (it's for a mobile game) if there is no better way..
Yeah. Sometimes things that seem simple and easy take a lot of work in Unity.
If you don't plan on changing the text at runtime, your best bet is to either use images prepared before, or use the code I provided (preferably during a loading screen) to generate all required sprites and store them somewhere for later use.
Answer by ejbytes · Dec 10, 2020 at 01:10 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612174803im_/https://answers.unity.com/themes/thub/images/avi.jpg)