about/VContainer是什么

news/2025/2/21 11:33:40/文章来源:https://www.cnblogs.com/aromacourt/p/18719722

VContainer

import {BenchmarkGraph} from "../../src/components/BenchmarkGraph"
import {GCAllocGraph} from "../../src/components/GCAllocGraph"

VContainer 是 Unity 游戏引擎中极快的 DI(依赖注入)工具。"V" 意味着让 Unity 的初始 "U" 变得更瘦、更坚固!

  • 快速解析: 基本上比 Zenject 快 5-10 倍。
  • 最小化 GC 分配: 在解析时,如果没有生成实例,我们实现了零分配
  • 代码量小: 内部类型少,.callvirt 调用少。
  • 辅助正确的 DI 方式: 提供简单透明的 API,并精心选择功能。这可以防止 DI 声明变得过于复杂。
  • 不可变容器: 线程安全和健壮性。

功能

  • 构造函数注入 / 方法注入 / 属性和字段注入
  • 在自定义 PlayerLoopSystem 上的纯 C# 入口点
  • 灵活的作用域
    • 应用程序可以自由创建嵌套的生命周期作用域,支持任何异步方式。
  • 通过 Roslyn 源代码生成器加速模式
  • 诊断窗口
  • UniTask 集成
  • ECS 集成 beta

Unity 中的 DI + 控制反转

DI 容器可以让我们将纯 C# 类作为入口点(而不是 MonoBehaviour)。这意味着控制流和其他领域逻辑可以与 MonoBehaviour 作为视图组件的功能分离开来。

进一步阅读:

  • Manning | .NET 中的依赖注入
  • Unity 的轻量级 IoC 容器 - Seba's Lab

性能

10,000 次迭代的基准测试结果(Unity 2019.x / IL2CPP Standalone macOS)

  • 默认情况下,VContainer 和 Zenject 都在运行时使用反射。
  • "VContainer (CodeGen)" 表示通过 ILPostProcessor 预生成注入方法的 IL 代码进行优化。更多信息请参见 优化 部分。

解析复杂测试用例中的 GC 分配结果(Unity Editor 分析)

基本用法

首先,创建一个作用域。在这里注册的类型会自动解析引用。

public class GameLifetimeScope : LifetimeScope
{protected override void Configure(IContainerBuilder builder){builder.RegisterEntryPoint<ActorPresenter>();builder.Register<CharacterService>(Lifetime.Scoped);builder.Register<IRouteSearch, AStarRouteSearch>(Lifetime.Singleton);builder.RegisterComponentInHierarchy<ActorsView>();}
}

类的定义如下:

public interface IRouteSearch
{
}public class AStarRouteSearch : IRouteSearch
{
}public class CharacterService
{readonly IRouteSearch routeSearch;public CharacterService(IRouteSearch routeSearch){this.routeSearch = routeSearch;}
}
public class ActorsView : MonoBehaviour
{
}
public class ActorPresenter : IStartable
{readonly CharacterService service;readonly ActorsView actorsView;public ActorPresenter(CharacterService service,ActorsView actorsView){this.service = service;this.actorsView = actorsView;}void IStartable.Start(){// 在 VContainer 的自定义 PlayerLoopSystem 上调度 Start()。}
}
  • 在这个例子中,当 CharacterService 被解析时,它的 routeSearch 会自动设置为 AStarRouteSearch 的实例。
  • 此外,VContainer 可以将纯 C# 类作为入口点。(可以指定 Start、Update 等各种时机。)这有助于“领域逻辑与表现的分离”。

异步灵活作用域

LifetimeScope 可以动态创建子作用域。这使你可以处理游戏中常见的异步资源加载。

public void LoadLevel()
{// ... 加载一些资源// 创建子作用域instantScope = currentScope.CreateChild();// 使用 LifetimeScope 预制件创建子作用域instantScope = currentScope.CreateChildFromPrefab(lifetimeScopePrefab);// 创建带有额外注册的子作用域instantScope = currentScope.CreateChildFromPrefab(lifetimeScopePrefab,builder =>{// 额外注册 ...});instantScope = currentScope.CreateChild(builder =>{// 额外注册 ...});instantScope = currentScope.CreateChild(extraInstaller);
}public void UnloadLevel()
{instantScope.Dispose();
}

此外,你可以在附加场景中创建 LifetimeScope 的父子关系。

class SceneLoader
{readonly LifetimeScope currentScope;public SceneLoader(LifetimeScope currentScope){currentScope = currentScope; // 注入该类所属的 LifetimeScope}IEnumerator LoadSceneAsync(){// 在此块中生成的 LifetimeScope 将以 `this.lifetimeScope` 为父级using (LifetimeScope.EnqueueParent(currentScope)){// 如果此场景有 LifetimeScope,其父级将是 `parent`。var loading = SceneManager.LoadSceneAsync("...", LoadSceneMode.Additive);while (!loading.isDone){yield return null;}}}// UniTask 示例async UniTask LoadSceneAsync(){using (LifetimeScope.EnqueueParent(parent)){await SceneManager.LoadSceneAsync("...", LoadSceneMode.Additive);}}
}
// 在此块中生成的 LifetimeScope 将额外注册。
using (LifetimeScope.Enqueue(builder =>
{// 为尚未加载的下一个场景注册builder.RegisterInstance(extraInstance);
}))
{// 加载场景...
}

更多信息请参见 作用域。

UniTask

public class FooController : IAsyncStartable
{public async UniTask StartAsync(CancellationToken cancellation){await LoadSomethingAsync(cancellation);await ......}
}
builder.RegisterEntryPoint<FooController>();

更多信息请参见 集成。

诊断窗口

更多信息请参见 诊断。

入门

