[英]Using Coroutine in Unity
我正在使用Unity,這就是我想要做的事情:以10秒的時差播放animationType 。 我希望代碼循環播放動畫並每次播放它們10秒。 代碼運行沒有錯誤,除了結果不是我預期的結果。 它播放第一個動畫, 拳擊 ,持續10秒,當它即將播放Backflip動畫時,它開始對角色做一些奇怪的事情。 這就是出錯的地方。
這是我的代碼:
public class BeBot_Controller : MonoBehaviour
{
Animator anim;
string animationType;
string[] split;
int arrayLength;
void Start()
{
//AndroidJavaClass pluginClass = new AndroidJavaClass("yenettaapp.my_bebot_plugin.My_BeBot_Plugin");
//animationType = pluginClass.CallStatic<string>("getMessage");
animationType="Null,Boxing,Backflip";
split = animationType.Split(',');
anim = gameObject.GetComponentInChildren<Animator> ();
arrayLength = split.Length;
}
// Update is called once per frame
void Update () {
if (arrayLength > 1){
StartCoroutine ("LoopThroughAnimation");
}
}
IEnumerator LoopThroughAnimation()
{
for (int i = 1 ; i < arrayLength; i++) {
animationType = split [i];
//anim.SetInteger ("AnimPar", 0);
anim.Play (animationType);
yield return new WaitForSeconds (10);
}
}
}
那我在這里做錯了什么? 還有其他辦法可以解決這個問題嗎?
由於您的動畫循環只需要調用一次,只需將StartCoroutine()
移動到Start()
並刪除Update()
內容:
public class BeBot_Controller : MonoBehaviour
{
private Animator anim;
private string animationType;
private string[] split;
private int arrayLength;
void Start ()
{
//AndroidJavaClass pluginClass = new AndroidJavaClass("yenettaapp.my_bebot_plugin.My_BeBot_Plugin");
//animationType = pluginClass.CallStatic<string>("getMessage");
animationType = "Null,Boxing,Backflip";
split = animationType.Split(',');
anim = gameObject.GetComponentInChildren<Animator>();
arrayLength = split.Length;
// Call here
StartCoroutine(LoopThroughAnimation());
}
IEnumerator LoopThroughAnimation ()
{
for (int i = 1; i < arrayLength; i++)
{
animationType = split[i];
Debug.Log(animationType);
//anim.SetInteger ("AnimPar", 0);
anim.Play(animationType);
yield return new WaitForSeconds(10);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.