Reading pixel colors of an image

Right now I’m taking the image from my game assets, but I’ll need a way to load them from a directory.

I need that image to be split into an array of pixel colors. I’ve tried to load it and
convert it into Texture2D but as a result I got a color array of plain RGBA(0, 0, 0, 1).

I need this to make my own tile based physics.

Would love to get help in Javascript. Thanks!

To load an image from a directory you can use


To make it work in unity you need to add a reference to System.Drawing in visual studio and copy the .dll to the Assets/Plugins folder. To find the .dll go to visual studio and after adding the reference click on its name, the path will be in the properties window.
Note that to work in the editor the .dll also has to be in the folder that contains the whole project.

Bitmap colors have values that range from 0 to 255, Texture2D colors are floats that range from 0 to 1 (i think that if you input bigger numbers they just go black).

The only thing you need to do is normalize the Bitmap color values:

public static float Normalize(float current, float min, float max)
    return (current - min) / (max - min);

Convert Bitmap to tex2D:

public Texture2D BitmapToTexture(Bitmap img)
  Texture2D newTex = new Texture2D(img.Width, img.Height);
  for (int x = 0; x < newTex.width; x++)
      for (int y = 0; y < newTex.height; y++)
        float r = Normalize(img.GetPixel(x, y).R, 0f, 255f);
        float g = Normalize(img.GetPixel(x, y).G, 0f, 255f);
        float b = Normalize(img.GetPixel(x, y).B, 0f, 255f);
        float a = Normalize(img.GetPixel(x, y).A, 0f, 255f);
        Color nColor = new Color(r, g, b, a);
        newTex.SetPixel(x, y, nColor);
  return newTex;