Some insights about UniTask

#Some insights about UniTask
http://t.csdn.cn/T1fua
Insert image description here
#Unity's UniTask is a C# library for asynchronous programming, which extends the Task and await/async modes in .NET.
1. Compared with the traditional Task and async/await modes, UniTask is more lightweight because it does not need to create and manage a thread pool like Task.
2. UniTask has higher performance because it uses less memory and CPU resources.
3. UniTask also provides more functions, such as cancellation and timeout functions, which are not easy to implement in traditional Tasks.

Common uses of #Unitask:
Asynchronous waiting: Using the await keyword, you can wait for an asynchronous operation to complete, so you don't need to manually handle callbacks or use coroutines.

Delayed execution: Use the UniTask.Delay method to perform an operation after a specified time without using a coroutine or timer.

Synchronized operations: Use the UniTask.SwitchToMainThread method to perform an operation on the main thread, which is very useful for accessing Unity components or APIs.

Iterator: Use the UniTask.ToCoroutine method to convert a UniTask object into an IEnumerator so that it can be used in coroutines.

Task chain: Use the UniTask.WhenAll or UniTask.WhenAny method to create a task chain and wait for multiple asynchronous operations to complete before performing the next operation.

Cancel tasks: Use the UniTaskCancellationToken class to cancel asynchronous operations, which is very useful for long-running operations.

#UniTask Features
1. UniTask is an asynchronous operation library based on C# structures that can be used in Unity. UniTask uses a custom AsyncMethodBuilder to achieve the goal of zero GC, which makes all Unity’s asynchronous operations and coroutines available Use await syntax.
3.unitask can provide effective GC-free async/await integration for Unity.
4.UniTask also provides asynchronous LINQ, Channel and AsyncReactiveProperty functions.
5.UniTask provides a TaskTracker window to help developers monitor and diagnose UniTask memory leaks at runtime.
6.UniTask provides Await/AsyncEnumerable support for MonoBehaviour message events and uGUI events.
7.UniTask has a high degree of behavioral compatibility with the Task, ValueTask and IValueTaskSource interfaces in the .NET standard library.

#unitaskWhy you can provide effective GC-free async/await integration for Unity.
1. Unitask is developed based on C# Task
2. This is because Unitask reduces Task Allocation as much as possible by optimizing the creation and destruction process of Task, thereby reducing GC pressure.
3. Unitask provides a custom task scheduler. Through the settings of the scheduler, you can control the execution order, priority and concurrency of tasks, etc., so as to better control the performance of the game.
4. Unitask is optimized for Unity game development scenarios, such as supporting synchronous calls of the Unity main thread, supporting the nesting and calling of Unity coroutines, and supporting the operation and recycling of Unity objects.

• UniTask.OnAnimatorIKAsync(int layerIndex): 当 Animator 执行 IK 动画时调用。
• UniTask.OnAnimatorMoveAsync(): 当 Animator 更新物体的 transform 时调用。
• UniTask.OnApplicationFocusAsync(bool hasFocus): 当应用程序获得或失去焦点时调用。
• UniTask.OnApplicationPauseAsync(bool pauseStatus): 当应用程序暂停或恢复时调用。
• UniTask.OnApplicationQuitAsync(): 当应用程序退出时调用。
• UniTask.OnAudioFilterReadAsync(float[] data, int channels): 当 AudioSource 读取音频数据时调用。
• UniTask.OnBecameInvisibleAsync(): 当该对象从相机视野中消失时调用。
• UniTask.OnBecameVisibleAsync(): 当该对象进入相机视野时调用。
• UniTask.OnCollisionEnterAsync(Collision collision): 当该对象与另一个对象发生碰撞时调用。
• UniTask.OnCollisionExitAsync(Collision collision): 当该对象与另一个对象碰撞结束时调用。
• UniTask.OnCollisionStayAsync(Collision collision): 当该对象与另一个对象持续碰撞时调用。
• UniTask.OnControllerColliderHitAsync(ControllerColliderHit hit): 当该对象与控制器发生碰撞时调用。
• UniTask.OnDisableAsync(): 当对象被禁用时调用。
• UniTask.OnEnableAsync(): 当对象被启用时调用。
• UniTask.OnJointBreakAsync(float breakForce): 当该对象的关节断裂时调用。

Guess you like

Origin blog.csdn.net/ranger__/article/details/129760256