收益回报新的WaitForSeconds(2)销毁函数 [英] yield return new WaitForSeconds(2) destroys function

查看:156
本文介绍了收益回报新的WaitForSeconds(2)销毁函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定创建一个类似游戏的内存,以学习使用unity3d进行游戏开发.
在玩家点击卡片后,游戏应等待2秒钟,然后再将卡片翻转回原处.
yield return new WaitForSeconds(2)语句对此应该是完美的,但是它的作用是不执行该函数的任何行.

I decided to create a little memory like game, to learn game development with unity3d.
The game should wait 2 seconds after a player clicked to cards, before it flips the cards back.
The yield return new WaitForSeconds(2)-statement should be perfect for this, but it has the effect, that no line of the function is executed.

这是我的代码:

这将建立卡片网格(带有按钮),并在单击卡片时调用翻转卡片的功能.

This builds the card grid (with buttons) and calls a function to flip the card, when the card is clicked.

Card card = grid[i, j];

if (GUILayout.Button(new GUIContent((Texture) Resources.Load(card.getImg()), ""), GUILayout.Width(cardWidth))) {
  Debug.Log("Call FlipCard");
  FlipCardFaceUp(card);
  Debug.Log("Returned from FlipCard");
}

这是翻转功能:

System.Collections.IEnumerable FlipCardFaceUp(Card card) {
    Debug.Log("This isn't shown in the console");
    card.isFaceUp = true;

    if (!cardsFlipped.Contains(card)) {
        cardsFlipped.Add(card);

        if (cardsFlipped.Count >= 2) {
            playerCanClick = false;
            //Waiting 2 seconds before the cards are flipped back or are removed
            yield return new WaitForSeconds(2);

            if (cardsFlipped[0].id == cardsFlipped[1].id) {
                cardsFlipped[0].isMatched = true;
                cardsFlipped[1].isMatched = true;
            } else {
                cardsFlipped[0].isFaceUp = false;
                cardsFlipped[1].isFaceUp = false;
            }

            cardsFlipped.Clear();
            playerCanClick = true;
        } 
    }
}

这是控制台输出,当我测试游戏并单击卡片时:

This is the console output, when I test my game and click on a card:

Call FlipCard
Returned from FlipCard

如果我删除了yield return所需的东西,则一切正常(除了玩家看不到第二张牌,因为第二张牌会立即向后翻转).

If I remove the stuff that is needed for the yield return, works everything fine (except that the player can't see the second card, because it is flipped back immediately).

我的收益率出了什么问题?

What's wrong with my yield return?

推荐答案

您需要使用

You need to start the coroutine by using StartCoroutine:

StartCoroutine(FlipCardFaceUp(card));

正如Marc Gravell所说的,需要枚举迭代器块才能执行任何操作,而这正是StartCoroutine的工作(异步执行,并且可能还需要其他一些东西). 异步"部分也是为什么在协程完成之前立即打印"Returned from FlipCard"的原因.

As Marc Gravell said, an iterator block needs to be enumerated to do anything, and that's what StartCoroutine does (asynchronously, and probably other stuff needed by unity). The "asynchronously" part is also why "Returned from FlipCard" is immediately printed before the Coroutine has finished.

如果要在打印"Return from FlipCard"前等待协程完成,也可以使用yield:

If you want to wait for your Coroutine to finish before printing "Return from FlipCard", you can use yield, too:

Debug.Log("Call FlipCard");
yield return StartCoroutine(FlipCardFaceUp(card));
Debug.Log("Returned from FlipCard");

请注意,您不能在UpdateFixedUpdate中使用yield.

Note that you can't use yield from within Update or FixedUpdate.

看到您正在尝试使用OnGUI方法执行此操作,更好的方法可能是仅在其中使用类型为Card的变量,以便您可以记住要翻转的卡.然后,在Update中,您可以检查该变量,如果该变量指示需要翻转卡,则可以启动协程.

Seeing that you are trying to do this in the OnGUI method, a better way might be to just use a variable of type Card there so that you can remember which card to flip. Then, in Update, you can check that variable, and if it says that a card needs to be flipped, you can start the coroutine.

这篇关于收益回报新的WaitForSeconds(2)销毁函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