Texture2D Apply() weird behaviour

Hi
I ran into an issue with Texture2D.Apply(). I fixed my problem but i’m not sure about the reason though.

Quick explanation : I made an android messaging app, and i’m basically sending raw picture via string message.(whatever)

//create contact
                        Contact newContact = new Contact(result.IdSender,result.Contact.Pseudo);
                        Texture2D tex = new Texture2D(result.Contact.Picture.Width,result.Contact.Picture.Height, TextureFormat.RGBA32, false);
tex.LoadRawTextureData(result.Contact.Picture.Raw);
                        tex.Apply();
                        newContact.picture = tex;
                        //At this point, sometimes my texture2D appears all black. But if i read some pixels color, there are not black (they have the expected color).
                        contacts.Add(result.IdSender, newContact);

Sometimes, the Texture2D i stored in this piece of code will show a totally black image everywhere i will use it. I first believed that my pixels values where incorect for some reason, but i checked and the pixels values are correct. I found out that i need to call Texture2D.Apply() again to fix the problem on the faulty texture.
I’m not sure to understand why.
My theory is that Texture2D.Apply() is somehow slow and sometimes it’s not finnished before the next line of code (where i assign this texture). But if it’s the case then Texture2D.Apply() would be a kind of asynchronous task ? Then is there a callback function or something that i can use to make sure the texture has been well applyed ?

I’m not sure if my assumption make sense but in any case i would like to have a way to know if i need to call Texture2D.Apply() again or not

Thanks,
Thibault

That does seem odd. Can you reproduce the Texture failure if instead of loading from the contact you instead have an array of bytes with some pattern in it that you Load instead? There is a handy example of providing raw data on the API reference page:

If you cannot reproduce the failure with a pre-made Color[ ] array, perhaps the .Raw property you are using is intended to be used in some way that is asynchronous, or else there is some “check if ready” function you have to poll first.

Another thing to check is if the alpha values are all 1.0 (or 255 if you are using Color32[ ]).

What i grab with “result.Contact.Picture.Raw” is a byte[ ], so i am indeed using
" public void LoadRawTextureData(byte[ ] data); " shown in the example at Unity - Scripting API: Texture2D.LoadRawTextureData.

If the bug appears, i am still able to get the value i expect with “contact.Picture.getPixel(x,y)”. But if i use the very same texture2D to display it :

Contact c = AppManager.instance.contacts[peer];
        contactName.text = c.pseudo;
        contactPicture.sprite = Sprite.Create(c.picture, new Rect(0, 0, c.picture.width, c.picture.height), new Vector2());

it will display all black. The alpha value is not the problem here :confused: And someHow, calling contact.Picture.Apply() fix the problem.

This weid behaviour seems to happen on a very randomly base :confused: