Change texture transparency at runtime on MonoGame
Asked Answered
O

1

7

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.

Otolith answered 18/7, 2013 at 9:24 Comment(2)
Oh i found an answer myselft, i need to user something like this:Otolith
Please post your answer to yourself as an answer, then mark it as accepted rather than editing your initial post.Norma
A
2

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.

Agist answered 28/7, 2015 at 9:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.