Texture2D.GetPixel returning wrong colours

I used Paint(dot)NET to create bitmaps where each pixel colour represents a type of block, where I’d then use a script to read each pixel and create a block based on that.

I saved using both uncompressed PNG and BMP and when I use Texture2D, it works fine for images of about size 20x20, but once I start moving up to about 60x60, it gets less and less accurate. I have also tried with bypassing sRGB sampling, no change in results.

I tried using both Color and Color32, however they both return similar inaccuracies when accounting for the different systems.

I do have the texture imported to not resize if it’s not a power by 2 along a dimension, and I have tried with textures that are a power of 2 along both dimensions. Accuracy of the results improved slightly when the image was a power of 2 along both dimensions.

I am ignoring alpha channels and not using them to compare, so they are not the issue. I am only comparing the RGB parts.

To put the errors into perspective, a pixel that was (155,155,0) when I made in Paint(dot)NET was read as (156,154,0) when the image was 20x20. When the image was 60x60 or even 64x64, I could not identify anywhere in my console logs anything that resembled the original colour.

When using Color32, I had margins of error for +/- 2 and tried with +/- 64. For 20x20 images, +/- 2 was good enough. For 64x64 images, +/- 64 would still have troubles identifying an area that was completely white (not on an image that was completely white).

If I had to guess, it’s like Unity was trying to apply some sort of antialiasing.

I have also opened the texture from after applying these files in Unity to check and they were exactly as I made them, so no errors were made into the files. It seems like Unity is just reading them wrong.

In summary:

  • I have set the test textures to readable
  • I have set them not to resize if not a power of 2 along a dimension, and have created test textures that are powers of 2
  • I have tried with and without sRGB sampling
  • I have tested with both Color and Color32
  • I have tested with ‘wiggle space’ of 25% on both
  • GetPixel is still having troubles

So, I guess my question is how do I get Unity to read the texture ‘absolutely’?

Edit: Changed it to Paint(dot)NET so it doesn’t make weird URLs.

I had a similar issue myself, mine was solved by making them non power of two though. Have you tried making it a GUI texture, as in selecting GUI as the import type, also, I had issues with the format. Try changing it in advanced, I seem to remember that RGBA 32 and ARGB 32 were the ones that worked.

If you want to read the actual pixels, make sure you set “Non power of 2” to “None”, otherwise your texture will be rescaled to the nearest power of two size (…, 16, 32, 64, …)

I was with the same problem, i have to setup it in photoshop to export in RGB32 and change some configurations from sprite in unity:


191499-screenshot-2.png