0
\$\begingroup\$

I am creating Animator OverrideController via script based on current controller assigned to animator component. I have created animation curve for position and it works. When i try to animate transparency (material color alpha) it does not work (it does not make any effect, however in Animation Dopesheet there is yellow line: "Object name : Material.Color.a (Missing!)"). Did I fail to properly access this property or is it the consequence that creating animation via script works only for legacy animations with Animation component?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimationScript : MonoBehaviour {

    void Start()
    {
        AnimationClip clip = new AnimationClip();

        AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, transform.position.x, 1.0f, transform.position.x);
        clip.SetCurve("", typeof(Transform), "localPosition.x", curve);

        curve = AnimationCurve.Linear(0.0f, 1.0f, 1.0f, 0.0f);
        clip.SetCurve("", typeof(Material), "_Color.a", curve);

        Animator anim = GetComponent<Animator>();
        AnimatorOverrideController animatorOverrideController = new AnimatorOverrideController(anim.runtimeAnimatorController);
        // "loop" is the name of clip not name of state
        animatorOverrideController["loop"] = clip;
        anim.runtimeAnimatorController = animatorOverrideController;
    }

}

However analogous code for legacy Animation component works:

void Start()
{
    Animation anim = GetComponent<Animation>();
    AnimationCurve curve;

    // create a new AnimationClip
    AnimationClip clip = new AnimationClip();
    clip.legacy = true;

    // update the clip to a change color
    curve = AnimationCurve.Linear(0.0f, 1.0f, 2.0f, 0.0f);
    clip.SetCurve("", typeof(Material), "_Color.a", curve);

    // now animate the GameObject
    anim.AddClip(clip, clip.name);
    anim.Play(clip.name);
}

What am I missing and why is first script not working?

\$\endgroup\$
  • \$\begingroup\$ Please be more descriptive than "it does not work". Does it remain the same alpha as before? Does it immediately change to the new alpha? Does it crash your game? There is no way for us to know. \$\endgroup\$ – Ruzihm Dec 21 '18 at 17:01
  • \$\begingroup\$ I have added some description to question. \$\endgroup\$ – hal Dec 22 '18 at 12:22
1
\$\begingroup\$

Take a look at this. I'm not sure 100% but if you create a 'Color' in code, that is taken the same as your original color, then set the curve value to that color each frame, and finally set this as the color for "_Color". So perhaps change from using SetCurve to SetColor.

(Note: This doesn't answer your question exactly, but i still think it demonstrates a way to fix the problem):

ALSO: I expect you already have it set, but ensure the Material's Rendering Mode (in Inspector) is set to "Transparent" (it defaults as Opaque I think)

 Color transparentColor;

void Start(){
    Material mat = GetComponent<Renderer>().material;
    Color transparentColor = mat.color;
}

void Update(){
   // ... your code to set correct value to 'curve' here
   transparentColor.a = curve;

   mat.SetColor("_Color", transparentColor);

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.