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 /
  • Help Room /
avatar image
12
Question by sillanstudios · Sep 13, 2015 at 04:50 PM · uitextmask

Invert/Reverse UI Mask

Hi Guys, Does anyone know a way to invert a UI mask? I have some text which I'm actually using as a mask to make part of an image see-through. However, it's only showing the image where the text is and I would like the text to be the part of the image that's going transparent. In other words, I want to have a button that has text on it that reveals the background. Does anyone know anyway to do this? Thanks!

Comment
Add comment · Show 1
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 FoxPT · Sep 29, 2015 at 11:08 AM 2
Share

I am having a similar problem and as far as I know it's a limitation of the $$anonymous$$ask component, since we're only allowed to mask child object and not the other way around, so I don't think it is possible using just elements from uGUI.

If you eventually get an answer please let me know. Cheers!

4 Replies

· Add your reply
  • Sort: 
avatar image
49

Answer by glitchers · May 03, 2017 at 05:39 PM

You can do this without writing any shaders or downloading any code. I am doing this in Unity 5.6 but I'm not using any new features to do so.

I was using UIMask to do the mask previously and noticed it was setting some values on the material to do the work. Seeing this, it is easy to create your own version and tweak from there.

Use whatever UI shader you want, I used UI/Default. The material values I used as follows:

 ---- Mask Material

 Tint Color                  (255,255,255,1) //using alpha of 1 gives crispest edge
 Stencil Comparison          8
 Stencil ID                  1
 Stencil Operation           2
 Stencil Write Mask          255
 Stencil Read Mask           255
 Color Mask                  0 // use 15 if you want to see the mask graphic (0 vs RGB 1110)
 Use Alpha Clip              True // toggles if the graphic affects the mask, or just the geometry


 ---- Masked Material

 Tint Color                  (255,255,255,255) // not important
 Stencil Comparison          3
 Stencil ID                  2 // default Unity mask has 1 here. this is the swap. I think it's GEqual => Less
 Stencil Operation           0
 Stencil Write Mask          0
 Stencil Read Mask           1
 Color Mask                  15
 Use Alpha Clip              False

You can find info about the stencil buffer here - https://docs.unity3d.com/Manual/SL-Stencil.html

Comment
Add comment · Show 18 · 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 clinton-reddie · May 20, 2017 at 03:24 AM 1
Share

thanks, it worked!

avatar image KGC · Jun 14, 2017 at 12:59 PM 11
Share

Thanks for a great solution! Just for clearing up ambiguity: The solution is to NOT use the mask component at all. Ins$$anonymous$$d, do the usual thing you'd do with the mask component: 2 gameobjects with Image components, one the child of the other. The parent that normally has the $$anonymous$$ask component should have the $$anonymous$$ask $$anonymous$$aterial assigned in the Image component rather than having a $$anonymous$$ask component. The child should have the $$anonymous$$asked $$anonymous$$aterial assigned in its Image component.

avatar image glitchers KGC · Jun 14, 2017 at 01:02 PM 1
Share

Yeh you don't need the mask component. All that does as far as I can tell is handle the setting of these values automatically.

I'm not using one to do my effect.

avatar image Stardog KGC · Nov 28, 2018 at 12:14 AM 1
Share

Note, the object with the 2nd material doesn't even have to be a child of the 1st mat, just underneath it in the hierarchy will also work.

avatar image Xirion11 KGC · Mar 01, 2019 at 12:13 AM 1
Share

Thank you for the thorough explanation, worked perfectly

avatar image Beks_Omega · Nov 10, 2017 at 07:56 PM 0
Share

@glitchers This simple way of doing inverted masks was very helpful! But I also want to display some things on top of the masked element. The only problem is that they are displayed behind the masked element. Do you know I could get these other elements to be displayed correctly? Ty again!

avatar image rockear · Feb 01, 2018 at 11:06 AM 0
Share

It's the best solution which I had seen!! Thanks bro!!

avatar image Orion · Apr 06, 2018 at 01:53 PM 0
Share

Wow, pure magic! It even works with multiple masks that move separately from each other (which is what I was actually looking for).

Show more comments
avatar image
6

