Unity 2D Game Development, Simple Shader and Material for 2D Graphics that can be used with 2D Shaders and Materials.

Unity is a powerful tool for developing 2D games. In particular, shaders and materials in Unity are important elements that determine the visuals of the game. In this article, we will explore the concepts of 2D shaders and materials, how to set them up, and simple example code to understand how these elements work.

1. Understanding Shaders and Materials

1.1. What is a Shader?

A shader is the way in which the graphics card handles the rendering of 3D and 2D objects. In simple terms, a shader is program code that adds effects to textures or changes their colors. In Unity, shaders can mainly be divided into two types: Vertex shaders and Fragment shaders. In 2D games, Fragment shaders are primarily used for pixel-level processing.

1.2. What is a Material?

A material combines a shader and textures to apply to an object, defining the surface properties of each object. Without a material, a shader cannot be applied to an object. Therefore, materials can apply various effects of the shaders to objects.

2. Key Elements of 2D Shaders

2.1. Unity’s Basic Shaders

Unity has several built-in shaders. The shaders commonly used in 2D games are as follows:

  • Unlit Shader: Used for applying simple colors and textures that do not require lighting.
  • Sprites/Default: The standard shader for 2D sprites, which applies lighting effects.
  • Sprites/Diffuse: A sprite shader that adds the effect of light reflection on the surface.

2.2. Writing Custom Shaders

Creating custom shaders in Unity allows for more unique and varied visual effects. Here is a simple example of a 2D custom shader:

Shader "Custom/MySimpleShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata_t
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata_t v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                return tex2D(_MainTex, i.uv);
            }
            ENDCG
        }
    }
    Fallback "Diffuse"
}

3. Setting and Applying Materials

After writing a shader, you create a material using it. A material is applied to an object by combining shaders and textures, and property values can be set to adjust the visual effects.

3.1. Creating a Material

The steps to create a material in Unity are as follows:

  1. Right-click in the project window of the Unity editor.
  2. Select Create -> Material.
  3. Click the created material and set the shader in the inspector. Select the Custom/MySimpleShader created in the current example.

3.2. Applying Textures to Materials

To apply a texture to a material:

  1. Select the material, then drag and drop the desired texture into MainTex in the inspector.
  2. Adjust the property values of the material to create the desired visual effect.

4. Applying Materials through Code

Materials can also be applied to game objects through scripts. The following code example shows how to apply a material to a specific sprite renderer.

using UnityEngine;

public class ApplyMaterial : MonoBehaviour
{
    public Material customMaterial;

    void Start()
    {
        SpriteRenderer spriteRenderer = GetComponent();
        if (spriteRenderer != null && customMaterial != null)
        {
            spriteRenderer.material = customMaterial;
        }
    }
}

5. Example of Applying Simple Shader Effects

The following is an example of a custom shader that implements a simple effect. This shader applies a texture to an object and can adjust its color.

Shader "Custom/ColorShift"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Color ("Color", Color) = (1,1,1,1)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata_t
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            fixed4 _Color;

            v2f vert (appdata_t v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 texColor = tex2D(_MainTex, i.uv);
                return texColor * _Color;
            }
            ENDCG
        }
    }
    Fallback "Diffuse"
}

6. Conclusion

In this article, we explored the concepts of 2D shaders and materials in Unity, how to set them up, and how to apply visual effects through simple example shaders. Unity is a powerful graphics engine that supports many types of shaders and allows users to create custom shaders for unique visuals. The use of these shaders enhances the overall quality of the game and provides players with a distinctive experience. Experiment to implement even more diverse shader effects. Unleash your creativity!