Friday, April 19, 2013

Fading a UITexture's opacity within a panel in NGUI

Here is a common scenario I'm commonly finding myself into: changing a UITexture's opacity using its parent UIPanel's alpha field.

The first time I tried this out, I couldn't get it to work. I was changing the opacity of the UIPanel using the alpha field but the UITexture inside the panel wasn't fading.


Long story short, you need to change the Shader of your UITexture to something like Unlit/Transparent Colored:


And this will give you the result you desired:


This is how it can be done programatically in C#:

var uiTexture = powerupTexture.AddComponent<UITexture>();
uiTexture.shader = Shader.Find("Unlit/Transparent Colored");