将IEnumerator从几秒钟翻译成几分钟?

时间:2018-04-06 02:33:53

标签: unity3d ienumerator

我不确定如何将其翻译成我的脚本。我有一个在玩家上空盘旋的鸟脚本。我将所有内容设置为浮动秒,但我希望我的FadeIn()函数和Ienumerator GameReset()使用1分钟。这将有所帮助,因为我希望玩家有时间击落鸟而不会在几秒钟内自动重置游戏问题。我的游戏的重点是,如果玩家花费太长时间,鸟会攻击它们导致重置窗口出现。

public class OwlEx : MonoBehaviour {


    public float waitTime = 10.0f;
    public float fadeTime = 3.0f;
    public float betweenFadesTime = 2.0f;

    // Flag to determine whether or not the player may respond.
    public bool canRespond = false;

    // Flag to determine if the player has responded within the wait time.
    public bool hasResponded = false;

    public GameObject enemy;

    void Start()
    { StartCoroutine(EnemyFadeIn(fadeTime, betweenFadesTime, waitTime)); }

    void Update() {
        if ((Input.GetKeyDown(KeyCode.S)) && (canRespond))
        { hasResponded = true; }
    }

    IEnumerator EnemyFadeIn(float timeToFade, float timeBetweenFades, float timeToWait) {
        Debug.Log("An Enemy Is Fading In");
        // Simulating Fade In Time.
        yield return new WaitForSeconds(timeToFade);
        // Fade in
         iTween.FadeTo(enemy, 1, 1);
         Invoke("SetMaterialOpaque", 1f);
        Debug.Log("An Enemy Has Appeared");

        yield return ListenForInput(timeToFade, timeBetweenFades, timeToWait);
    }

    IEnumerator EnemyFadeOut(float timeToFade, float timeBetweenFades, float timeToWait) {
        Debug.Log("An Enemy Is Fading Away");
        // Simulating Fade Out Time.
        yield return new WaitForSeconds(timeToFade);
        // Fade out
         SetMaterialTransparent();
         iTween.FadeTo(enemy, 0, 1);
        Debug.Log("An Enemy Has Departed");
        // Simulating Time Between Fades.
        yield return new WaitForSeconds(timeBetweenFades);

        yield return EnemyFadeIn(timeToFade, timeBetweenFades, timeToWait);
    }

    // Responsible for reacting to the 'S' key input.
    IEnumerator ListenForInput(float timeToFade, float timeBetweenFades, float timeToWait) {
        canRespond = true;
        Debug.Log("Press the 'S' Key to Destroy the Enemy!");
        float startTime = Time.time;

        // Check every 0.25 seconds to see if the S key was pressed.
        while (Time.time < (startTime + timeToWait)) {
            if (hasResponded) {
                Debug.Log("The 'S' Key was Pressed!");
                hasResponded = false;
                canRespond = false;
                yield return EnemyFadeOut(timeToFade, timeBetweenFades, timeToWait);
            }
            yield return new WaitForSeconds(0.25f);
        }

        Debug.Log("The 'S' Key was not Pressed!");
        canRespond = false;
        yield return ResetGame();
    }

    IEnumerator ResetGame() {
        Debug.Log("Game is Performing a Reset");
        Simulating Game Reset Time.
        yield return new WaitForSeconds(30);
        Debug.Log("Game has Restarted. End of Coroutine!");
    }
}

1 个答案:

答案 0 :(得分:1)

float waitTimeSeconds = 0.0f;
float waitTimeMinutes = 1.0f;
int formatMinutesFlag = 1;    //this flag is 1 if you want to add minutes
yield return new WaitForSeconds(waitTimeSeconds + formatMinutesFlag * waitTimeMinutes * 60f);

如果你真的不希望每次想要使用分钟而不是秒来乘以60,那么你可以把它放在一个函数中。

编辑:添加了乘以60的部分。感谢您提醒我。