유니티 코루틴 관련 함수(대화 관련)

|
출처 : http://www.unitystudy.net/bbs/board.php?bo_table=writings&wr_id=43


코루틴용 데이터

엔진이 수행하는 기능

yield return null

다음 프레임까지 대기

yield return new WaitForSeconds(float)

지정된 초 만큼 대기

yield return new WaitForFixedUpdate()

다음 물리 프레임까지 대기

yield return new WaitForEndOfFrame()

모든 렌더링작업이 끝날 때까지 대기

yield return StartCoRoutine(string)

다른 코루틴이 끝날 때까지 대기

yield return new WWW(string)

웹 통신 작업이 끝날 때까지 대기

yield return new AsyncOperation

비동기 작업이 끝날 때까지 대기 ( 씬로딩 )



순차적 수행 가능







public class DialogExample : MonoBehaviour {

   

    bool showDialog = false;

    string answer = "";

   

    IEnumerator Start()

    {

        yield return StartCoroutine("ShowDialog"); //다른 코루틴이 끝날 때까지 대기

        yield return StartCoroutine(answer);

    }

   //answer 에 값이 들어가기 전까지는 코루틴이 끝나지 않음

    IEnumerator ShowDialog()

    {

        showDialog = true;

        do

        {

            yield return null;

        } while(answer == "");

       

        showDialog = false;

    }

   

    IEnumerator ActionA()

    {

        Debug.Log ("Action A");

        yield return new WaitForSeconds(1f);

    }

 

    IEnumerator ActionB()

    {

        Debug.Log ("Action B");

        yield return new WaitForSeconds(2f);

    }

   

    void OnGUI()

    {

        if(showDialog)

        {

            if(GUI.Button(new Rect(10f, 10f, 100f, 20f), "A"))

            {

                answer = "ActionA";  

            } else if(GUI.Button(new Rect(10f, 50f, 100f, 20f), "B")) {

                answer = "ActionB";

            }

        }

    }

   

}




Trackback 0 And Comment 0