게임개발 예제모음

메소드 지연 호출/실행 본문

유니티 메모장

메소드 지연 호출/실행

게임을만들어보자 2025. 6. 3. 11:23

○ invoke

ExamScript01.cs
-------------
public class ExamScript01 : MonoBehaviour
{
    void ExamMethod001()
    {
        Invoke("ReferenceMethod", 10f)
        // " "사이에 호출하려고 하는 메소드 이름을 직접 입력
        // 딜레이 시키려는 시간을 float값으로 입력
    }

    void ReferenceMethod()
    {
        Debug.Log("10초 지연 실행");
    }
}

 

○ Coroutine

ExamScript02.cs
-------------
public class ExamScript02 : MonoBehaviour
{
    void ExamMethod001()
    {
        StartCoroutine(ReferenceMethod());
        // 코루틴 메소드호출
    }

    IEnumerator ReferenceMethod()
    {
        yield return new WaitForSeconds(10f);
        // float값으로 지연시킬 시간 지정
        
        Debug.Log("10초 지연 실행");
    }
}

'유니티 메모장' 카테고리의 다른 글

Light컴포넌트 불빛 깜빡이기  (0) 2025.06.03
Sin함수를 이용한 반복운동  (0) 2025.06.03
씬 호출  (0) 2025.06.03
씬 인덱스 읽어오기  (0) 2025.06.03
다른 스크립트의 변수/메소드 호출  (0) 2025.06.03