  • 安装
  • Hello World
  • 与 Zenject 的比较

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/885459.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vue打包项目后,宝塔面板里的Nginx下的接口反向代理如何设置?

场景描述: vue开发H5页面过程中,需要用到加密解密接口,以及一系列反向代理配置。配置文件一般为——vue.config.js ; 如下:const path = require(path); let zipName = sdbf-h5; module.exports = {css: {loaderOptions: {sass: {implementation: require(sass), // 使用 d…

【Pytorch】深度学习-day01

【Pytorch2.0 版本介绍】- 是一个用于机器学习和深度学习的开源深度学习框架- 完全向下兼容,不论是新手,还是已经用过多年,有成熟项目,都可以完美升级到2.0 【安装-配置】下载地址:https://pytorch.org/ 版本选择:gpu/cpu版本,目前pytorch的gpu版本仅支持英伟达的显卡…

MyBatisCodeHelper Pro 3.2.4激活

去Jetbrains插件市场下载对应3.2.4版本 https://plugins.jetbrains.com/plugin/9837-mybatiscodehelperpro/versions/stable 2.安装 英文中文翻译3.下载jar包 https://i-blog.csdnimg.cn/direct/03908cc038b14bc2a0a2555c63226b4b.gif (来源:https://blog.csdn.net/GOODter/ar…

《Indie Tools • 半月刊》第001期

《INDIE TOOLS》专注于分享独立开发出海精选、最新、最实用的工具。 欢迎订阅半月刊:《INDIE TOOLS • 半月刊》 如果本文能给你提供启发和帮助,感谢各位小伙引言:独立开发者工具分享 《INDIE TOOLS》专注于分享独立开发出海精选、最新、最实用的工具。 欢迎订阅半月刊:《I…

在QJ7 手动执行自动化测试

https://wiki.one.int.sap/wiki/display/Joule/Development-+and+Test-Landscape 1: 申请加入user group 2: 使用joule login 查看登录到central joule instance的信息。 点击 service key 的URL, 查看service key和 secret 3: 登录 central joule instance4: 执行测试:本…

Dynamics 365 Online通过OAuth 2 Client Credential授权(Server-to-Server Authentication)后调用Web API

本文很多内容来自 John Towgood 撰写的 Dynamics 365 Online Authenticate with Client Credentials ,也着重参考了官方的 Use Single-Tenant server-to-server authentication ,我根据新的Azure Portal界面做了一些操作上的变化,并且改了一些代码,还使用ADAL来简化代码。 …

【转载】在Visual Studio 2015中添加报表功能 (使用 VS2015打开c#项目,新建文件没有报表选项,或者打开已有的wsdl不能打开设计器 )

Visual Studio 2015默认安装时没有报表,这时需要添加Microsoft Office 开发人员工具、Microsoft SQL Server Data Tools选项,安装之后就可以显示报表了,具体操作如下。方法/步骤 双击Visual Studio 2015的安装程序图标,启动软件安装向导。 在Visual Studio软件安装向导对话…

BUUCTF-PWN-jarvisoj_level2

这道题对我来说有点新奇,它利用了程序中自带的system程序,和字符/bin/bash构造了一个后门函数让我们看看是怎么做的吧 首先和程序进行交互:发现是一个读取输入相关的程序,我们对其进行分析,首先用checksec检测到了NX保护,但是没关系。我们再用IDA进行程序的分析:我们看到…

推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!

在AI技术飞速发展的今天,大语言模型(LLM)的应用越来越广泛,但高昂的使用成本常常让个人开发者和小型团队望而却步。今天,我要为大家介绍一个非常实用的开源项目——DeepSeek-Free-API,它能够让你免费接入DeepSeek大模型,轻松实现各种AI功能。 1、项目简介 DeepSeek-Free…

Deepseek+Cherry Studio,打造便利的AI大模型使用体验

1、Cherry Studio 下载 官网下载地址:https://cherry-ai.com GitHub地址:https://github.com/kangfenmao/c2. 导入大模型IPA,开始使用 2.1 在线模型设置 在Cherry Studio中,可以接入自己使用的大语言模型API,点击设置,填入API密钥与API地址就可以。 下面以硅基流动为例进…

ATG1E BBQ Hard 学习笔记

ATG1E BBQ Hard 学习笔记 Luogu Link 题意简述 计算 $$\sum_{i=1}^n \sum_{j=i+1}^n \dbinom{a_i+b_i+a_j+b_j}{a_i+a_j}$$ 的值。答案对 \(10^9+7\) 取模。 \(N\le 2\times 10^5,a_i,b_i\le 2\times 10^3\)。 做法解析 \(O(N^2)\) 的做法是暴力枚举 \(i,j\),\(O(1)\) 计算出 …