Unity: change the color of a material with uniform

#Shader
##Unlit Shader
I'm relatively new to shader development, then didn't know what Unlit shader was. According to the article found on [this page](http://wiki.unity3d.com/index.php/Unlit), 
> This is shader that doesn't do lightning, rendered colors are just the texture color mixed with the set color constant.

Unlit shader doesn't handle lighting stuff.

##Create an Unlit shader in the Unity Editor
When you create an Unlit shader in your editor(suppose the name is test_shader.shader), the code probably looks the following.

```
Shader "Unlit/test_shader"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			// make fog work
			#pragma multi_compile_fog
			
			#include "UnityCG.cginc"

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

			struct v2f
			{
				float2 uv : TEXCOORD0;
				UNITY_FOG_COORDS(1)
				float4 vertex : SV_POSITION;
			};

			sampler2D _MainTex;
			float4 _MainTex_ST;
			
			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				UNITY_TRANSFER_FOG(o,o.vertex);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				// sample the texture
				fixed4 col = tex2D(_MainTex, i.uv);
				// apply fog
				UNITY_APPLY_FOG(i.fogCoord, col);
				return col;
			}
			ENDCG
		}
	}
}
```

##Fragment Shader
You can find a code block related to fragment shader in the code above.

```
            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
```

The fixed4 type variable named "col" applies the color value of each pixel. That said, if you cope with the col before returning it, the look of the material having this shader should look different.

##My First Unlit Shader
Then let's write shader code. The easiest code might apply an identical color to each pixel.

```
			fixed4 frag (v2f i) : SV_Target
			{
				fixed4 col = float4(1.0f, 0.0f, 0.0f, 1.0f);
				return col;
			}
```

Then the game object having the material should look like this.
<img width="428" alt="Screen Shot 2016-06-26 at 21.21.59.png" src="https://qiita-image-store.s3.amazonaws.com/0/53282/9a7ac692-97a9-357a-52dc-9395c917ceeb.png">

##Change the Color via Script
Now you might want to change the color via a script. Given the possibilities of interactions and events that change the material's color, it wouldn't be sufficient to set the color in shader code. You need use "uniform" keywords that your script communicates with shader code.

You need to set a uniform variable. The name can be anything, however, let's name it, say, "_CurrentColor". You can write the following one line of code just below the line of code "#include "UnityCG.cginc". The code thereafter looks like this.

```
Shader "Unlit/test_shader"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			// make fog work
			#pragma multi_compile_fog
			
			#include "UnityCG.cginc"

			uniform float4 _CurrentColor;

			struct appdata
``` 

Also, you need to fix the fragment shader to change the color based on a received value via script.

```
			fixed4 frag (v2f i) : SV_Target
			{
				fixed4 col = _CurrentColor;
				return col;
			}
```

Next, it is requrired to attach a script to the game object and it declares the color and pass it to the shader code.

```csharp(UniformSetter.cs)
using UnityEngine;
using System.Collections;

public class UniformSetter : MonoBehaviour {

	// Use this for initialization
	void Start () {
		//just basic green
		GetComponent<Renderer> ().material.SetVector ("_CurrentColor", new Color(0.0f, 1.0f, 0.0f, 1.0f));
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
```

Finally you got a visual like this.
<img width="424" alt="Screen Shot 2016-06-26 at 21.43.14.png" src="https://qiita-image-store.s3.amazonaws.com/0/53282/f14e6150-8df7-2c6d-99ee-ec45254f0594.png">

##Color Gradient Effect
Furthermore, if you use a method in the Unity API like "Color.Lerp", you would even create a color gradient effect.
[my weird demo here](https://youtu.be/4DS5KytVmas)