Author Topic: Modifying UI2DSprite color at runtime using material shader  (Read 2203 times)

Oberyn

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Modifying UI2DSprite color at runtime using material shader
« on: August 25, 2014, 05:43:56 PM »
Hi everyone,

I currently have:

  • An UI2DSprite.
  • A material with a custom shader that I wrote.

As you can guess, my UI2DSprite uses that material.

The shader on the material is simple : given a value, the shader will lerp 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 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.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Modifying UI2DSprite color at runtime using material shader
« Reply #1 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.

Oberyn

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Modifying UI2DSprite color at runtime using material shader
« Reply #2 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, 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. I will update and try it ASAP.
« Last Edit: August 26, 2014, 10:27:46 AM by Oberyn »