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
1
Question by xiqiang · Jan 20, 2016 at 07:22 AM · shaderui imagemask

UI Mask override my shaders custom property ?

I made an ui shader with a grayscale switcher,codes are coppied from "UI-Default",then:

Add a property like this:

 _GraySwitch ("GraySwitch", Float) = 1

Declare:

 fixed _GraySwitch;

In the frag function, after the:

 color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);

Add the codes:

 if(_GraySwitch > 0)
     color.rgb = dot(color.rgb, float3(0.3, 0.59, 0.11));

I also created a script to test this:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System.Collections.Generic;
 
 [RequireComponent(typeof(UnityEngine.UI.Image))]
 public class GrayImage : MonoBehaviour {
     public bool graySwitch = true;
     public Shader grayscaleShader;
 
     UnityEngine.UI.Image image;
 
     void Start () 
     {
         image = GetComponent<UnityEngine.UI.Image>();
         grayscaleShader = Shader.Find("UI/Grayscale");            
         image.material = new Material(grayscaleShader);
     }
     
     void Update () 
     {
         image.material.SetFloat("_GraySwitch", graySwitch ? 1f : 0f );
     }
 }

This works fine, but when the image under a parent with "Mask" component, it can't work! After some tests, I found the variable "_GraySwitch" was changed correctly. So, need some help - -

link text

My unity's version is "5.3.1p1"

grayscale.zip (1.6 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
4
Best Answer

Answer by yaukey · Jan 20, 2016 at 04:08 PM

You should derive "Image" component such as "CustomImage" by your own, and override the "GetModifiedMaterial" method, this method will return a new material copy which is compatible with mask rendering if there is a "Mask" parent attached. Use the "CustomImage" instead of your "Image".

 public class CustomImage : Image
 {
     public override Material GetModifiedMaterial(Material baseMaterial)
     {
         Material cModifiedMat = base.GetModifiedMaterial(baseMaterial);
         // Do whatever you want with this "cModifiedMat"...
         // You can also hold this and process it in your grayscale code.
         // ...
         return cModifiedMat;
     }
 }

Be aware that you are responsible for checking if the material of this image is under the "mask" mode.

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 xiqiang · Jan 21, 2016 at 08:29 AM 1
Share

Works perfect, great job! :D

avatar image rockear · Mar 03, 2019 at 07:57 AM 1
Share

We use Unity 2017.4.18f1,I found Class Image already have a member variable(named materialForRendering) to provided to do this.Have a nice day : P

avatar image
0

Answer by cruelbob · Jan 20, 2016 at 11:04 AM

We have same problem for a half of year.

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

48 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

Related Questions

Problem with sprite materials/shaders inside a mask 1 Answer

Cover overlay sprites with geometry 0 Answers

Change alpha on material with custom shader 0 Answers

Trying to write a 2d mask shader with stencil 0 Answers

RenderTexture on RawImage not working 2 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