团结 |如何播放存储在变量中的动画(无需将它们放在动画制作器中)

Unity | How to play animations that are stored in a variable (without having them in the animator)

我正在制作一个 2D 格斗游戏,玩家可以在其中选择多个战士。战士有不同的攻击和不同的动画。

每次攻击的数据(伤害、命中框、施法时间、动画等)都存储在可编写脚本的对象中,然后由玩家脚本触发。到目前为止一切正常,但我无法找到如何从代码播放动画而不需要它在动画师中。

我已经尝试了在这里找到的几种解决方案,但它们似乎最终不得不退回到将动画放入动画制作器中。

but I can't find out how to play an animation from code without needing it to be in the animator.

您将需要 Animator-Component 来 运行 动画。

但是你可以指定你想在运行时间播放的动画。为此,您需要 RuntimeAnimationController 来保存 AnimatorController-对象并将其分配给您的 Animator(在代码中)。

您不一定需要 中所述的 Animator 组件。

还有 Animation component. It is marked as legacy 但仍然非常强大,尤其是当您不需要配置整个状态机但只想简单地播放单个 AnimationClip 时。这可能是为什么它仍然进入较新的 Unity 版本的原因:

public AnimationClip clip;

private void Awake()
{
    var animation = GetComponent<Animation>();
    animation.clip = clip;
    animation.Play();
}

另见 Animation.Play