Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change texture transparency at runtime on MonoGame

I am very new with MonoGame library. I load a texture from .xnb file

_background = content.Load<Texture2D>(_backgroundKey);

and then i want to change it transparancy(alpha) at the runtime.

Oh i found how to do it myself

spriteBatch.Draw(texture, position, sourceRect, Color.White * 0.5f, .......);

This line of code will draw the texture at half transparency.

like image 622
igorGIS Avatar asked Jul 18 '13 09:07

igorGIS


1 Answers

You can change the opacity of a texture by using a (semi-)transparent color in the draw call:

spriteBatch.Draw(texture, position, new Color(Color.Pink, 0.5f);

The values range from 0 (completely transparent) to 1 (completely opaque). Color has a lot of different constructors, so you can also pass a byte (0-255) instead of a float, which will result in the same thing.

like image 79
jalgames Avatar answered Oct 21 '22 21:10

jalgames