Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Oberyn on August 25, 2014, 05:43:56 PM

Title: Modifying UI2DSprite color at runtime using material shader
Post by: Oberyn on August 25, 2014, 05:43:56 PM
Hi everyone,

I currently have:


As you can guess, my UI2DSprite uses that material.

The shader on the material is simple : given a value, the shader will lerp (http://en.wikipedia.org/wiki/Lerp_(computing)) between the sprite texel color and a color given as an argument. The alpha is not changed.

In my case, for exemple, given a value between 0 and 1, the UI2DSprite's color will vary between the current color (the UI2DSprite's color) and white (1,1,1,1). Meaning that if I give the value 0 to my material, the UI2DSprite will appear normally. If the value is 1, the UI2DSprite will be a bright white silhouette.

If I set the values for each object containing the UI2DSprite with my material before runtime, everything is great !

Here is my problem : I am able to change and set the value given to the shader at runtime but the changes are not applied to the UI2DSprite at runtime. If, using the Unity editor, I change the 2D Sprite of my game object (to another 2D Sprite), then the changes given the shader will apply. Otherwise, I can change the value given to the shader all day long and the UI2DSprite will not change at all.

Is this by design or is there a way around it ? I have tried marking the widget dirty (using "SetDirty()") but it is not geometry related, so no success here.

I am not sure but is the issue discussed here (http://answers.unity3d.com/questions/616060/change-spriterenderers-texture2d-in-runtime.html) related to my problem ? I tried to apply the solution given in the answer proposed but I realized that UI2DSprite does not have a SpriteRenderer component object to use the function SetPropertyBlock.
Title: Re: Modifying UI2DSprite color at runtime using material shader
Post by: ArenMook on August 26, 2014, 03:22:39 AM
You need to do it in onRender callback. Set UIWidget.onRender, then do your material changes inside. The material is passed as an argument to that function.
Title: Re: Modifying UI2DSprite color at runtime using material shader
Post by: Oberyn on August 26, 2014, 09:57:47 AM
Thank you for the reply ArenMook.

I am looking in the documentation and I cannot find any reference about a member function called onRender. I know there is OnRenderObject (http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnRenderObject.html), but I don't think it is what you mean since this function sadly does not help the issue.

Is onRender a NGUI callback or a MonoBehavior callback ?

Thanks again. :)

EDIT: I just saw that this feature was added in the version 3.7.0 version of NGUI (http://www.tasharen.com/forum/index.php?topic=11.msg50648#msg50648). I will update and try it ASAP.