Modify a Sprite alpha
Hey everyone!
I'm finding it a little bit hard to modify a sprite's alpha, as in set by script the alpha value that is on the component that is exposed in the properties panel.
The documentation shows how to set other properties of a SpriteVisual but couldn't find anything related to the Alpha value.
I would really appreciate if someone can point me in the right direction.
Cheers!
Hi Omar,
You can set adjust a sprite's alpha in script by setting the baseColor attribute of the SpriteVisual's material. For example:
This code sets the sprite's alpha to 0.5. You can read more about this technique in the Sprite Guide:
https://lensstudio.snapchat.com/guides/2d/sprite/
Let me know if that works for you!
Best,
Peter
Hey! Did you get it to work?
I'm doing what you say Peter, but the opacity doesn't seem to change at runtime. Is there something I'm missing? I set mySpriteVisual to my current sprite. And I update the alpha by a small value and the script gets called every frame. Even when I try to just keep the alpha at 0 in the editor and then set it to 1 through script it doesn't work.
Thanks!
Hi Shree,
I have some troubleshooting questions for you:
- How are you setting your reference to mySpriteVisual? The example I shared above should have included a property definition for mySpriteVisual, which would then be set in the Inspector panel:
- Do you have any other Scripts in your scene that might be changing the alpha value of the sprite?
- Can you verify that the script is getting called every frame? Try printing a message and check for it in the Logger.
- What is the Blend Mode on your Sprite Visual? Make sure that it's Blend Mode is set to anything other than Disabled. You can verify the Blend Mode by taking a look at the Sprite Component in the Inspector Panel, or by taking a look at the Sprite's Material in Resources (if you set the Material to anything other than Default).
Let me know if any of these checks help resolve your issue.
Best,
Peter
It was the fact that another script was affecting it. Thanks a lot!
Happy to help!