gpt4 book ai didi

c# - 在 Unity 中使用协程

转载 作者:太空狗 更新时间:2023-10-29 13:49:39 26 4
gpt4 key购买 nike

我正在使用 Unity,这就是我想要做的:以 10 秒的时差播放 animationType。我希望代码循环播放动画并每个播放 10 秒。代码运行没有错误,除了结果不是我预期的那样。它播放第一个动画拳击,持续 10 秒,就在它要播放后空翻 动画时,它开始对角色做一些奇怪的事情。这就是问题所在。

这是我的代码:

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);
}
}
}

关于c# - 在 Unity 中使用协程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49788402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com