Setting pixels using c# script

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By daniel500013

Hi as in title i would to know how can i set on sprite pixels using c# script. I tried before change pixel on sprite using script but this didn’t work in my project under i give a part of script

Script:

    Sprite map = GetNode("/root/Node2D/Sprite") as Sprite;
    var image = map.GetTexture().GetData();
    image.Lock();

    for (int w = 0; w <= map.GetTexture().GetWidth(); w++)
    {
        for (int h = 0; h <= map.GetTexture().GetHeight() ; h++)
        {
            image.SetPixel(w, h, Color.Color8(244, 253, 21, 50));
        }
    }

    image.Unlock();

What is not working? This code might successfully modify the pixels you downloaded from the graphics card, but it won’t upload them back (unless you did it later in your code?)

Zylann | 2019-12-04 19:56

so if you could me tell how can i upload my received pixel back?

daniel500013 | 2019-12-05 15:49

:bust_in_silhouette: Reply From: Zylann

If your sprite has initially a StreamTexture on it, you will have to create another one of type ImageTexture.

var tex = new ImageTexture()
tex.CreateFromImage(image, map.GetTexture().Flags)
map.SetTexture(tex)

Note: in your code you are filling the whole texture with the same color, you could use Fill instead.