- Home /
Question by
AllTheGoodNamesWereTaken · Feb 19, 2021 at 08:26 PM ·
shaderruntimeui image
Change UI shader property at runtime
I have a UI Image with a material & I want to change the property of the shader/material.
gameObject.GetComponent<Image>().material.SetFloat("_highlight", 1);
works just fine but it seems to apply on my prefab shared material. I want to change the instance so I tried:
Material materialInstance;
materialInstance = Instantiate(gameObject.GetComponent<Image>().material);
gameObject.GetComponent<Image>().material = materialInstance;
materialInstance.SetFloat("_highlight", 1);
When I debug it seems that the material property has changed but I don't see the change in game. Do I need to use CanvasRenderer.SetMaterial? I've been at this for over a day, any help much appreciated.
ui-image.png
(33.3 kB)
Comment
Best Answer
Answer by AllTheGoodNamesWereTaken · Feb 20, 2021 at 09:32 AM
If the Image is masked, the shader stencils makes a copy of the shader. In order to reference the shader that is actually rendered on screen you need to use materialForRendering (instead of material).
Your answer
