You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为仅含单个动画剪辑的Animator组件播放动画时能否无需指定剪辑名称?

无需指定剪辑名称播放单个动画的Animator方案

当然可以实现,因为每个Animator仅绑定了一个动画剪辑,我们可以直接从Animator的配置中提取这个唯一的剪辑名称,不用手动写死字符串。

核心实现方式

直接通过Animator.runtimeAnimatorController获取控制器内的动画剪辑列表,由于只有一个剪辑,直接取索引0的元素即可:

Animator animator = GetComponent<Animator>();
// 获取唯一的动画剪辑名称
string targetClipName = animator.runtimeAnimatorController.animationClips[0].name;
// 播放动画
animator.Play(targetClipName, -1, 0.25f);

补充说明

  • 这个方案的前提是每个Animator确实只绑定了一个动画剪辑,如果后续某个Animator添加了多个剪辑,建议加个判断animationClips.Length == 1避免索引越界报错。
  • 如果你的Animator使用的是状态机(而非直接绑定剪辑),也可以通过runtimeAnimatorController.layers[0].stateMachine.states[0].state.name获取唯一状态的名称,再传入Play方法。

内容的提问来源于stack exchange,提问作者Nijako

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 00:02:41