유니티로 배우는 c#

코루틴

테오구 2021. 10. 12. 01:02
728x90
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;


public class NewBehaviourScript : MonoBehaviour
{
    Coroutine myCoroutine1;
    Coroutine myCoroutine2;
    // private IEnumerator myCoroutine1; 4번째 방법

    void Start()
    {
        // myCoroutine1 = LoopA(1,2,3,4,5,6)
        myCoroutine1 = StartCoroutine(LoopA());
        StartCoroutine("LoopB");//LoopA와 LoopB가 동시에 실행 문자열이 상대적으로 과부화에 더 걸린다. 최대 파라미터가 한 개
        StartCoroutine(Stop());
    }

    // Update is called once per frame
    IEnumerator LoopA()
    {
        for(int i = 0; i < 100; i++){
            print("i의 값 = " + i);
            yield return new WaitForSeconds(1f);//괄호 안에 시간 만큼 대기 1초대기
        }
    }

    IEnumerator LoopB()
    {
        for(int x = 0; x < 100; x++){
            print("x의 값 = " + x);
            yield return new WaitForSeconds(1f);//괄호 안에 시간 만큼 대기 1초대기
        }
    }
    IEnumerator Stop()
    {
        yield return new WaitForSeconds(2f);//2초뒤에 LoopA를 정지
        StopCoroutine(myCoroutine1);
        //StopAllCoroutines(); 동작중인 모든 것을 정지시킨다.
        yield return new WaitForSeconds(2f);//2초뒤에 LoopB를 정지
        StopCoroutine("LoopB");
    }
}
728x90

'유니티로 배우는 c#' 카테고리의 다른 글

예외 처리  (0) 2021.10.11
Action과 Func  (0) 2021.10.11
람다식  (0) 2021.10.11
형식 매개 변수 T  (0) 2021.10.10
인터페이스  (0) 2021.10.10