Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Pitch92 · Sep 17, 2018 at 01:44 PM · textmaskmasking

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

alt text

131.jpg (68.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

  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.
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pitch92 · Sep 17, 2018 at 08:37 PM 0
Share

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..

avatar image LCStark Pitch92 · Sep 18, 2018 at 05:53 AM 0
Share

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.

avatar image
0

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.

alt text


render.png (246.2 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

93 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can you put a text mesh pro under a 2d sprite mask? 1 Answer

TextMeshPro InputField masking not working 0 Answers

Using UI Text as as mask for an underlying image 2 Answers

UI masking problem on android device since unity 5.3.5 0 Answers

Mask to Camera? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges