Buttons with a material will not render

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:

Shader "Custom/TestShader" {
    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200
     
        CGPROGRAM
        #pragma surface surf Lambert

        sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutput o) {
            half4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = dot(c.rgb, float3(0.3, 0.59, 0.11));
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

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.


Try working of the UI-Default.shader. They are different than the regular default shader.

1892938–121881–UI-Default.shader (1.7 KB)

1 Like

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?


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.

1 Like

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.

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)

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!

1 Like

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.

It works now. I probably saved it before changing the shader name. Oh well. All part of learning :slight_smile: