Fade between two Texture2d

Is there a way to adjust this code to work with texture 2d? I tried to Change everything to work with Texture2d but no luck with the Lerp. I also changed the code to render.material.mainTexture instead. Can fading between two textures only be done with shaders or something? I saw this also worked with colors. Got this off the Unity 3d website but didn’t notice anything for Texture2d.

// Blends between two materials

var material1 : Material;
var material2 : Material;
var duration = 2.0;

function Start () {
    // At start, use the first material
    renderer.material = material1;
}

function Update () {
    // ping-pong between the materials over the duration
    var lerp : float = Mathf.PingPong (Time.time, duration) / duration;
    renderer.material.Lerp (material1, material2, lerp);
}

You’ll be needing a shader for this. Essentially, the shader will take two diffuse textures and have a float value. A script will change the float value and that value will be used to multiply the two textures as they’re drawn. There are a ton of examples on shaders here:

http://wiki.unity3d.com/index.php/Shaders

You can blend textures via script using GetPixels and SetPixels, but this is slow and requires that both textures be readable (the default is non-readable).

But if you just want to blend two materials in some object, things are easier: add the two materials to the renderer and select a transparent shader to the second material (Transparent/Diffuse, for instance), then lerp between them with color.a of the second material - like this:

var duration = 2.0;

function Update () {
    // ping-pong between the materials 0 and 1 over the duration
    var lerp : float = Mathf.PingPong (Time.time, duration) / duration;
    renderer.materials[1].color.a = lerp;
}

This controls the second material’s transparency - when it’s 0, the first material shows through, and is progressively replaced by the second material as the alpha value approaches 1.

NOTE: Remember to use a transparent shader for the second material, or this won’t work.