我正在尝试制作一个弹出屏幕,我想停止一些代码,直到点击该弹出屏幕上的按钮是可能的。你能告诉我一些示例代码吗?
答案 0 :(得分:1)
你的协程的代码应如下所示:
IEnumerator MyCoroutine()
{
while(!buttonClickFlag)
{
yield return null;
}
//...
buttonClickFlag = false;
action();
}
当buttonClickFlag设置为true时,将执行操作。
答案 1 :(得分:0)
当然,在Unity 5.3之后,他们添加了WaitUntil类,你也可以使用'while'等待。这是一个例子:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Popup : MonoBehaviour {
public Button button;
bool clicked;
void Start(){
button.onClick.AddListener (ClickButton);
StartCoroutine (WaitUntilForClick ());
}
public void ClickButton(){
clicked = true;
}
IEnumerator WaitUntilForClick(){
#if UNITY_5_3_OR_NEWER
yield return new WaitUntil (() => clicked);
#else
// Here you can cache WaitForEndOfFrame object
WaitForEndOfFrame waitForFrame = new WaitForEndOfFrame();
while(!clicked){
yield return waitForFrame;
}
#endif
Debug.Log ("after click");
}
void Update(){
if(!clicked)
Debug.Log ("waiting for click!");
}
}