Author Topic: UITexture not updating  (Read 8598 times)

sonicviz

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 58
    • View Profile
UITexture not updating
« on: October 02, 2013, 12:41:00 AM »
Hi,
I'm trying to get this color picker https://www.assetstore.unity3d.com/#/content/7353 working with NGUI but having a real pita.
I'm using NGui 2.7 for now.

I have a slider with color hues sending the update to a huebrightness material that is on a UITexture, which just has a color value.
This material is updating in the editor properly, and on the NGUI texture on initial start (it sets the right color when the OnSliderChange is sent on Start) but the UITexture will not update after that.

This is driving me nuts!

Any clues as to why this does not work properly?
It should be
. assign material to UITexture
. update material
. UITexture shows changed material

Here's the shader (one of them, the HueBrightness)

As I say this works on startup (see Start() below) but will not update on screen after that (see SetHue(hueSliderValue) ) but in the editor the background material is updating correctly.
Why does it work initially then fail to update?

  1.         public Material backgroundMaterial;
  2.     private     UITexture texture;
  3.         private Texture2D tex;
  4.  
  5.         void Start() {
  6.                
  7.                 texture = GetComponent<UITexture>();           
  8.                 //color = HSBColor.FromColor(renderer.sharedMaterial.GetColor("_Color"));
  9.                 texture.material = backgroundMaterial; 
  10.         }
  11.  
  12.     public void SetHue(float hue)
  13.     {
  14.                 texture.material.SetColor("_Color", new HSBColor(hue, 1, 1).ToColor());
  15.    }

  1. Shader "ColorPicker/ColorSaturationBrightness" {
  2.         Properties {
  3.             _Color ("Main Color", Color) = (0.75,0.15,0.56,1)
  4.         }
  5.         SubShader {
  6.             Pass {     
  7.                         CGPROGRAM
  8.                         #pragma vertex vert
  9.                         #pragma fragment frag
  10.                         #include "UnityCG.cginc"
  11.                        
  12.                         float4 _Color;
  13.                        
  14.                         // vertex input: position, UV
  15.                         struct appdata {
  16.                             float4 vertex : POSITION;
  17.                             float4 texcoord : TEXCOORD0;
  18.                         };
  19.        
  20.                         struct pos_output {
  21.                             float4 pos : SV_POSITION;
  22.                             float4 uv : TEXCOORD0;
  23.                         };
  24.                        
  25.                         pos_output vert(appdata v) {
  26.                             pos_output o;
  27.                             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  28.                             o.uv = float4(v.texcoord.xy, 0, 0);
  29.                             return o;
  30.                         }
  31.                        
  32.                         half4 frag(pos_output o) : COLOR {
  33.                                 half4 c = o.uv.y + (_Color - 1)*o.uv.x;
  34.                             return c;                  
  35.                         }
  36.                         ENDCG
  37.             }
  38.         }
  39. }

ty!
« Last Edit: October 02, 2013, 01:32:13 AM by sonicviz »

freeExec

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: UITexture not updating
« Reply #1 on: October 02, 2013, 03:51:41 AM »
UIDrawCall recreate material.
Workaround share UIDrawCall.mMat

Ferazel

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 150
    • View Profile
Re: UITexture not updating
« Reply #2 on: October 02, 2013, 11:17:55 AM »
Yeah, I've noticed that UITexture was redone since 2.7 where it is a little "stickier" to work with if you change the material rather than going through the UITexture itself. What I end up doing is adding an extension method to UITexture which I wrap a call to UITexture.mainTexture call which will refresh the panel as appropriate. I used to use MarkAsChanged() but in 3.0 I started having problems with that too.

KrakenSpraken

  • Guest
Re: UITexture not updating
« Reply #3 on: November 28, 2013, 09:37:30 AM »
Hello everyone, I have a problem a little similar to this, the texture is load with this codes below but i wont display, any idea why?

  1. UITexture textureComponent = GetComponent<UITexture>();
  2. textureComponent.mainTexture = Resource.Load("texturename") as Texture;
  3. textureComponent.MakePixelPerfect();

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture not updating
« Reply #4 on: November 28, 2013, 02:39:33 PM »
NGUI3 creates a copy of the original material rather than using the original one because it has to create separate draw calls in order to split everything up correctly. You should modify UIWidget.drawCall.dynamicMaterial instead.