如何在按钮调用多个函数时正确使用IEnumerator

问题描述 投票:0回答:1

我正在制作纸牌游戏,我试图在每张卡实例化之前做出(0.5f)延迟。我有我的代码实例化和对象

public IEnumerator Name(int x,int y, int z){
 }    

在IEnum中我有一个yeild return new WaitForSeconds(0.5f)before所有代码与实例化。

我使用StartCoroutine(Name(...par...));在两个不同的类中调用我的IEnumerator 2次

在我的游戏按钮上,我有4个事件使用枚举产生卡但没有延迟。

有没有办法让卡片逐一出现。

谢谢你的支持。

c# unity3d unityscript coroutine ienumerator
1个回答
2
投票

Whatever it is that is currently calling StartCoroutine needs to be the coroutine.

现在你的代码看起来/行为如下:

StartCoroutine(Name(...par...));
StartCoroutine(Name(...par...));
StartCoroutine(Name(...par...));
StartCoroutine(Name(...par...));

而且他们所有人都在生产一张卡而不是互相等待。您不希望这样,因此您需要对调用方式进行根本性更改,以便您可以获得此行为:

StartCoroutine(SomeMethod(...));

IEnumerator SomeMethod(...) {
    yield return Name(...par...)
    yield return Name(...par...)
    yield return Name(...par...)
    yield return Name(...par...)
}
© www.soinside.com 2019 - 2024. All rights reserved.