Summary

When we write shaders, we usually want to be able to change parameters on a per material basis. Properties allow us to do that in unity. In this simple example we’re just going to make color of our unlit shader adjustable.

For this tutorial you should know how to make a super basic unlit shader, you can find a tutorial for that here.

Properties

The first thing we do is change our shader to use a variable as output value instead of a fixed value. We can declare the variable anywhere in the HLSL section of the shader outside of functions or structs. After that the material should look solid black in the editor.

fixed4 _Color;

fixed4 frag(v2f i) : SV_TARGET{
    return _Color
}

Next we add the Properties section to the top of the shader, outside of our subshader. And we add our _Color Property, tell unity to show it as Color in the inspector, define the type as a Color and set the default value to black.

Shader "Tutorial/03_Properties"{
    Properties{
        _Color("Color", Color) = (0, 0, 0, 1)
    }
    ...

The material then looks white. The reason why it didn’t use our default value we defined in the shader is that our material was originally a material with a default shader and had the Color property set to white, that transferred to our new shader. To change the color we can now select the object our material is applied to or the material in the edtitor and change it in the inspector.

I hope it’s clear now how to use properties to change variables in the materials that use your shader instead of writing a lot of slightly different shaders.

You can find the source code here: https://github.com/ronja-tutorials/ShaderTutorials/blob/master/Assets/003_Properties/properties.shader

If you liked my tutorial and want to support me you can do that on Patreon (patreon.com/RonjaTutorials) or Ko-Fi (ko-fi.com/RonjaTutorials).