Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Texture2D Apply() weird behaviour

Discussion in 'Scripting' started by Thibault-Potier, Oct 4, 2019.

  1. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    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)

    Code (csharp):
    1.  
    2. //create contact
    3.                         Contact newContact = new Contact(result.IdSender,result.Contact.Pseudo);
    4.                         Texture2D tex = new Texture2D(result.Contact.Picture.Width,result.Contact.Picture.Height, TextureFormat.RGBA32, false);
    5. tex.LoadRawTextureData(result.Contact.Picture.Raw);
    6.                         tex.Apply();
    7.                         newContact.picture = tex;
    8.                         //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).
    9.                         contacts.Add(result.IdSender, newContact);
    10.  
    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
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,798
    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:

    https://docs.unity3d.com/ScriptReference/Texture2D.LoadRawTextureData.html

    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[]).
     
  3. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
    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 https://docs.unity3d.com/ScriptReference/Texture2D.LoadRawTextureData.html.

    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 :
    Code (CSharp):
    1. Contact c = AppManager.instance.contacts[peer];
    2.         contactName.text = c.pseudo;
    3.         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 :/ And someHow, calling contact.Picture.Apply() fix the problem.

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