๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿง‘‍๐Ÿ’ป ์ฝ”๋”ฉ/Unity

[Practice Unity] ์ฝ”๋ฃจํ‹ด(Coroutine) ์‚ฌ์šฉ๋ฒ• ์ •๋ฆฌ

728x90

1. ์ฝ”๋ฃจํ‹ด์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

์ฝ”๋ฃจํ‹ด์€ ์–ด๋–ค ํ–‰๋™์— ๋”œ๋ ˆ์ด๋ฅผ ์ฃผ๊ฑฐ๋‚˜ ์ ์ง„์ ์œผ๋กœ ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

Update ๋ฌธ์€ ํ”„๋ ˆ์ž„ ๋‹จ์œ„๋กœ ์‹คํ–‰์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— Update ๋ฌธ ๋‚ด์—์„œ ์ ์ง„์ ์œผ๋กœ ๋ณ€ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค๊ณ  ํ•ด์„œ ์›ํ•˜๋Š” ๋Œ€๋กœ ์‹คํ–‰๋˜์ง€ ์•Š๊ณ  ํ•œ ํ”„๋ ˆ์ž„ ๋งŒ์— ์ž‘๋™ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์งœ๋ฉด ํ•œ ํ”„๋ ˆ์ž„ ๋งŒ์— 3๊ฐœ์˜ prefab ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ์ƒ์„ฑ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
void Update()
{
if (Input.GetKeyDown("s"))
    {
Spawn();
}
}
 
void Spawn()
{
   for(int i=0; i<3; i++)
   {
       Instantiate(prefab, gameObject.transform.position, gameObject.transform.rotation);
   }
}
cs

 

2. ์ฝ”๋ฃจํ‹ด ์‚ฌ์šฉ๋ฒ•

๊ทธ๋ ‡๋‹ค๋ฉด ์ฝ”๋ฃจํ‹ด์„ ์‚ฌ์šฉํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ์ ์ง„์ ์œผ๋กœ ์‹คํ–‰๋˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์ฝ”๋ฃจํ‹ด ์‚ฌ์šฉ๋ฒ•์„ ์„ค๋ช…ํ•˜๋ฉด์„œ ๊ฐ™์ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๋Š” ์ฝ”๋ฃจํ‹ด์„ ์ด์šฉํ•˜์—ฌ 1์ดˆ์— ํ•œ ๋ฒˆ์”ฉ ์ด 3๊ฐœ์˜ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

 

- ์ฝ”๋ฃจํ‹ด์€ IEnumerator๋ผ๋Š” ๋ฐ˜ํ™˜ํ˜•์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

     - ์œ„์™€ ๊ฐ™์ด IEnumerator ํ•จ์ˆ˜์ด๋ฆ„()์˜ ํ˜•ํƒœ๋กœ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

- yield return์ด ํ•„์ˆ˜์ž…๋‹ˆ๋‹ค. ๋ณดํ†ต ์•„๋ž˜ ๋‘ ์ข…๋ฅ˜๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š”๋ฐ,

     - yield return null; // ํ•œ ํ”„๋ ˆ์ž„์„ ์‰ฝ๋‹ˆ๋‹ค.

     - yield return new WaitForSeconds(์‹œ๊ฐ„); // ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ž…๋ ฅํ•œ ์ดˆ๋งŒํผ ์‰ฝ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋ฃจํ‹ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
IEnumerator Spawn()
{
    for (int i = 0; i < 3; i++)
    {
        Instantiate(prefab, gameObject.transform.position, gameObject.transform.rotation);
        yield return new WaitForSeconds(1f);
    }
}
cs

 

๊ทธ๋ฆฌ๊ณ  ์ฝ”๋ฃจํ‹ด์€ ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜์™€ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅธ๋ฐ ์•„๋ž˜ ๋‘ ํ•จ์ˆ˜๋กœ ์‹คํ–‰๊ณผ ์ค‘์ง€๋ฅผ ์ปจํŠธ๋กคํ•ฉ๋‹ˆ๋‹ค.

- StartCoroutine() : ์‹คํ–‰

- StopCoroutine() : ์ค‘์ง€

1
2
3
4
5
6
7
8
9
10
11
12
void Update()
{
    if (Input.GetKeyDown("s"))
    {
        StartCoroutine("Spawn");
    }
        
    if (Input.GetKeyDown("p"))
    {
        StopCoroutine("Spawn");
    }
}
cs

 

์•„๋ž˜๋Š” ์ œ๊ฐ€ ์ด๋ฒˆ์— ํŒ€ ํ”„๋กœ์ ํŠธ ๊ฒŒ์ž„์„ ๋งŒ๋“ค๋ฉด์„œ ์‚ฌ์šฉํ•œ ์ฝ”๋ฃจํ‹ด ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

ํ”ผ๊ฒฉ ์‹œ ์ผ์ • ์‹œ๊ฐ„ ๋™์•ˆ ๋ฌด์  ์ƒํƒœ๋ฅผ ์ฒดํฌํ•˜๋Š” ๋ณ€์ˆ˜๋ฅผ true๋กœ ๋งŒ๋“ค๊ณ  ์Šคํ”„๋ผ์ดํŠธ์˜ ํˆฌ๋ช…๋„๋ฅผ ์กฐ์ ˆํ•ด ๊นœ๋นก๊ฑฐ๋ฆฌ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
IEnumerator Invincibility()
{
    isInvincibility = true;
    invincibilityPlatform.SetActive(true);
 
    for (int i=0; i<invincibilityDuration; i++)
    {
        playerSprite.color = new Color(2552552550.4f);
        yield return new WaitForSeconds(0.5f);
        playerSprite.color = new Color(2552552550.7f);
        yield return new WaitForSeconds(0.5f);
    }
 
    isInvincibility = false;
    invincibilityPlatform.SetActive(false);
    playerSprite.color = new Color(2552552551);
}
cs

 

์ฐธ๊ณ  ๋ฌธํ—Œ : https://docs.unity3d.com/kr/530/Manual/Coroutines.html (Unity ๊ณต์‹ ๋ฌธ์„œ)

728x90