播放并等待animation/animation师完成播放

在我的脚本中,当玩家在平台的顶部时,我会这样做。 这工作正常。 但是现在我想让它一旦起来,就播放剪辑“Down”。

using UnityEngine; using System.Collections; using System.Reflection; public class DetectPlayer : MonoBehaviour { GameObject target; public void ClearLog() { var assembly = Assembly.GetAssembly(typeof(UnityEditor.ActiveEditorTracker)); var type = assembly.GetType("UnityEditorInternal.LogEntries"); var method = type.GetMethod("Clear"); method.Invoke(new object(), null); } void OnCollisionEnter(Collision collision) { if (collision.gameObject.name == "Platform") { Debug.Log("Touching Platform"); } } void OnTriggerEnter(Collider other) { if (other.gameObject.name == "OnTop Detector") { Debug.Log("On Top of Platform"); GameObject findGo = GameObject.Find("ThirdPersonController"); GameObject findGo1 = GameObject.Find("Elevator"); findGo.transform.parent = findGo1.transform; target = GameObject.Find("Elevator"); target.GetComponent<Animation>().Play("Up"); } } } 

在线后

 target.GetComponent<Animation>().Play("Up"); 

我想当它完成播放下来玩:

 target.GetComponent<Animation>().Play("Down"); 

虽然这两个答案应该工作,另一种做法与协程和IsPlayingfunction。 如果您还想在animation之后执行其他任务,则使用协程解决scheme。

对于Animation系统

旧的Unityanimation播放系统。 除非您仍然使用旧的Unity版本,否则应该在您的新项目中使用它。

 IEnumerator playAndWaitForAnim(GameObject target, string clipName) { Animation anim = target.GetComponent<Animation>(); anim.Play(clipName); //Wait until Animation is done Playing while (anim.IsPlaying(clipName)) { yield return null; } //Done playing. Do something below! Debug.Log("Done Playing"); } 

对于Animator系统

这是新的Unityanimation播放系统。 这应该在您的新项目中使用 ,而不是Animation API。

 IEnumerator playAndWaitForAnim(GameObject target, string stateName) { int animLayer = 0; Animator anim = target.GetComponent<Animator>(); anim.Play(stateName); //Wait until Animator is done playing while (anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName) && anim.GetCurrentAnimatorStateInfo(animLayer).normalizedTime < 1.0f) { //Wait every frame until animation has finished yield return null; } //Done playing. Do something below! Debug.Log("Done Playing"); } 


针对碰撞callback函数( OnTriggerEnter )这个特殊问题的解决scheme,有两种可能的方法来做到这一点:

1.启动协同函数,在触发检测后播放animation:

 void OnTriggerEnter(Collider other) { if (other.gameObject.name == "OnTop Detector") { Debug.Log("On Top of Platform"); GameObject findGo = GameObject.Find("ThirdPersonController"); GameObject findGo1 = GameObject.Find("Elevator"); findGo.transform.parent = findGo1.transform; target = GameObject.Find("Elevator"); StartCoroutine(playAnim(target)); } } IEnumerator playAnim(GameObject target) { Animation anim = target.GetComponent<Animation>(); anim.Play("Up"); //Wait until Up is done Playing the play down while (anim.IsPlaying("Up")) { yield return null; } //Now Play Down anim.Play("Down"); } 

要么

2.使用OnTriggerEnter函数一个协程(IEnumerator)而不是void函数:

 IEnumerator OnTriggerEnter(Collider other) { if (other.gameObject.name == "OnTop Detector") { Debug.Log("On Top of Platform"); GameObject findGo = GameObject.Find("ThirdPersonController"); GameObject findGo1 = GameObject.Find("Elevator"); findGo.transform.parent = findGo1.transform; target = GameObject.Find("Elevator"); Animation anim = target.GetComponent<Animation>(); anim.Play("Up"); //Wait until Up is done Playing the play down while (anim.IsPlaying("Up")) { yield return null; } //Now Play Down anim.Play("Down"); } } 

一种不需要手动检查的方法就是使用排队

 target.GetComponent<Animation>().PlayQueued("Down", QueueMode.CompleteOthers); 

此代码将等待当前在对象上播放的任何其他animation在播放排队的animation之前完成。

关于这个主题的Unity API页面