본문 바로가기
게임개발/게임플랫폼 응용 프로그래밍

코루틴을 사용하여 Delay 연습하기

by pudding81 2024. 2. 29.

1. 3초동안 대기한후 콘솔에 "3초가 지났습니다" 라고 출력하기

2. 매 1초마다 "1초 경과!" 라고 출력하되 총 10번 반복후 자동으로 중지

 

3. 스페이스바를 누를때까지 대기한후 스페이스바를 누르면 "스페이스바가 눌렸습니다" 출력하기

 

4. 화면을 클릭할때까지 기다며 2초마다 "대기중" 이라고 출력하고 화면을 클릭하면 "완료" 라고 출력하기

 

5. 코루틴내에서 다른 코루틴을 호출 하여 첫번째 코루틴이 3초 대기후 실행되고 두번째 코루틴이 그이후 실행되어 2초후 "작업 완료" 라고 출력 한다

 

 

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class TankController : MonoBehaviour
{
    //코루틴 연습
    private IEnumerator coroutine;


    private void Start()
    {
        //StartCoroutine(CoSol1());
        // StartCoroutine(CoSol2());
        //StartCoroutine(CoSol3());
       // StartCoroutine(CoSol4());
       // StartCoroutine(CoSol5());


    }

    // 1. 3초동안 대기한후 콘솔에 "3초가 지났습니다" 라고 출력하기 
    IEnumerator CoSol1()
    {
        yield return new WaitForSeconds(3.0f); //3초가 지나면 실행 
        Debug.Log("3초가 지났습니다.");

    }

    //2. 매 1초마다 "1초 경과!" 라고 출력하되 총 10번 반복후 자동으로 중지

    IEnumerator CoSol2()
    {
        for (int i = 0; i < 10; i++) //10번 반복
        {
            yield return new WaitForSeconds(1.0f); // 1초가 지나면 실행
            Debug.LogFormat("1초가 지났습니다.{0}/10", i + 1);

        }

    }

    //3. 스페이스바를 누를때까지 대기한후 스페이스바를 누르면 "스페이스바가 눌렸습니다" 출력하기 
    IEnumerator CoSol3()
    {

        while (!Input.GetKeyDown(KeyCode.Space)) //스페이스바를 누르않으면 대기 
        {
            yield return null;
            
        }

        Debug.Log("스페이스바가 눌렸습니다");

    }


    //4. 화면을 클릭할때까지 기다며 2초마다 "대기중" 이라고 출력하고 화면을 클릭하면 "완료" 라고 출력하기 

    IEnumerator CoSol4()
    {
        float wait = 2f;
        float Timer = 0.0f;

        while (!Input.GetMouseButtonDown(0)) //클릭하기전까지 대기
        {
            Timer += Time.deltaTime;
            if (Timer >= wait)
            {
                Debug.Log("대기중");
                Timer = 0.0f; //초기화

            }

            yield return null;
        }

        Debug.Log("완료");

    }


    //5. 코루틴내에서 다른 코루틴을 호출 하여 첫번째 코루틴이 3초 대기후 실행되고
    //두번째 코루틴이 그이후 실행되어 2초후 "작업 완료" 라고 출력 한다 

    IEnumerator CoSol5()
    {
        
       
       yield return StartCoroutine(CoSol1());
          
       yield return new WaitForSeconds(2.0f);
           

       Debug.Log("작업 완료");

    }

}

 

 

* 참고 답안

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class Main : MonoBehaviour

{

    void Start()

    {

        //StartCoroutine(this.Sol1());

        //StartCoroutine(this.Sol2());

        //StartCoroutine(this.Sol3());

        //StartCoroutine(this.Sol4());

        //StartCoroutine(this.Sol5());

    }



    //1. 5초동안 대기한후 콘솔에 "5초가 지났습니다" 라고 출력하기 

    IEnumerator Sol1()

    {

        yield return new WaitForSeconds(5.0f);

        Debug.Log("5초가 지났습니다.");

    }



    //2. 매 1초마다 "1초 경과!" 라고 출력하되 총 10번 반복후 자동으로 중지

    IEnumerator Sol2()

    {

        int cnt = 0;

        while (cnt < 10) 

        {

            yield return new WaitForSeconds(1); 

            Debug.Log("1초 경과!");

            cnt++; 

        }

    }



    //3. 스페이스바를 누를때까지 대기한후 스페이스바를 누르면 "스페이스바가 눌렸습니다" 출력하기 

    IEnumerator Sol3()

    {

        yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.Space));

        Debug.Log("스페이스바가 눌렸습니다.");

    }



    //4. 화면을 클릭할때까지 기다며 2초마다 "대기중" 이라고 출력하고 화면을 클릭하면 "완료" 라고 출력하기

    IEnumerator Sol4()

    {

        float waitTime = 2f; 

        float timer = 0f; 



        while (!Input.GetMouseButtonDown(0))

        {

            timer += Time.deltaTime; 



            if (timer >= waitTime)

            {

                Debug.Log("대기중");

                timer = 0f; 

            }



            yield return null; 

        }



        Debug.Log("완료");

    }



    //5. 코루틴내에서 다른 코루틴을 호출 하여 첫번째 코루틴이 3초 대기후 실행되고 두번째 코루틴이 그이후 실행되어 2초후 "작업 완료" 라고 출력 한다

    IEnumerator Sol5()

    {

        yield return new WaitForSeconds(3.0f);

        Debug.Log("작업완료1");

        yield return StartCoroutine(Sol5_1());

        Debug.Log("작업완료2");

    }



    IEnumerator Sol5_1()

    {

        yield return new WaitForSeconds(2.0f);

    }



}

'게임개발 > 게임플랫폼 응용 프로그래밍' 카테고리의 다른 글

Coroutine 코루틴  (0) 2024.03.01
시즈탱크 1단계 코드 수정  (0) 2024.02.29
시즈탱크 2단계  (0) 2024.02.29
시즈탱크 1단계 - 에러 수정전  (1) 2024.02.29
벌쳐 지뢰 매설하기  (0) 2024.02.28