Get Your Personalized Game Dev Plan Tailored tips, tools, and next steps - just for you.

Creating Shaders in Unity: A Step-by-Step Tutorial

Posted by Gemma Ellison
./
February 25, 2025
The cover for Creating Shaders in Unity: A Step-by-Step Tutorial

Ever wondered how to make water shimmer realistically or create a fiery explosion in Unity? The answer lies in shaders. They dictate how objects look in Unity. They unlock stunning effects and optimized performance. This tutorial will walk you through crafting your own shaders. No prior experience is needed. If you are new to game development, consider that Wayline, a comprehensive game development platform, can provide tools and resources for every stage of the development process.

Introduction to Shaders and Shader Languages

Shaders are programs that run on the GPU, defining how every object is rendered on screen. Think of them as the artist behind the visual scene.

Photograph of a tranquil lake reflecting a clear blue sky, with gentle ripples disturbing the surface

They’re integral to the rendering pipeline, handling color, lighting, and textures.

Shader languages like HLSL/Cg are used to write shaders, following a basic structure: input, calculations, and output color.

There are different types of shaders, such as surface shaders, which simplify the process, and vertex/fragment shaders, which offer the deepest control.

Setting Up Your Unity Project for Shader Development

Start with a new or existing Unity project. This is your canvas. To create a shader, right-click in the Project window and select Create > Shader. Let’s pick “Unlit Shader” for now. Name it "MyShader.shader". Double-click to open it in your code editor.

Next, create a new material (Create > Material). Select the newly created material. In the Inspector panel for the material, click the “Shader” dropdown menu (usually at the top). Select your custom shader (it will likely be under the “Custom” or “Unlit” category, depending on the type you chose, e.g. Custom/Unlit/MyShader). Finally, drag and drop the material onto a GameObject in your scene. This applies your shader. Learn How To Import Assets Into Unreal Engine

Writing Your First Simple Shader: A Basic Color Shader

Let’s get started. Here’s the code for a basic color shader. Copy and paste this into your shader file, replacing the default content:

Shader "Unlit/ColorShader"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1) // Defines a color property named "_Color"
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" } // Tags for rendering type
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert // Declares the vertex shader function
            #pragma fragment frag // Declares the fragment shader function

            #include "UnityCG.cginc" // Includes common Unity shader functions

            struct appdata
            {
                float4 vertex : POSITION; // Vertex position
                float2 uv : TEXCOORD0; // UV coordinates
            };

            struct v2f
            {
                float2 uv : TEXCOORD0; // UV coordinates
                float4 vertex : SV_POSITION; // Clip space position
            };

            fixed4 _Color; // Color property

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex); // Transforms object space to clip space
                o.uv = v.uv; // Passes UV coordinates to fragment shader
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                return _Color; // Returns the defined color
            }
            ENDCG
        }
    }
}

The Properties block defines adjustable variables in the Inspector. _Color is a color property, defaulting to white. Without it, the color would be fixed.

The #pragma vertex vert and #pragma fragment frag lines tell Unity which functions are the vertex and fragment shaders. These are the entry points for each rendering stage.

The appdata struct defines the input data passed to the vertex shader, including vertex position and UV coordinates. The v2f struct defines the data passed from the vertex shader to the fragment shader, including UV coordinates and clip space position.

The UnityObjectToClipPos function transforms the vertex position from object space to clip space, which is necessary for rendering.

Create a free account, or log in.

Gain access to free articles, game development tools, and game assets.