본문 바로가기

C#

코루틴,

 

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

public class CoroutineTest : MonoBehaviour
{

    //Co routine 협력하는 루틴

    IEnumerator timeCo;

    // Start is called before the first frame update
    void Start()
    {
        //1번 미리 인스턴스를 빼서 넣어두는 방식//
        timeCo = DelayTimeCo(0.2f);
        StartCoroutine(timeCo);     //IEnumerator 리턴타입을 가진 함수를 매개변수로 쓸때,

        //2번 그냥 함수를 넣는 방식//
        //StartCoroutine(DelayTimeCo(0.2f));
        
        //3번 함수의 이름으로 넣는 방식//
        //StartCoroutine("DelayTimeCo",0.2);    //string methodName 을 통해 처리할 때
    }

    IEnumerator DelayTimeCo(float delayTime)
    {
        while(true)
        {
            yield return new WaitForSeconds(delayTime);
            Debug.Log(delayTime +"초 지남");
        } 
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Escape))
        {
            //1번 스탑//
            StopCoroutine(timeCo);

            //2번 스탑//
            //StopCoroutine(DelayTimeCo(0.2f));

            //3번 스탑//
            //StopCoroutine("DelayTimeCo");
        }
    }
}

 

교수님꼐선 가급적이면 StopCoroutine할거면 인스턴스 빼서 넣는 방식 사용하는 게 좋을거라 하신다.

'C#' 카테고리의 다른 글

Interface  (0) 2022.06.24
반복자, 캡슐화  (0) 2022.06.23
type  (0) 2022.06.23
Deep Copy  (0) 2022.06.22
Parameter / Arguments 차이  (0) 2022.06.22