LayerBase.Task 1.3.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package LayerBase.Task --version 1.3.1
                    
NuGet\Install-Package LayerBase.Task -Version 1.3.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LayerBase.Task" Version="1.3.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LayerBase.Task" Version="1.3.1" />
                    
Directory.Packages.props
<PackageReference Include="LayerBase.Task" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LayerBase.Task --version 1.3.1
                    
#r "nuget: LayerBase.Task, 1.3.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package LayerBase.Task@1.3.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LayerBase.Task&version=1.3.1
                    
Install as a Cake Addin
#tool nuget:?package=LayerBase.Task&version=1.3.1
                    
Install as a Cake Tool

🚀 LayerBase.Task: 工业级游戏异步任务系统

LayerBase.TaskLayerBase 架构总线的核心基建之一,提供了一套专为游戏引擎(如 Unity、Godot)心跳循环(Pump)深度调优的零分配(Zero-Allocation)异步任务模型:LBTask


🎯 为什么要开发 LBTask?

在现代游戏开发中,异步操作(如等待动画播放、网络请求回调、分帧加载)无处不在。使用 C# 标准的 TaskTask<T> 会面临一个无法回避的痛点: 每次 await Task 都会在托管堆上分配内存。 在一秒钟 60 帧或 120 帧的游戏主循环中,这种高频的堆分配会导致严重的垃圾回收(GC)尖峰,从而引发游戏卡顿和掉帧。

LBTask 专为此而生:

  1. 同步路径零 GC 分配:通过内置的池化技术与状态机机机制,如果一个异步任务是同步完成的,它将绝对不会在堆上产生任何 GC Allocation。
  2. 完美融合引擎心跳:LBTask 深度集成于 LayerBase.LayerHub.Pump(deltaTime)。它拥有自己的 LayerBaseSynchronizationContext,无需依赖引擎原生的同步上下文即可实现安全的线程回归和时间调度。
  3. 极简 API:保留了原生 Task 的手感,支持 await LBTask.Delay()await LBTask.NextFrame() 等游戏级特性。

📦 如何使用?

本包默认已随主库 LayerBase 自动集成,无需单独配置。如果您需要单独使用或查看依赖,请参考以下方式。

基本异步处理

通过在 ManagerService 的事件处理器上挂载 [SubscribeAsync] 特性,即可轻松使用 LBTask 编写全异步逻辑,且全程 0 GC:

using LayerBase.Async;
using LayerBase.Core.Event;
using LayerBase.DI;

public partial class BattleManager : ILayerContext
{
    [SubscribeAsync]
    private async LBTask OnPlayerDead(PlayerDeathEvent e)
    {
        // 游戏级的延迟等待:此处的 Delay 完全依托于 LayerHub.Pump 的驱动,
        // 且不会产生标准的 Task GC 垃圾
        await LBTask.Delay(TimeSpan.FromSeconds(3f));
        
        Console.WriteLine("3 秒后,玩家重生逻辑触发...");
        
        // 甚至可以等待下一帧
        await LBTask.NextFrame();
        
        Console.WriteLine("这是下一帧...");
    }
}

返回值的异步任务 (LBTask<T>)

除了空任务,LBTask 也支持携带泛型返回值,其底层使用了 LBTaskCompletionSource<T> 实现了零分配的缓存回收:

public async LBTask<int> CalculateHeavyDataAsync()
{
    await LBTask.Delay(TimeSpan.FromSeconds(1));
    return 42;
}

public async LBTask RunTest()
{
    int result = await CalculateHeavyDataAsync();
    Console.WriteLine(result);
}

⚙️ 核心 API 总览

  • LBTask.CompletedTask: 返回一个已完成的 LBTask(零分配)。
  • LBTask.Delay(TimeSpan): 提供基于引擎时间的延迟操作。
  • LBTask.Delay(int milliseconds): 基于毫秒的延迟。
  • LBTask.NextFrame(): 等待至引擎驱动的下一帧。
  • LBTaskCompletionSource<T>: 手动控制生命周期的异步状态源,推荐用于包装外部的跨线程回调。

🔗 关于 LayerBase

LayerBase.Task 是 LayerBase 高性能架构生态的一环,配合底层总线(1.5亿 TPS 分发)和源生成器(零反射依赖注入)使用,可解锁完整的工业级能力。

项目主页:LayerBase GitHub 仓库

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on LayerBase.Task:

Package Downloads
LayerBase

高性能一体化游戏架构框架。集成模块化层级、轻量级 DI、异步任务(LBTask)与超极速事件总线。专为 Godot/Unity 优化。

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.5.1 91 5/16/2026
1.5.0 97 5/14/2026
1.4.9 97 5/12/2026
1.4.8.2 120 5/11/2026
1.4.7.5 119 5/5/2026
1.4.7.4 112 5/5/2026
1.4.7.3 114 5/4/2026
1.4.7.1 115 5/3/2026
1.4.5 148 5/2/2026
1.4.3 121 4/30/2026
1.4.2 126 4/28/2026
1.4.1 124 4/25/2026
1.4.0 122 4/24/2026
1.3.6 127 4/21/2026
1.3.3 203 4/19/2026
1.3.2 129 4/19/2026
1.3.1 122 4/19/2026
1.3.0 89 4/19/2026
1.2.0 154 4/18/2026