
How to do it...
Once the StandardColor shader is ready, we can start changing its properties:
- In the first line of the script, update the name to the following:
Shader "CookbookShaders/Chapter 02/StandardColor" {
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files emailed directly to you.
- In the Properties block of our shader, remove the current property by deleting the following code from our current shader:
_MainTex ("Albedo (RGB)", 2D) = "white" {}
- As we have removed an essential property, this shader will not compile until the other references to _MainTex are removed. Let's remove this other line inside of the SubShader section:
sampler2D _MainTex;
- The original shader used _MainTex to color the model. Let's change this by replacing the first line of code of the surf() function with this:
fixed4 c = _Color;
Just as you may be used to the float type being used for floating point values when writing code in C# and other programming languages, fixed is used for fixed-point values and is the type used when writing shaders. You may also see the half type used as well, which is like the float type but takes up half the space. This is useful for saving memory but is less precise in how it is presented. We will be discussing this in much greater detail in the Techniques to make shaders more efficient recipe in Chapter 8, Mobile Shader Adjustment.
For more information on fixed-point values, check out https://en.wikipedia.org/wiki/Fixed-point_arithmetic.
The 4 in fixed4 stands for the fact that the color is a single variable that contains four fixed values: red, green, blue, and alpha. You will learn more about how this works and how to modify these values in more detail in the next chapter, Chapter 3, Surface Shaders and Texture Mapping.
- When you save and return to Unity, the shader will compile, and you will see that now our material's Inspector tab doesn't have a texture swatch any more. To complete the refit of this shader, let's add one more property to the Properties block and see what happens. Enter the following code:
_AmbientColor ("Ambient Color", Color) = (1,1,1,1)
- We have added another color swatch to the material's Inspector tab. Now let's add one more in order to get a feel for other kinds of properties that we can create. Add the following code to the Properties block:
_MySliderValue ("This is a Slider", Range(0,10)) = 2.5
- We have now created another GUI element that allows us to visually interact with our shader. This time, we created a slider with the name This is a Slider, as shown in the following screenshot:

- Properties allow you to create a visual way to tweak shaders without having to change values in the shader code itself. The next recipe will show you how these properties can actually be used to create a more interesting shader.
While properties belong to shaders, the values associated with them are stored in materials. The same shader can be safely shared between many different materials. On the other hand, changing the property of a material will affect the look of all the objects that are currently using it.