유니티 C# Unitask 비동기 사용법 간단 예시 코드 작성

반응형

1. 기본적인 비동기 작업

using Cysharp.Threading.Tasks;

public async UniTask StartAsyncTask()
{
    Debug.Log("Task Started");
    await UniTask.Delay(1000); // 1 second delay
    Debug.Log("Task Completed");
}

 

 

2. 비동기 메소드 호출

using Cysharp.Threading.Tasks;

public async UniTask CallAsyncMethod()
{
    await SomeAsyncMethod();
    Debug.Log("Method Called");
}

private async UniTask SomeAsyncMethod()
{
    await UniTask.Delay(500);
    Debug.Log("Async Method Executed");
}

 

 

3. UniTask와 Coroutine 혼합 사용

using Cysharp.Threading.Tasks;
using UnityEngine;

public class UniTaskWithCoroutine : MonoBehaviour
{
    private async void Start()
    {
        await UniTask.Delay(1000);
        StartCoroutine(ExampleCoroutine());
    }

    private IEnumerator ExampleCoroutine()
    {
        Debug.Log("Coroutine Started");
        yield return new WaitForSeconds(1);
        Debug.Log("Coroutine Completed");
    }
}

 

 

4. UniTask를 통한 비동기 루프

using Cysharp.Threading.Tasks;
using UnityEngine;

public class AsyncLoop : MonoBehaviour
{
    private async void Start()
    {
        await RepeatTaskAsync();
    }

    private async UniTask RepeatTaskAsync()
    {
        while (true)
        {
            Debug.Log("Repeating Task");
            await UniTask.Delay(1000);
        }
    }
}

 

 

5. 비동기 조건 검사

using Cysharp.Threading.Tasks;
using UnityEngine;

public class AsyncConditionCheck : MonoBehaviour
{
    private async void Start()
    {
        await WaitForConditionAsync(() => Input.GetKeyDown(KeyCode.Space));
        Debug.Log("Space Key Pressed");
    }

    private async UniTask WaitForConditionAsync(System.Func<bool> condition)
    {
        while (!condition())
        {
            await UniTask.Yield();
        }
    }
}

 

 

6. UniTask.WhenAll 사용 예제

using Cysharp.Threading.Tasks;
using UnityEngine;

public class UniTaskWhenAllExample : MonoBehaviour
{
    private async void Start()
    {
        await UniTask.WhenAll(Task1(), Task2());
        Debug.Log("Both Tasks Completed");
    }

    private async UniTask Task1()
    {
        await UniTask.Delay(1000);
        Debug.Log("Task1 Completed");
    }

    private async UniTask Task2()
    {
        await UniTask.Delay(2000);
        Debug.Log("Task2 Completed");
    }
}

 

 

7. UniTask.WhenAny 사용 예제

using Cysharp.Threading.Tasks;
using UnityEngine;

public class UniTaskWhenAnyExample : MonoBehaviour
{
    private async void Start()
    {
        await UniTask.WhenAny(Task1(), Task2());
        Debug.Log("One of the Tasks Completed");
    }

    private async UniTask Task1()
    {
        await UniTask.Delay(1000);
        Debug.Log("Task1 Completed");
    }

    private async UniTask Task2()
    {
        await UniTask.Delay(2000);
        Debug.Log("Task2 Completed");
    }
}

 

8. 비동기 이벤트 처리

using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

public class AsyncButtonHandler : MonoBehaviour
{
    public Button myButton;

    private void Start()
    {
        HandleButtonClickAsync().Forget();
    }

    private async UniTaskVoid HandleButtonClickAsync()
    {
        await myButton.OnClickAsync();
        Debug.Log("Button Clicked");
    }
}

 

9. UniTask.ToCoroutine 사용 예제

using Cysharp.Threading.Tasks;
using UnityEngine;

public class UniTaskToCoroutineExample : MonoBehaviour
{
    private void Start()
    {
        StartCoroutine(MyCoroutine());
    }

    private IEnumerator MyCoroutine()
    {
        yield return SomeAsyncMethod().ToCoroutine();
        Debug.Log("Coroutine Completed");
    }

    private async UniTask SomeAsyncMethod()
    {
        await UniTask.Delay(1000);
        Debug.Log("Async Method Executed");
    }
}

 

 

10. UniTask.ToCoroutine 사용 예제

using Cysharp.Threading.Tasks;
using UnityEngine;

public class UniTaskCompletionSourceExample : MonoBehaviour
{
    private async void Start()
    {
        var completionSource = new UniTaskCompletionSource();
        Invoke("CompleteTask", 2);
        await completionSource.Task;
        Debug.Log("Task Completed");
    }

    private void CompleteTask()
    {
        var completionSource = new UniTaskCompletionSource();
        completionSource.TrySetResult();
    }
}
반응형