有以下方法:
public void AddFocusEffect(AnimationClip focusClip, GameObject[] focusElements)
{
// AnimationClip focus = Resources.Load("Focus", typeof(AnimationClip)) as AnimationClip;
foreach (GameObject focusElement in focusElements)
{
focusElement.AddComponent<Outline>();
Animation anim = focusElement.AddComponent<Animation>();
anim.clip = focusClip;
}
}
focusClip 通过检查器挂起(因为注释掉的代码由于某种原因不起作用),结果,所有组件都正确创建,看起来好像我们自己添加和更正了它们,但不起作用(不像通常情况下)。
为什么会出现这种行为?

Animation 组件已经被弃用了很长一段时间,并且将在新版本中被删除。
相反,最好使用带有Animator的方案,通过协程或各种孪生实现小动画。
本质上是在分配剪辑之后添加一行。自动播放
anim.Play();仅在Start()时存在剪辑时才有效。