Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Buttons with a material will not render

Discussion in 'UGUI & TextMesh Pro' started by irdc, Dec 19, 2014.

  1. irdc

    irdc

    Joined:
    Mar 23, 2013
    Posts:
    16
    Greetings! I am attempting to add a material with a shader to a button raw image that turns the button grayscale. The shader is as follows:

    Code (CSharp):
    1. Shader "Custom/TestShader" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.     }
    5.     SubShader {
    6.         Tags { "RenderType"="Opaque" }
    7.         LOD 200
    8.      
    9.         CGPROGRAM
    10.         #pragma surface surf Lambert
    11.  
    12.         sampler2D _MainTex;
    13.  
    14.         struct Input {
    15.             float2 uv_MainTex;
    16.         };
    17.  
    18.         void surf (Input IN, inout SurfaceOutput o) {
    19.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    20.             o.Albedo = dot(c.rgb, float3(0.3, 0.59, 0.11));
    21.             o.Alpha = c.a;
    22.         }
    23.         ENDCG
    24.     }
    25.     FallBack "Diffuse"
    26. }
    This is pretty much the default shader with the exception of changing the Albedo. The shader works on objects just fine. It also works on buttons in the scene view. When I switch to game view, the button image disappear. The button is still active but no image is being rendered.

    Does anyone have any idea how I can fix this? Thank you.
     

    Attached Files:

  2. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Try working of the UI-Default.shader. They are different than the regular default shader.
     

    Attached Files:

    RSin11 likes this.
  3. irdc

    irdc

    Joined:
    Mar 23, 2013
    Posts:
    16
    Thank you for the response!

    I attached the UI-default shader to a material and then attached the material to a button. I made some changes that affected the button rendering. One thing I noticed is that even though this shader was only attached to a single button, its effect is being applied to all GUI elements. This doesn't seem very logical.

    I am just starting out with shaders so I am probably missing something. Is there a way to avoid this?
     

    Attached Files:

  4. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    408
    Did you overwrite the UI-Default.shader instead of copying it and modifying the copy?

    That's the shader that's applied by default if you don't assign a material, so if you overwrote it, it will get applied to everything like you said.
     
    irdc likes this.
  5. irdc

    irdc

    Joined:
    Mar 23, 2013
    Posts:
    16
    Thanks for the response.

    I thought I might have done that but ... I created an empty shader in the editor and then pasted in the code. I changed the top to:

    Shader "Custom/Grayscale-Test"

    I don't think I actually wrote over the default. I mean. I guess I had to have but I don't quite know where.

    I deleted the test shaders and all my GUI elements turned into a pink plane (of doom). Now I can't seem to find a way to restore the gui.
     
  6. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    408
    Hmm,

    Check Edit... ProjectSettings...GraphicsSettings

    That should show you what the always included shaders are. You should be able to restore the defaults from there.

    There are three in my list,

    Legacy Shaders/Diffuse
    UI/Default
    UI/Default Font

    (I'm using Unity 5 beta, I think it's just Diffuse in 4.6)
     
  7. irdc

    irdc

    Joined:
    Mar 23, 2013
    Posts:
    16
    Thanks.

    I have just those three as well. I wonder if it has something to do with the ShaderCache. Researching how to clear it to test.

    Yep, the issue was the cache. I just had to close Unity and open the project again and everything was back to normal.

    Thanks for the help!
     
    Last edited: Dec 30, 2014
    DanSuperGP likes this.
  8. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    408
    That still leaves the question of why the default shader got overwritten with your custom one and you ended up with all greyscale UI sprites. Perhaps you saved it before changing the shader name at the top?

    Or does it work now that you've repeated the steps.
     
  9. irdc

    irdc

    Joined:
    Mar 23, 2013
    Posts:
    16
    It works now. I probably saved it before changing the shader name. Oh well. All part of learning :)