- Home /
Material not updating on Image
I am updating the texture of a material on runtime, and you can see in the inspector that the material color changes like it should, but the changes don't update on the UI Image unless I edit any property on the Image (like enabling & disabling Raycast Target, Maskable, setting color or source image), what is happening? The texture on the Material is a black-green gradient
The texture of the image remains black-white, which is how it was from the start. I can't attach more images but if you edit any property of the Image, the white part will turn green.
I've tried both img.material.mainTexture = texture and img.material.SetTexture(), same result with both.
Answer by V0odo0 · Aug 03, 2021 at 07:18 PM
I do not recommend to directly change material properties of UI elements but img.materialForRendering is probably what you should use since Graphic component internally copying your shared material. You can also try to request for canvas update after modifying material properties.
Oh, the other option was to update the sprite directly using Sprite.Create() but I assumed it would be more performance heavy, would it be better if I switch?
Answer by chusmaverde · Nov 21, 2021 at 08:53 PM
Hello,
I just ran into a similar issue in which I am editing a texture (due to some image processing with OpenCV) and I want to display it on screen using a UI element. I do not know whether this is expensive but the way I found to make it work is to let the image component know the texture has changed.
This is how I managed to get the texture updated, by calling "SetMaterialDirty":
public Image image;
void Update()
{
if (Input.anyKeyDown)
{
ModifyTexturePixels(); //Some code that modifies the texture
image.SetMaterialDirty();
}
}
Hope it helps
Your answer
Follow this Question
Related Questions
Set image as material 0 Answers
Get Texture 2D after all shader passes are applied 0 Answers
Problem with textures ? 1 Answer
Scaling / Resizing an Image (Texture2D) 6 Answers