- Home /
Disabling UI masks during runtime and changed material properties
I am currently working on a UI where I dis-/enable a mask during runtime depending on some user-modifyable state. When the mask is first enabled, Unity creates a stenciled copy of each of the children's material. I can access this material while the mask is active by accessing 'MaterialForRendering', but when I disable the mask again, this property points to the normal material. According to a Unity Staff Member, the stenciled material should be discarded when the mask is disabled, but, at least for me, this never happened. Maybe this only happens on the object that holds the mask component, and not on its children. When I enable the mask for the second time, the original material is not copied again, but the old stenciled material is reused.
If I make changes to the material while the mask is disabled, these changes will not be visible in the masked material (or vice versa).
Does anyone know a way to force Unity to discard the stenciled material? Or another useful workaround? Earlier in the project I resorted to never disabling masks, but this is no longer possible.
I did try to call 'Recalculate$$anonymous$$asking' manually, but it didn't make a difference.
Answer by Piflik · Jul 11, 2018 at 02:30 PM
I just found a usable workaround: You can Disable the child-object and then enable it again. MaskableGraphic (which for exampla the UI.Image derives from) discards the stenciled material in OnDisable.
A more experimental (and permanent) solution would be to get Unity's UI repository from Bitbucket and copy two lines from OnDisable() into RecalculateMasking():
StencilMaterial.Remove(m_MaskMaterial);
m_MaskMaterial = null;
You can then build the dll and replace the one in your Unity installation.
I am not sure if that doesn't create problems elsewhere.