Author Topic: UITexture - Changing shader in runtime doesn't work  (Read 5368 times)

khenkel

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 3
    • View Profile
UITexture - Changing shader in runtime doesn't work
« on: April 25, 2014, 09:32:51 AM »
Hey guys,

so I was writing a simple grayscale shader, which can be applied to an UITexture in runtime. But when applying it to the texture it stays the old shader.

Here's the code: (it basically iterates through a bunch of effects when clicking on a button. The important part here is the one with "case GRAYSCALE_INDEX")
  1. public void OnClick()
  2.         {
  3.                 _currentIndex++;
  4.  
  5.                 if(_currentIndex >= Colors.Length)
  6.                         _currentIndex = DEFAULT_INDEX;
  7.  
  8.                 switch(_currentIndex)
  9.                 {
  10.                 case DEFAULT_INDEX:
  11.                         if(_coverTexture.enabled)
  12.                                 _coverTexture.enabled = false;
  13.                         if(DrawTexture.shader != _shaderDefault)
  14.                                 DrawTexture.shader = _shaderDefault;
  15.                         break;
  16.                 case GRAYSCALE_INDEX:
  17.                         if(_coverTexture.enabled)
  18.                                 _coverTexture.enabled = false;
  19.                         if(DrawTexture.shader != ShaderGrayscale)
  20.                                 DrawTexture.shader = ShaderGrayscale;
  21.                         break;
  22.                 default:
  23.                         if(DrawTexture.shader != _shaderDefault)
  24.                                 DrawTexture.shader = _shaderDefault;
  25.                         if(!_coverTexture.enabled)
  26.                                 _coverTexture.enabled = true;
  27.                         _coverTexture.color = Colors[_currentIndex];
  28.                         break;
  29.                 }
  30.  

As I said, I can't see the grayscale shader ingame, it just stays the old one.
But:
  • It works correctly in the editor/inspector (changing shader here has the correct result)
  • The shader definitely gets changed in the inspector on runtime
  • The shader stays the one it was on game start. If I put the grayscale shader on the UITexture and then start the game, it doesn't change to the default shader

I also tried it with a material and modifying its shader, but this didn't work, too.

Can anybody help me here? I am using NGUI 3.5.5. Let me know if you need more information.
Thanks in advance!

Cheers
Kilian

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture - Changing shader in runtime doesn't work
« Reply #1 on: April 25, 2014, 10:21:28 AM »
Please update to the latest. If this is regarding what I think it is, it was resolved in one of the recent updates.

khenkel

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: UITexture - Changing shader in runtime doesn't work
« Reply #2 on: April 26, 2014, 02:08:34 PM »
Thanks for your answer!

Ok will do, I'll give you a shout in case this problem persists.

khenkel

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: UITexture - Changing shader in runtime doesn't work
« Reply #3 on: April 28, 2014, 04:15:09 AM »
Ok so I updated to 3.5.8 which (unfortunately) did not fix the problem.
The behaviour is the same as explained in the top post.

However I managed to fix the problem with a kind of workaround:

I randomly observed that the shader changed correctly when switching ON an additional overlay UITexture in front of the texture. (I could see the shader change because the overlay was transparent)

So what I did now was enabling and disabling this overlay texture within one frame (so that the player won't notice), and voilá - it works!

I changed
  1. case GRAYSCALE_INDEX:
  2.                 if(_coverTexture.enabled)
  3.                         _coverTexture.enabled = false;
  4.                 if(DrawTexture.shader != ShaderGrayscale)
  5.                         DrawTexture.shader = ShaderGrayscale;
  6.                 break;

to

  1. case GRAYSCALE_INDEX:
  2.                 _coverTexture.enabled = true;
  3.                 _coverTexture.enabled = false;
  4.                 if(DrawTexture.shader != ShaderGrayscale)
  5.                         DrawTexture.shader = ShaderGrayscale;
  6.                 break;

(Notice how the texture gets enabled and disabled in 2 lines ... but it works somehow)

I am not into graphical stuff, so maybe you guys have any idea why it behaves like this?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture - Changing shader in runtime doesn't work
« Reply #4 on: April 29, 2014, 02:25:27 PM »
The code for setting the shader already removes the widget from the panel, which is what happens when you disable the widget, so I'm not sure why it is like that in your case.