UnityAPI手册-2019.4官网版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
Animation.Play
# [Animation](Animation.html).Play public bool **Play** ([PlayMode](PlayMode.html) **mode**= PlayMode.StopSameLayer); public bool **Play** (string **animation**, [PlayMode](PlayMode.html) **mode**= PlayMode.StopSameLayer); ## 描述 播放没有混合的动画。 如果没有提供名称,则将播放默认动画。在无法播放该动画的情况下 (例如,当没有默认动画或没有指定名称的动画时),该函数将返回 false。通过可选 [PlayMode](PlayMode.html) 可选择该动画会对其他已播放动画产生的影响。 如果该指定动画已经在播放,则其他动画将停止,但该动画不会倒回到开头。当已到达该动画的末尾时,其将自动停止并倒回到开头,除非已将 [PlayMode](PlayMode.html) 设置为 Loop。如果在帧更新期间在某个对象上调用了 [Animation.Play](Animation.Play.html),并且在这期间还[停用](../Manual/DeactivatingGameObjects.html)了此对象,则相当于取消此调用。当稍后重新激活此对象时,该动画将不会开始播放。但如果在后续帧上进行此调用(当此对象仍处于非活动状态时),则在重新激活后该动画将开始播放。 要使用 [Animation.Play](Animation.Play.html),动画数据必须在 Inpsector 窗口中显现。此窗口以数组的形式包含了 [GameObject](GameObject.html) 的所有动画。例如,两个动画 `jump` 和 `spin` 存储在动画列表中。[Animation.Play](Animation.Play.html) 可以分别播放这两个动画中的每一个。[Animation](Animation.html) 也可以组合这两个动画。对此使用了 [AnimationState](AnimationState.html).`jump`(不受支持且无相关文档)。例如,将 `jump` 置于第 0 层并将 `spin` 移到第 123 层,这样将允许它们一起播放。 动画在检视面板中必须标记为“Legacy”才能被此方法发现。将 Inspector 窗口切换为“Debug”后,会显示此选项。 ``` using UnityEngine; // Animation.Play example. Let the S and J keys start // a spin or jump animation. Let Space play back spin and // jump at the same time. Let Z play spin and jump with // spin doubled in speed. // // Spin: rotate the cube 360 degrees in half or one second // Jump: bounce up to 2 units and down in one second // // Note: AnimationState.layer is no longer supported, but still exists. public class ExampleScript : MonoBehaviour { private Animation anim; void Start() { anim = gameObject.GetComponent<Animation>(); anim["spin"].layer = 123; } // double the spin speed when true private bool fastSpin = false; void Update() { // leave spin or jump to complete before changing if (anim.isPlaying) { return; } if (Input.GetKeyDown(KeyCode.S)) { Debug.Log("Spinning"); anim.Play("spin"); } if (Input.GetKeyDown(KeyCode.J)) { Debug.Log("Jumping"); anim.Play("jump"); } // combine jump and spin if (Input.GetKeyDown(KeyCode.Space)) { Debug.Log("Jumping and spinning"); anim.Play("jump"); anim.Play("spin"); } // have spin speed reverted to 1.0 second if (fastSpin == true) { anim["spin"].speed = 1.0f; fastSpin = false; } if (Input.GetKeyDown(KeyCode.Z)) { Debug.Log("Jumping and spinning in half a second"); anim.Play("jump"); anim["spin"].speed = 2.0f; anim.Play("spin"); // we've used spin at a speed of two, now mark // the spin speed to revert back to one fastSpin = true; } } } ```
da
2022年5月23日 22:34
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码