Determine if Image has transparent background

I have two types of images. Ones with bascially no transparent background and those with plenty of transparency. When displaying them I want to treat them differently.

How can my script figure out which type of image it deals with? Lot’s of transparency or no transparency.

I have tried this code: GraphicsFormatUtility.HasAlphaChannel(backGroundImage.mainTexture.graphicsFormat) but it always returns true.

Hoping for better ideas. Thanks a lot guys!

The best way I can think would be just checking the name of the texture etc.(very cheap and simple) instead of checking the pixels of the texture(can be very expensive and convoluted).

You could mark your high transparency textures with textureName_highTransparency or put the high transparency textures to a list* on a scriptableobject or something.

If you really really cant do this, you could maybe utilize the physics shape of the sprites. If the physics shapes’ area is much smaller than the texture’s area, that’d mean there is a lot of transparent pixels in that sprite. Note that this can get problematic once you have a texture with multiple sprites or once you pack the sprites into a sprite atlas.

*If you are going to check if a texture is in the high transparency textures list a lot of times, you should consider converting the list to a hashset in runtime etc.

u can use this method

    public bool HasTransparency(Texture2D texture)
    {
        Color[] pixels = texture.GetPixels();

        foreach (Color pixel in pixels)
        {
            if (pixel.a < 1f)
            {
                return true;
            }
        }

        return false;
    }