Unity

코루틴 μ‹€ν–‰ 쀑인지 μ²΄ν¬ν•˜κΈ°

γ…Ž-γ…Ž 2022. 12. 31. 13:02

1. bool λ³€μˆ˜

 

2. int

- 코루틴 μ‹€ν–‰ μ‹œ +1, μ’…λ£Œ μ‹œ -1

 

3. Action

- IEnumerator λ©”μ†Œλ“œμ— μ’…λ£Œ μ‹œ μˆ˜ν–‰ν•  Action λ„£μ–΄μ£ΌκΈ°


private void Update()
{
    bool test1, test2;

    StartCoroutine(MoveToDirection(swappingSlotFrom.slotIndex, dragDirection, () =>
    {
        test1 = true;
    }));
    StartCoroutine(MoveToDirection(swappingSlotTo.slotIndex, -dragDirection, () =>
    {
        test2 = true;
    }));
}

public IEnumerator MoveToDirection(Vector2Int slotIndex, Vector2Int direction, Action onFinished = null)
{
    while(true)
    {
        // ...
    }
    
    onFinished?.Invoke();
}

 

4. 묢음 IEnumerator

private IEnumerator Swap()
{
    isAnimationPlaying = true;

    bool test1 = false, test2 = false;

    MoveToDirection(swappingSlotFrom.slotIndex, dragDirection, () =>
    {
        test1 = true;
    });

    MoveToDirection(swappingSlotTo.slotIndex, -dragDirection, () => {

        test2 = true;
    });

    yield return new WaitWhile(() => test1 && test2);

    isAnimationPlaying = false;
}

 

5. Queue

 

6. 클래슀 λ§Œλ“€κΈ°

- 1) CustomYieldConstruction 상속

- 2) 클래슀 μ•ˆμ—μ„œ 코루틴 μ‹€ν–‰, μ’…λ£Œλ₯Ό κ΄€μž₯