팝업 화면을 만들려고하는데이 팝업 화면에서 버튼을 클릭 할 때까지 일부 코드를 중지하고 싶습니다. 몇 가지 예제 코드를 보여 주시겠습니까?단추가 단일 3D로 눌러 질 때까지 Unity에서 공동 작업을 일시 중지 할 수 있습니까?
0
A
답변
1
당신의 코 루틴의 코드는 다음과 같아야합니다 buttonClickFlag이 작업이 실행되는 true로 설정되어
IEnumerator MyCoroutine()
{
while(!buttonClickFlag)
{
yield return null;
}
//...
buttonClickFlag = false;
action();
}
.
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!");
}
}
코 루틴을 "일시 중지"한다는 의미는 아닙니다. 코 루틴 (coroutine)은 "프로세스 (process)"또는 이와 유사한 어떤 것과도 전혀 관련이 없습니다. *** 게임 엔진의 모든 프레임 ***이 코드를 실행한다는 것입니다. 게임 엔진은 프레임 기반이며 coroutines은 그대로 "프레임에 액세스하는 방법"입니다. – Fattie
일시 중지 하시겠습니까? 일단 버튼을 클릭하면 코 루틴을 시작하지 않는 이유는 무엇입니까? 그리고 ** ** 일부 코드 **를 중단하는 것이 무엇을 의미합니까? 코드 좀 보여줘, 제발. – zwcloud