How to animate a MeshRenderer color in Unity3D?

I tried to animate MeshRenderer.material.color.a, but the code doesn’t work. Animator starts working, but no change happens. Rendering Mode is set to Transparent and animation works, if created in editor.

Code:

private void Method()
{
    AnimationClip clip = new AnimationClip();
    AnimationClipPlayable playable = AnimationClipPlayable.Create(clip);

    AnimationCurve alpha = AnimationCurve.Linear(0, 1, 1, 0);
    clip.SetCurve("", typeof(MeshRenderer), "Material._Color.a", alpha);

    //gameObject.GetComponent<MeshRenderer>().material.SetColor("_Color", Color.red); // Works
    gameObject.GetComponent<Animator>().Play(playable); // Doesn't work
}

The property name should be “material._Color.a” and you should animate all properties, not only one, to make it work. Otherwise animator will set them all to default.

Code:

    private void Method()
    {
        AnimationClip clip = new AnimationClip();
        AnimationClipPlayable playable = AnimationClipPlayable.Create(clip);     
        AnimationCurve white = AnimationCurve.Linear(0, 0, 1, 1);
 
        clip.SetCurve("", typeof(MeshRenderer), "material._Color.r", white);
        clip.SetCurve("", typeof(MeshRenderer), "material._Color.g", white);
        clip.SetCurve("", typeof(MeshRenderer), "material._Color.b", white);
        clip.SetCurve("", typeof(MeshRenderer), "material._Color.a", white);
 
        gameObject.GetComponent<Animator>().Play(playable);
    }