Strange behavior in Texture2D getpixels

I’m trying to achieve a level editor behavior working with pixeled png to create stages, but I’ve found a strange behavior when I getting pixels from image and don’t found some info about it. I’ve annexed a small scene to show the problem.

  1. Let’s use a simple 3x1 image with a red pixel between 2 transparent pixels
    6010463--647663--upload_2020-6-22_15-44-38.png

  2. The method that read png

public class LevelEditor : MonoBehaviour
{
    public Texture2D level;

    public void CreateMap()
    {
        for (int y = 0; y < level.height; y++)
            for (int x = 0; x < level.width; x++)
                Debug.Log($"[{x}, {y}] -> {level.GetPixel(x, y)}");
    }
}
  1. I’ve created a simple button to call CreateMap in playing mode
[CustomEditor(typeof(LevelEditor))]
public class LevelEditorEditor : Editor
{
    public LevelEditor _target
    {
        get => (LevelEditor)target;
    }
   
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        GUI.enabled = Application.isPlaying;

        if (GUILayout.Button("Create map"))
            _target.CreateMap();
    }
}
  1. Texture import settings

  2. Log that appears when running
    6010463--647672--upload_2020-6-22_15-48-40.png

So, all pixels are red, but some are invisible and some not… I think that i’m making something wrong in import settings but don’t found what.

Obs:

  • If I change the block to any color, the problem persists
  • I can workaround now that I know that the problem is in alpha, but is this the expected behavior?

Hope that someone can help me :stuck_out_tongue_winking_eye:

Tnks

6010463--647672--upload_2020-6-22_15-48-40.png

The PNG format actually does not define what the colors (RGB) will be if the alpha is zero.

1 Like

I really didn’t know it… thanks for info :stuck_out_tongue_winking_eye:

1 Like