- Home /
Is there a technique that really works that allows you to change an image by modifying the Texture2D you have associated with it?
I have searched high and wide both in Unity site posts and generally on the web for the answer to this question.
In the Unity posts you find several references to the correct method being to set .mainTexture. My experience is that that works ONCE but not twice in a row.
Here's my situation: I have an IEnumerator running as a coroutine which executes:
fileData = File.ReadAllBytes(this_file);
tex2 = new Texture2D(2000, 2000);
tex2.LoadImage(fileData); //..this will auto-resize the texture dimensions.
tex2 is a null-initialized public static Texture2D. Then the execution sequence does: GameManager.ImageUI_Image.transform.GetComponent().material.mainTexture = tex2;
I also fiddle around with the sizeDelta on the RectTransform of ImageUI_Image. Maybe that isn't relevant.
So far so good. The image is loaded just fine. I see it at execute. Then I modify the image by creating tex3 which is another Texture2D and trying to reset the image to be that newly-generated tex3. The visual picture I see remains unmodified. I have tried several approaches.
Doing the same thing once again does not work. Not change in visual image. i.e.doing the following:
GameManager.ImageUI_Image.GetComponent().mainTexture = GetColorFromanImageUI.tex3;
This doesn't work:
GameManager.ImageUI_Image.transform.GetComponent().material.SetTexture("_MainTex", tex3);
All of the above code is not executing in a script attached to the toplevel UI in which the Image resides. When I create such a script and do the same things there that also does not work. Perhaps I've gotten flustered enough that I haven't actually done every test I thought I did.