0 votes

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();
asked Dec 4, 2019 in Projects by daniel500013 (54 points)
edited Dec 4, 2019 by daniel500013

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?)

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

1 Answer

+1 vote
Best answer

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.

answered Dec 5, 2019 by Zylann (21,715 points)
selected Dec 6, 2019 by daniel500013
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.