Search Unity

  1. We want you to join us at GDC this year! We've added new sessions to Unity Central, space is limited so sign up now!
    Dismiss Notice
  2. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  3. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Tell us about your experience here and you’ll get early access to the 2018 Game Studios report + more goodies.
    Dismiss Notice
  6. Be the first to take a peek at upcoming 2D Animation Preview. Drop into the forum and check it out!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Unity UI Masked Material Shader Changes not working correctly

Discussion in 'Unity UI & TextMesh Pro' started by Piflik, Mar 13, 2018.

  1. Piflik


    Sep 11, 2011
    I regularly run into the same problem currently:

    I have a Material on an UI-Image that is masked by a parent with a Mask Component. Now I want to change some Material Properties via script (SetTexture, SetFloat, etc). I know that there is a new-ish 'MaterialForRendering' property, especially for masked Materials, but for some reason, Unity is unable to use this properly. When I edit properties before turning the Mask on, they are not applied to the masked material, and vice versa. (When I enable the Mask for the first time, the material is copied correctly, but on all subsequent enable/disables, the materials seem to be handled completely separate.)

    I already filed a bug report weeks ago (994413), but it was closed because apparently it is "its not the common case" to want to enable/disable a Mask after changing material properties during runtime.

    An additional problem is that we create all our UI via code, not buy building it in the Editor, so even if I never enable/disable a Mask, I cannot guarantee that material changes happen always after Unity has copied the Material for masking. I can do the changes after parenting, but the masking/material-copying seems to happen sometime during rendering, so I have to make some changes to the 'material', but later ones to the 'materialForRendering'.

    My current 'workaround' is to never disable/enable masks (which brings some limitations to how my application looks) and duplicate every change manually like so (which is stupid):

    img.material.SetFloat("Name", value);
    img.materialForRendering.SetFloat("Name", value);

    Is there a known fix that actually works, or is my use-case just too outlandish?