协程调用以下方法太多次

时间:2018-11-05 20:45:02

标签: c# unity3d

我正在为我最近的想法尝试一些代码。但是现在,我坚持尝试寻找增加延迟的好方法。我一直在尝试使用协程,但会出现延迟,但是之后调用的方法被调用了太多次(我只希望它被调用一次)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System;
using Random = UnityEngine.Random;

public class YourHealth : MonoBehaviour
{
    bool yourTurn = true;
    public Button button1;
    public Button button2;
    public Text YH;
    public Text EH;
    private int yourHealth = 100;
    private int enemyHealth = 100;

    // Use this for initialization
    void Start()
    {
        button1.onClick.AddListener(Heal20);
        button2.onClick.AddListener(Damage40);
        YH.text = Convert.ToString(yourHealth);
        EH.text = Convert.ToString(enemyHealth);
    }

    public void Heal20()
    {
        yourHealth += 20;
        yourTurn = false;
    }

    public void Damage40()
    {
        enemyHealth -= 40;
        yourHealth -= 5;
        yourTurn = false;
    }

    public void Update()
    {
        YH.text = Convert.ToString(yourHealth);
        EH.text = Convert.ToString(enemyHealth);

        if (yourTurn == false)
        {
            button1.interactable = false;

            StartCoroutine(Wait(2));
        }
        else
        {
            button1.interactable = true;
        }
    }

    public void EnemyTurn()
    {
        int roll = Random.Range(1 , 7);

        switch (roll)
        {
            case 1:
                yourHealth -= 10;
                break;
            case 2:
                enemyHealth -= 10;
                break;
            case 3:
                yourHealth -= 30;
                break;
            case 4:
                yourHealth += 5;
                break;
            case 5:
                break;
            case 6:
                enemyHealth += 10;
                break;
        }

        yourTurn = true;
    }

    IEnumerator Wait(float time)
    {
        yield return new WaitForSecondsRealtime(time);

        EnemyTurn();
    }
}

我希望EnemyTurn在延迟后仅被调用一次。但是相反,它运行了很多次。我希望我能很快找到解决方案,因为它只是使我的项目停顿了,我自己也无法解决。

1 个答案:

答案 0 :(得分:0)

那是因为您要在玩家端与敌人回合之间的每个Wait通话中创建一个Coroutine Update。您需要一种在每个玩家回合结束时只能创建一次的方法。

一种方法是添加一个在创建Wait Coroutine时设置的标志,然后在敌人转弯时将其取消设置:

// If this is true, we are already waiting 
// and don't want more Wait coroutines (yet)
private bool waiting;  

void Start()
{
    button1.onClick.AddListener(Heal20);
    button2.onClick.AddListener(Damage40);
    YH.text = Convert.ToString(yourHealth);
    EH.text = Convert.ToString(enemyHealth);
    waiting = false;
}

// ...

    if (yourTurn == false)
    {
        if (!waiting) 
        {
            // we only want to do this stuff the first frame we start waiting

            button1.interactable = false;
            waiting = true;
            StartCoroutine(Wait(2));
        }
    }
    else
    {
        button1.interactable = true;
    }

// ...

IEnumerator Wait(float time)
{
    yield return new WaitForSecondsRealtime(time);

    EnemyTurn();
    waiting = false;
}