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
}
Answer by Ziya · Mar 07, 2017 at 09:39 AM
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);
}
Your answer
Follow this Question
Related Questions
How to make an Animation layer not set curve parameters? 0 Answers
Unity animation is adding some odd curves/movement to the object 0 Answers
Can Animation Curves be used to control variables? 2 Answers
How to Change (CENTER x ,y,z) of Capsule Collider Animation from Curve ? C# 1 Answer
Why default animation state rewrites scale of an object? 0 Answers