How to make a certain color in Texture2D transparent? (ChromaKey)

Hello,

I am using this link, How to save a picture (take screenshot) from a camera in game? - Questions & Answers - Unity Discussions, to make a screenshot and save it, using toddmoore’s answer. He is using a Texture2D to store the pixel information and later convert it to a file.

However, I would like to replace a certain color (greenscreen) with transparency. So that the resulting picture would have greenscreen replaced with transparency.

An example screen of the picture where I am trying to convert green to transparent:

I tried using a for loop:

foreach (Color pxl in screenShot.GetPixels())
{
    if (pxl.r == 0 && pxl.g == 1 && pxl.b == 0)
    {
        Debug.Log("True"); // Replace this with changing to alpha.
    }
}

This however froze Unity as it is too slow for a picture with resolution 1920x1080.

Setting the camera’s background color to transparent, or Culling to depth does make the background transparent in Unity. However, a black color still remains in the resulting image file.

Thank you!

Do it with a shader and with a falloff instead of an if. Like I posted here:

Thank you, but where do I need to add the shader? On the camera somehow?

I got it working in a sense that I can take screenshots. I had to change the Texture2D-s format to ARBG32.
When taking a screenshot, my camera’s clear flag is set to Depth only. This means when taking pictures there’s a black background, which is registered as transparent. The shader in the link you gave me simply makes the item white. However I see that it does help with removing the 1 pixel edge. Even if just a little bit.

However, when taking a screenshot, I get a 1 pixel black edge (with a standard shader). The result can be seen here:
ScreenShot

I would need to remove the 1 pixel black border. Any ideas on how to do that?