Hello,
Here I’m trying to edit a texture at run time for achieving a scratch card functionality, I know that there are other options to achieve this but for learning purposes, I want to go with editing pixels itself.
My idea is according to mouse position I will change the alpha value of the nearby pixels to 0.
For that, I have conducted a small experiment where I’m copying a texture and just setting its middle portion pixel’s alpha value to 0 through the following code.
public class TextureEditor : MonoBehaviour
{
public Image image1;
public Image image2;
// Start is called before the first frame update
void Start()
{
Texture2D copyTexture = new Texture2D(image1.sprite.texture.width, image1.sprite.texture.height, TextureFormat.ARGB32, false);
Graphics.CopyTexture(image1.sprite.texture, 0, 0, copyTexture, 0, 0);
var colorData = copyTexture.GetPixels32();
for (int i = 0; i < colorData.Length; i++)
{
if (i > (colorData.Length/2) - (colorData.Length * 0.2f) && i < (colorData.Length / 2) + (colorData.Length * 0.2f))
colorData[i].a = 0;
}
copyTexture.SetPixels32(colorData);
copyTexture.Apply();
image2.sprite = Sprite.Create(copyTexture, image1.sprite.rect, image1.sprite.pivot);
}
}
But the resulted texture(copytexture) is not what I have expected, though its middle portion is disappeared, the top and the bottom portion have lost their original colors. I’m not getting why the other 2 portions are changing their color to blue/pink? is I’m doing anything wrong in the code? please can someone help here?
I Have also attached my project setup video link (1minute video), where I have shown texture editor settings and other details.






