The solution to "dynamically change the material of the picture but there is no response" in Unity

The cause of the problem is that I use a font to convert a letter into Material and then hang it on the Image as a randomly generated verification code: like
this.
Verification code image
However, when I click the Button to regenerate (change) the Material and try to change the picture, there is no response.
I was then It turns out that the material is different from the image (Image) itself, it is in a lower generation stage, so it must be rerendered.
As I thought, just changing the color of the picture can make the change of the picture cause the rerender to produce the change I want.
But this method is definitely not the optimal solution. Sure enough, after exploring, I found that the image comes with a command that directly causes rerender:

///Mark the material as dirty and needing rebuilt.
///Send a OnDirtyMaterialCallback notification if any elements are registered. See RegisterDirtyMaterialCallback
Image.SetMaterialDirty();

As its comment describes, easy.

Guess you like

Origin blog.csdn.net/weixin_42767868/article/details/122991493