Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Dispose Exception for Native Array of Texture2D

Discussion in 'General Graphics' started by PixelizedPlayer, Feb 21, 2021.

  1. PixelizedPlayer

    PixelizedPlayer

    Joined:
    Feb 27, 2013
    Posts:
    506
    Hello


    I have a simple function where i get the native array of my Texture2D, edit the data then apply it.

    I am then trying to Get the native array from the image again to read the data to confirm its the same what i inserted previously, but i get an exception:

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         _data = _result.GetPixelData<Vector4>(0);
    4.         for (int i = 0; i < _size * _size; i++)
    5.         {
    6.  
    7.             Vector4 a = _data1[i];
    8.             Vector4 b = _data2[i];
    9.  
    10.             Vector4 c = Vector4.Lerp(a, b, _t);
    11.             _data[i] = c;
    12.         }
    13.  
    14.         _result.Apply();
    15.         _data = _result.GetPixelData<Vector4>(0); // error here
    16.     }
    Why can't i get the pixel data after applying in the same frame, the next frame i can do it but not the same frame?
     
  2. yzRambler

    yzRambler

    Joined:
    Jan 24, 2019
    Posts:
    37
    _result.Apply();
    This function is not processed immediately.
     
  3. PixelizedPlayer

    PixelizedPlayer

    Joined:
    Feb 27, 2013
    Posts:
    506
    So theres no way to read the data after applying to confirm it worked in the same frame?
     
  4. AljoshaD

    AljoshaD

    Unity Technologies

    Joined:
    May 27, 2019
    Posts:
    69
    The Apply copies/uploads the data to video memory. It should not impact the use of GetPixelData unless you call Apply(makeNoLongerReadable = true). This would remove the CPU copy that is accessed by GetPixelData.
     
  5. PixelizedPlayer

    PixelizedPlayer

    Joined:
    Feb 27, 2013
    Posts:
    506
    Ah so it's default true - sneaky. I didn't notice that. That's one of the reasons I don't like default method parameters, easy to not notice. :(
     
  6. AljoshaD

    AljoshaD

    Unity Technologies

    Joined:
    May 27, 2019
    Posts:
    69
    the default should actually be false. Did changing this value fix it for you?
     
  7. PixelizedPlayer

    PixelizedPlayer

    Joined:
    Feb 27, 2013
    Posts:
    506
    I tried both true and false and got the same error, did you by chance see if it reproduced the same error for you? Should be able to copy paste my code with the necessary setup variables to see if it does for you.
     
unityunity