How to set transparency for a transform with a number of child GameObjects (with Sprite Renderers)?

I use a transform and attach at runtime a number of generated GameObjects with SpriteRenderers to it. Now I am trying to set the transparency/alpha of this construct at the parent level (the transform). I know that I could loop through all children and modify the alpha of the SpriteRenderers - but I don’t want to have individual transparency of sprites as they overlap and this would have weird effects.

Is there a way to set a transparency for the parent transform which affects the whole construct?

EDIT to clarify the question:
Basically my goal is that I want to combine a transparent background out of different sprites at runtime.

  • One solution would be to create sprite graphics (PNG) with transparency, but then I would have to do it as “Pixel Perfect” (see e.g. here) as the bi- or trilinear filtering will not allow clear combination of transparent PNGs (they overlap and look weird etc).
  • The other solution is this question here: Can I have all those different PNGs without transparency in the original image and when I attach them all at runtime to a gameobject (so that I have my compiled background - but just without transparency - can I then set somewhere an overall transparency on the parent which affects everything. Looping through the childs is not an option, because the transparency on sprite level would lead to the same overlapping problematic as described in the first bullet point.

Hey there

From your question, I’m going to assume you know you can just iterate over the whole ‘tree’ of transforms, and set the transparency of the individual sprite renderers you find in various ways - either by setting their color, or fiddling with materials. If that assumption is wrong, disregard this answer!

I think what you’re asking is a more complex question. It seems you’ve hit the issue that if you have a set of sprite renderers (such as a load of buttons that make up a UI, or a set of sprites for an animated character), some of which may already be partially transparent and overlap each other, when you fiddle with all their transparencies you see artifacts due to layering. An area where 2 sprites overlap is half as transparent as an area with just one sprite. If its a quick fade this often isn’t an issue, but it can cause ugly visuals if the fade is slow or permanent.

Due to the way in which rendering works, this is a tricky problem to solve in game development. The GPU doesn’t understand ‘layers’. If your sprites are generally fully opaque (no transparent bits at all) you can get away with rendering front-to-back and rely on z testing, but this is rarely the case.

The only really solid way of solving this problem is to render your sprites (unfaded) to a separate camera, which is pointed at a unity ‘render texture’. This effectively draws all your sprites in their unfaded state to a single texture.

The render texture can then be used on a material that you draw to the main camera using a single quad. Because this quad is just 1 layer, you can safely fade it out.

This composite-then-fade approach is really the only way on a GPU to achieve your layering effect - certainly with the restrictions of unity’s engine. It is however not massively cheap and can be a pain to implement. It’s often used for things like fading in or out entire user interfaces - a few extra render targets (aka render textures) won’t hurt, but lots and lots will cost serious memory and performance.

If you want lots of things to be faded separately, then I’m afraid you’ll need to simply avoid the issue. Either go down the path of constructing your sprites more carefully so they don’t need to overlap, or avoid slow/permanent fades so the user never notices the artifacts.

-Chris

Do all the sprite renderers use the same material? Then adjust the opacity of the sharedMaterial.