본문 바로가기

Unity2020.1

[Unity]async/await 통합 패키지 UniTask

https://github.com/Cysharp/UniTask#upm-package

 

GitHub - Cysharp/UniTask: Provides an efficient allocation free async/await integration for Unity.

Provides an efficient allocation free async/await integration for Unity. - GitHub - Cysharp/UniTask: Provides an efficient allocation free async/await integration for Unity.

github.com

유니티 강의를 보다가 UniTask의 존재를 알게 되었다.

기존의 async와 await와 같이 쓰던 Task와 비교해보자면 Task는 class 참조형식이지만

https://docs.microsoft.com/ko-kr/dotnet/api/system.threading.tasks.task?view=net-5.0 

 

Task 클래스 (System.Threading.Tasks)

비동기 작업을 나타냅니다.Represents an asynchronous operation.

docs.microsoft.com

UniTask는 struct 값형식이기 때문에 힙 메모리할당을 하지 않기 때문에 CPU부하가 드는 GC의 동작을 줄여서 최적화를 하고 있다고 한다.

https://neuecc.medium.com/unitask-v2-zero-allocation-async-await-for-unity-with-asynchronous-linq-1aa9c96aa7dd

 

UniTask v2 — Zero Allocation async/await for Unity, with Asynchronous LINQ

I’ve previously published UniTask, a new async/await library for Unity, now I’ve rewritten all the code and released new one.

neuecc.medium.com

 

 

 

Window->PackageManager좌측의 플러스 버튼을 클릭하며 URL을 통해 패키지추가를 할 수 있다.

패키지에 UniTask가 추가된 걸 확인할 수 있다.

 

Addressables를 사용하는데 함수가 전부 비동기여서 유용하게 사용하였다.

Addressables.InitializeAsync().Completed += op=>
{
	Addressables.Release(op);
}

 

위의 예제코드를 아래와 같이 사용하였다.

private async UniTask Intialize()
{            
	AsyncOperationHandle<IResourceLocator> initHandle = Addressables.InitializeAsync();
    await initHandle;
    Addressables.Release(initHandle);
}