Wednesday, November 14, 2012

Creating a Colored Unlit Texture Shader for Unity

When dealing with Unity materials, one easy and convenient feature is the ability to change the Main Color of a shader to tint the color of the texture it uses.  This can be used for a lot of different situations, like changing the color of a tile on a selection grid or changing the background color of a 2d game, to name a few.

Unfortunately I found the ability to specify a shader color missing in Unity's Unlit Texture shader.  The Unlit Texture shader isn't affected by lights, which is very convenient when you don't want to include lights in a scene or want to manually control the brightness of a texture.  It makes sense why the Unlit Texture shader doesn't include the color combiner support, the Unlit Texture shader is the simplest and fastest shader they have, and adding a color combiner would add extra instructions which would rarely be used.  Since I needed the ability to color tint an Unlit Texture for my new project, I decided to extend the existing Unlit Texture shader to support a color multiply operation.

The source for the new shader is below, you are free to copy it for your own needs*.  Use the 'Create--Shader' command in unity, then paste this code into the new file.  The shader will appear in the shader dropdown menu under NAKAI/Unlit/Texture Colored.  In case you're wondering, the Unlit Texture shader was written using Unity's ShaderLab Shader Syntax and I added a few extra lines to support the color combine operation.  Here's a picture of it in action, Enjoy!

// Unlit color shader. Very simple textured and colored shader.
// - no lighting
// - no lightmap support
// - per-material color

// Change this string to move shader to new location
Shader "NAKAI/Unlit/Texture Colored" {
    Properties {
        // Adds Color field we can modify
        _Color ("Main Color", Color) = (1, 1, 1, 1)        
        _MainTex ("Base (RGB)", 2D) = "white" {}

    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 100
        Pass {
            Lighting Off
            SetTexture [_MainTex] { 
                // Sets our color as the 'constant' variable
                constantColor [_Color]
                // Multiplies color (in constant) with texture
                combine constant * texture

*You are free to use this for your own games.  Please do not sell it on the Unity asset store or anywhere else. :)


  1. This is Perfect, what I was looking for.
    Thank you.

  2. thanks for sharing, plz add lightmap support too...

  3. This comment has been removed by the author.

  4. This comment has been removed by the author.

  5. I'm making a point-click adventure game and your shader has been really helpful. Thanks a lot for sharing!

    I have a problem though, and I'd appreciate your help on that: I want to read the initial color of a mesh whose material I'm using your shader on.

    The command I'm using in the Start() function is:
    Color intialColor = getComponent().material.color;

    However, I always get the value of (1,1,1,1), which is the "Main Color" value you have defined in your shader.

    Is it possible to read the actual initial color of a mesh?

    Thanks in advance,


    1. Unfortunately, HTML doesn't let me use the "less than" and "greater than" symbols... GetComponent is followed by "Renderer".

      Sorry for spamming your thread :)

  6. I found a way out, eventually. It was a Unity bug having nothing to do with your shader after all. The solution came through this block of assignments:

    Renderer rend = GetComponent[Renderer] ();
    Color initialColor = rend.material.color;

    The Color assignment must take place immediately after the Renderer one, otherwise Color is set to (1,1,1,1).

  7. Thank you so much! I modified your code to have transparency too!

  8. Thank you so much! I modified your code to have transparency too!

  9. I am very much pleased with the contents you have mentioned. I wanted to thank you for this great article. I enjoyed every little bit part of it and I will be waiting for the new updates.
    mobile game developers