- Home /
How can I change the shader parameters for an UI Image?
How can I change the shader parameters for an UI Image?
—
So, inside a canvas I have an image. I have written a custom shader for it and I want to set a shader parameter using SetFloat().
I’m trying this:
myImage.materail.SetFloat( “Param”, 1f );
But the setting the parameter will access the global material, thus affecting all images. I want to access the instance material for the image so that I change only that single image. How can I do this?
—
Footnote: I do know how to do this for meshes. There I access the material for the renderer component and it works. But for UI Images this there is no render compenent.
Answer by Matkins · Mar 10, 2015 at 08:15 PM
I just ran into the same problem. I solved it like this:
Material mat = Instantiate(image.material);
mat.SetFloat("_SomeProperty", 1f);
image.material = mat;
Thanks for sorting this out. Wonder why Unity does these kind of things. What is the reason for making the Image not behave like other entities with materials.
I wish there was some way to get google to move this up in the results when I searched for material instance problems. Nearly every other response seemed to assume that all materials worked the same as mesh render materials. This fixed my problem instantly, thanks!
Answer by boylec · Jun 02, 2015 at 08:37 AM
The following is true for all renderers I have worked with, I'm assuming it is true for UI.Image components as well (Unity GUI images): Just use the renderer.material of the object. Renderer.sharedMaterial is the global one, but as soon as you access the renderer.material Unity makes the material it's own instance.
Your answer
Follow this Question
Related Questions
Image on game object is not being rendered 1 Answer
RenderTexture does not render canvas image. 0 Answers
UI Escape menu script not working. 2 Answers
UI Layering Problem! 2 Answers