**VContainer-optimization/async-container-build | 优化——异步容器构建
1.VContainer 介绍2.VContainer-about/what-is-vcontainer | 概述——VContainer 是什么?3.VContainer-about/what-is-di | 概述——DI 是什么?4.VContainer-getting-started/installation | 入门——安装5.VContainer-getting-started/hello-world | 入门——Hello World6.VContainer-resolving/constructor-injection | 解析——构造函数注入7.VContainer-resolving/method-injection | 解析——方法注入8.VContainer-resolving/property-field-injection | 解析——属性/字段注入9.VContainer-resolving/gameobject-injection | 解析——注入到 MonoBehaviours 中10.VContainer-resolving/container-api | 解析——容器 API11.VContainer-registering/register-type | 注册——注册纯 C# 类型12.VContainer-registering/register-using-delegate | 注册——注册使用委托13.VContainer-registering/register-factory | 注册——注册工厂14.VContainer-registering/register-monobehaviour | 注册——注册 MonoBehaviour15.VContainer-registering/register-scriptable-object | 注册——注册 ScriptableObject16.VContainer-registering/register-collection | 注册——注册集合17.VContainer-registering/register-callbacks | 注册——注册回调18.VContainer-integrations/entrypoint | 集成——纯 C# 入口点19.VContainer-integrations/unitask | 集成——Unitask20.VContainer-integrations/unirx | 集成——UniRx21.VContainer-integrations/messagepipe | 集成——MessagePipe22.VContainer-integrations/ecs | 集成——ECS (beta)23.VContainer-scoping/lifetime-overview | 作用域——生命周期管理总览24.VContainer-scoping/generate-child-via-scene | 作用域——通过场景或 prefab 生成子作用域25.VContainer-scoping/generate-child-with-code-first | 作用域——通过代码优先生成子作用域26.VContainer-scoping/project-root-lifetimescope | 作用域——项目根作用域 LifetimeScope27.VContainer-diagnostics/diagnostics-window | 诊断——VContainer 诊断窗口28.VContainer-optimization/source-generator | 优化——源码生成器
29.**VContainer-optimization/async-container-build | 优化——异步容器构建
30.VContainer-optimization/parallel-container-build | 优化——并行容器构建31.VContainer-optimization/codegen | 优化——预生成 IL 代码(已弃用)32.VContainer-comparing/comparing-to-zenject | 对比 Zenject33.MessagePipe 中文文档VContainer 在构建容器时会执行反射等预处理操作。
如果要减少主线程阻塞:
- 将
LifetimeScope.autoRun
设置为false
- 场景加载后手动调用
LifetimeScope.Build
。
例如:
var nextScene = await LoadSceneAsync(...);
var lifetimeScope = LifetimeScope.Find<MyLifetimeScope>(nextScene.Scene);
// 异步解决方案
await UniTask.Run(() => lifetimeScope.Build());
⚠️ Unity 依赖的功能 builder.RegisterComponentInHierarchy()
无法在后台线程中运行。
若报错可使用 Awake 替代。
例如:
class GameLifetimeScope : LifetimeScope
{
Ground groundInScene;
protected override void Awake()
{
// 在主线程中运行。
groundInScene = FindObjectOfType<Ground>();
base.Awake();
}
protected override void Configure(IContainerBuilder builder)
{
// 现在可以在后台线程中运行。
builder.RegisterInstance(groundInScene);
}
}
合集:
VContainer 中文文档
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)