본문 바로가기
Unity

코루틴 WaitUntil

by imagineer_jinny 2021. 8. 5.

yield return new WaitUntil(() => 조건);

괄호 안의 조건이 true가 되면 구문 실행

 

https://docs.unity3d.com/ScriptReference/WaitUntil.html

 

Unity - Scripting API: WaitUntil

 

docs.unity3d.com

 

- Suspends the coroutine execution until the supplied delegate evaluates to true.

- WaitUntil can only be used with a yield statement in coroutines.

- Supplied delegate will be executed each frame after script MonoBehaviour.Update and before MonoBehaviour.LateUpdate.

- When the delegate finally evaluates to true, the coroutine will proceed with its execution.

using UnityEngine;
using System.Collections;

public class WaitUntilExample : MonoBehaviour
{
    public int frame;

    void Start()
    {
        StartCoroutine(Example());
    }

    IEnumerator Example()
    {
        Debug.Log("Waiting for princess to be rescued...");
        yield return new WaitUntil(() => frame >= 10);
        Debug.Log("Princess was rescued!");
    }

    void Update()
    {
        if (frame <= 10)
        {
            Debug.Log("Frame: " + frame);
            frame++;
        }
    }
}

 

 


WaitUntil | 아이군의 블로그 (theeye.pe.kr)

 

WaitUntil | 아이군의 블로그

유니티에서 사용되는 코루틴(Coroutine)은 왜 필요한가? 유니티에서 화면의 변화를 일으키기 위해서는 Update() 함수 내에서 작업을 하게 됩니다. 이 Update() 함수는 매 프레임을 그릴때마다 호출되며

theeye.pe.kr

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RotateManager : MonoBehaviour
{
    private float speed = 3f;
  
    bool Rotator(float x, float y, float z)
    { 
        transform.Rotate(new Vector3(x,y,z),Space.World);

        if (Input.GetButtonUp("Fire1"))
            return true;
        
        else
            return false;
     
    }

    public void rotateX(float x)
    {
        StartCoroutine(coRot(x, 0, 0));
    }

    public void rotateY(float y)
    {
        StartCoroutine(coRot(0, y, 0));
    }
    public void rotateZ(float z)
    {
        StartCoroutine(coRot(0, 0, z));
    }

    IEnumerator coRot(float x, float y, float z)
    {
        yield return new WaitUntil(() => Rotator(
            10f *speed *Time.deltaTime*x,
            10f * speed * Time.deltaTime * y,
            10f * speed * Time.deltaTime * z));
    }

}

 

'Unity' 카테고리의 다른 글

Photon을 이용한 네트워크 프로그래밍  (0) 2021.08.17
ML-Agents : 에이전트, 브레인, 아카데미  (0) 2021.08.10
오디오 믹싱 + 최종 빌드  (0) 2021.07.22
게임 매니저, PlayerPrefs  (0) 2021.07.22
카메라 추적  (0) 2021.07.22

댓글