private IEnumerator waitForKeyPress(KeyCode key, GUIDialogue win = null) { bool done = false; float coolDown = 1f; while (!done) // essentially a "while true", but with a bool to break out naturally { if (win != null) { if (coolDown >= 1 && Input.GetKeyDown(key) && win.CheckAnimateCoroutine() == false) { done = true; // breaks the loop coolDown = 0f; } yield return(null); // wait until next frame, then continue execution from here (loop continues) } else { if (coolDown >= 1 && Input.GetKeyDown(key)) { done = true; // breaks the loop coolDown = 0f; } yield return(null); // wait until next frame, then continue execution from here (loop continues) } coolDown += Time.deltaTime; } // now this function returns }
/// <summary> /// Play a sequence of dialogue /// </summary> /// <param name="index"> The start id of dialogues </param> /// <returns></returns> public IEnumerator PlayDialogue(int index) { GUIDialogue dialogueWin = (GUIDialogue)GUIManager.Singleton.Open("DialogueUI"); dialogueWin.GetComponent <Canvas>().worldCamera = Camera.main; dialogueWin.CurrentTimelineManager = currentTimelineManager; if (CheckDialogueType(index) == DialogueType.Normal) { dialogueWin.DisplayDialogue(dialogues[index].Text, dialogues[index].Actor); } else { yield break; } yield return(null); while (dialogues[index].Next != "-1") { Debug.Log(LogUtility.MakeLogStringFormat("DialogueManager", "Enter Loop")); //wait for next line yield return(waitForKeyPress(KeyCode.G)); if (dialogueWin.CheckAnimateCoroutine()) { dialogueWin.SetSkip(true); yield return(waitForKeyPress(KeyCode.G, dialogueWin)); dialogueWin.SetSkip(false); } int nextDialogue = Convert.ToInt32(dialogues[index].Next); if (CheckDialogueType(index) == DialogueType.Normal) { dialogueWin.DisplayDialogue(dialogues[nextDialogue].Text, dialogues[nextDialogue].Actor); } else { yield break; } index = nextDialogue; } yield return(waitForKeyPress(KeyCode.G, dialogueWin)); GUIManager.Singleton.Close("DialogueUI"); if (currentTimelineManager.CheckEndState()) { currentTimelineManager.OnTimelineEnd("Moving"); } DialogueEnd.Invoke(); }