- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个非常简单的对话系统。当玩家获得一个对话框时,该框会一直保留到他们按下某个键为止。然后出现第二个对话框。他们再次按下一个键,依此类推。
但是,我意识到我使用协程的方法行不通,因为每个对话框都会同时显示。使用简化版本:
IEnumerator Test() {
//Code that displays a message
yield return StartCoroutine(WaitForKey(KeyCode.Space));
}
IEnumerator WaitForKey(KeyCode keyCode)
{
while (!Input.GetKeyDown(keyCode))
yield return null;
}
void start(){
StartCoroutine(Test());
StartCoroutine(Test());
}
上面代码的结果是显示了两条消息。此外,它们会立即显示——一旦满足条件,进程就会从该协程跳转到启动程序,运行下一行代码,并定期返回到第一个协程以查看它是否已完成。
如何让一个协程在继续执行其他代码之前完成?
最佳答案
解决此问题的简单方法是创建 Canvas 数组作为对话框。在编辑器中禁用所有这些。使用下面的代码,您可以更改 dialogueGameObjectCanvas 的数组大小 为您有多少对话,然后按顺序分配每个对话的父级到编辑器中的 dialogueGameObjectCanvas 数组。 Cancas 必须是每个对话的父对象。运行。
按空格键将进入下一个对话。这是最简单的方法。
现在,以专业的方式。如果对话是在运行时期间生成的,您可以使用List添加或删除对话框(GameObjectUICanvas)而不是使用数组来完成它。您可以轻松地将数组转换为列表,这应该适用于动态生成的对话。
startDisplayDialogue()
开始对话。
isDialogueDisplaying()
告诉您对话当前是否显示在屏幕上。
getDialogueCurrentNumberDisplaying()
获取当前对话显示数组数
stopDisplayDialogue()
终止整个对话并关闭它们。
记住startDisplayDialogue()
是协程函数,必须用StartCoroutine(startDisplayDialogue());
public GameObject[]dialogueGameObjectCanvas;
bool isRunning = false;
int dialogNumberDisplaying = 0;
// Use this for initialization
void Start ()
{
//animationSprite = new GameObject[5]; Uncomment if you want to assign through code, otherwise assign the dialogues from the Editor
StartCoroutine (startDisplayDialogue ());
}
//Display the first dialogue then wait for the space to be pressed then display the next dialue
IEnumerator startDisplayDialogue ()
{
//Make sure there is only one instance of the dialuge functionr running. Break if there is alread one running
if (isRunning) {
yield break;
} else {
isRunning = true;
}
int dialogueNumber = 0; //Starts from 0 to the size of the animationSprite array
dialogNumberDisplaying = dialogueNumber;
while (isRunning) {
//Stop if the dialogNumber is >= number of dialogues to dispaly(dialogue arrays)
if (dialogueNumber >= dialogueGameObjectCanvas.Length) {
isRunning = false;
//dialogueGameObjectCanvas [dialogueNumber].SetActive (false);
Debug.Log ("End of dialogue");
yield break;
}
//Dispaly the current Dialogue based on the array number
dialogueGameObjectCanvas [dialogueNumber].SetActive (true);
//Wait until the Space bar is pressed
while (!Input.GetKeyDown(KeyCode.Space)) {
yield return null; //Must wait here or else Unity would freeze
}
//Space bar has been pressed. Disable the current Dialogue then increement dialogueNumber so that then the next one in the array will display
dialogueGameObjectCanvas [dialogueNumber].SetActive (false);
dialogueNumber++;
dialogNumberDisplaying = dialogueNumber; //For the getDialogueCurrentNumberDisplaying function
//Check if stopDisplayDialogue is called then exit
if (!isRunning) {
dialogueGameObjectCanvas [dialogueNumber].SetActive (false);
yield break;
}
yield return null;
}
}
//Check if a dialogue is currently displaying
bool isDialogueDisplaying ()
{
return isRunning;
}
//Get the current array number of dialogue currently displaying
int getDialogueCurrentNumberDisplaying ()
{
return dialogNumberDisplaying;
}
//Stops the dialogue from displaying
void stopDisplayDialogue ()
{
isRunning = false;
}
关于c# - 有没有办法在满足条件之前留在协程中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30261256/
这是我的jade雕像: section#entry-review-template.template(data-class='entry-review') table thead
我是一名优秀的程序员,十分优秀!