Answer by trx88 · Nov 30, 2015 at 11:13 AM

Some simple instructions on how to use (as FoxPT said):

  1. Download shaders from the provided link

  2. Put them into your project (anywhere would do, but keep in mind your own project hierarchy)

  3. Notice that there are two shaders (UIMask and UIMasked). You need to create two materials (i.e. ReverseMask and ReverseMasked).

  4. Create a new material (ReverseMask) and under "Shader" drop-down menu, select "IndieYP->UIMask". As it's texture, put the texture that you want to be the mask (in my case it was a circle, because I wanted the make a "hole" in the black background).

  5. Create a new material (ReverseMaked) and under "Shader" drop-down menu, select "IndieYP->UIMasked". Set the color of you material (in my case, black).

  6. Now you need to assign the materials to your game objects. ReverseMasked goes to the game object that you want to mask (in my case, panel that's serving as a background). ReverseMask goes to game object that's going to do the masking, in my case, image that's set as a child of the mentioned panel (this is very important).

  7. Also, set the same texture (that you used for ReverseMask) as a source image for Image game object.

And there you have ti! :)

And go to GitHub, and star the guy, this is a big help.

Comment
Add comment · Show 4 · 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 gRntaus · Dec 02, 2015 at 01:16 PM 1
Share

I'm also looking at solving this problem. I've followed your steps but it seems to mask EVERYTHING even my camera space UI that is IN FRONT of the mask. Have you come across this problem or would you know how to go about altering the shader to ignore this issue?

Otherwise it seems to be working as I would expect which is a good start :)

avatar image tomtominc · Mar 21, 2016 at 11:45 PM 1
Share

@trx88 This works very well except for the fact that the UI$$anonymous$$ask shader renders the image to the front of everything regardless of the hierarchy order.

Is there any way around this?

avatar image AndrewRyan · Jul 29, 2016 at 07:10 PM 0
Share

This no longer appears to work as intended in recent updates to Unity.

avatar image KGC · Jun 14, 2017 at 01:00 PM 0
Share

See this answer for a solution for Unity 5.6: http://answers.unity3d.com/answers/1348829/view.html

avatar image
3

Answer by SlavaObninsk · Oct 19, 2015 at 10:04 PM

Unity 5.0 UI Reverse Mask https://github.com/slavaobninsk/UIMask

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 FoxPT · Oct 20, 2015 at 01:59 PM 1
Share

Thanks for sharing the shaders! But can you provide some simple instructions on how to use them?

avatar image t-schulz · Aug 09, 2016 at 09:12 AM 0
Share

Can´t get the UI$$anonymous$$ask from slavaobninsk to work. Can anyone give me help? Have followed the Readme steps but its not masking at all.

avatar image
0

Answer by vincismurf · Sep 08, 2016 at 06:24 PM

Doesn't seem to work in 5.4

Comment
Add comment · Show 4 · 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 t-schulz · Sep 09, 2016 at 07:16 AM 0
Share

no it doesn´t :(

avatar image vincismurf · Sep 13, 2016 at 06:24 PM 0
Share

Actually it does work but latest update caused Z fighting, giving it the illusion it wasn't working check your Z depth when using it on UI elements

avatar image CamoLeopard vincismurf · Sep 14, 2016 at 06:59 AM 0
Share

Can you elaborate on how to get it to work? I have been messing around with the Z depth but it hasn't changed anything.

avatar image clinton-reddie · Oct 18, 2016 at 04:41 PM 0
Share

It also stopped working for me in 5.4, and I haven't been able to make it work messing with Z-depth. Before, I could turn the masks on and off, and they would disappear and reappear properly. Now, I can't find a way to reshape or remove the mask at runtime.

EDIT: After I switched my 2nd camera to Forward mode, this reverse mask works again.

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

56 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

Related Questions

Can not properly display the score on pong game 0 Answers

I have a score displayed constantly in my game, but I can't get it to work for my game-complete screen? Help? 0 Answers

UI Text rendering in front and behind planes 0 Answers

UI TEXT not showing up in game mode 1 Answer

GameObject destroyed when changing scene 1 Answer


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