getting-started/hello-world | hello world

news/2025/2/21 13:06:41/文章来源:https://www.cnblogs.com/aromacourt/p/18719737

将VContainer集成到应用程序中的基本方法是:

  • 在场景中创建一个继承自LifetimeScope的组件。它有一个容器和一个作用域。
  • 在LifetimeScope的子类中使用C#代码注册依赖项。这是组合根。
  • 当播放场景时,LifetimeScope会自动构建容器并将其分发到自己的PlayerLoopSystem中。

:::note
通常,“作用域”在游戏过程中会反复创建和销毁。LifetimeScope假设了这一点,并具有父子关系。
:::

1. 编写一个依赖于其他类的类

让我们说“Hello world”。

namespace MyGame
{public class HelloWorldService{public void Hello(){UnityEngine.Debug.Log("Hello world");}}
}

2. 定义组合根

接下来,让我们编写一个可以自动装配类的设置。

  • 在项目选项卡中的文件夹中右键单击,选择创建 -> C#脚本
  • 将其命名为GameLifetimeScope.cs

:::note
VContainer会自动为以*LifetimeScope结尾的C#脚本生成模板。
:::

你指示builder并注册上面的类。

using VContainer;
using VContainer.Unity;namespace MyGame
{public class GameLifetimeScope : LifetimeScope{protected override void Configure(IContainerBuilder builder){
+            builder.Register<HelloWorldService>(Lifetime.Singleton);}}
}

:::note
VContainer始终需要显式地传递Lifetime参数。这为我们提供了透明性和一致性。
:::

3. 创建附加了LifetimeScope的GameObject

在层次结构选项卡中右键单击并选择创建空对象。并将其命名为GameLifetimeScope

然后附加上面创建的组件。

4. 如何使用新的HelloWorldService?

注册的对象将自动具有依赖注入功能。
如下所示:

using VContainer;
using VContainer.Unity;namespace MyGame
{public class GamePresenter{readonly HelloWorldService helloWorldService;public GamePresenter(HelloWorldService helloWorldService){this.helloWorldService = helloWorldService;}}
}

并且让我们也注册这个类。

builder.Register<HelloWorldService>(Lifetime.Singleton);
+ builder.Register<GamePresenter>(Lifetime.Singleton);

5. 在PlayerLoopSystem上执行注册的对象

要在Unity中编写应用程序,我们必须中断Unity的生命周期事件。
(通常是MonoBehaviour的Start / Update / OnDestroy / 等)

使用VContainer注册的对象可以独立于MonoBehaviour执行此操作。
这是通过实现和注册一些标记接口自动完成的。

using VContainer;
using VContainer.Unity;namespace MyGame{
-    public class GamePresenter
+    public class GamePresenter : ITickable{readonly HelloWorldService helloWorldService;public GamePresenter(HelloWorldService helloWorldService){this.helloWorldService = helloWorldService;}+        void ITickable.Tick()
+        {
+            helloWorldService.Hello();
+        }}}

现在,Tick()将在Unity的Update时机执行。

因此,通过标记接口保持任何副作用入口点是一个好习惯。

(从设计上讲,对于MonoBehaviour来说,使用Start / Update等已经足够了。VContainer的标记接口是一个将领域逻辑和表现逻辑的入口点分离的功能。)

我们应该将其注册为在Unity的生命周期事件上运行。

- builder.Register<GamePresenter>(Lifetime.Singleton);
+ builder.RegisterEntryPoint<GamePresenter>();

:::note

  • RegisterEntryPoint<GamePresenter>()是注册与Unity的PlayerLoop事件相关的接口的别名。
    • 类似于Register<GamePresenter>(Lifetime.Singleton).As<ITickable>()
  • 注册生命周期事件而不依赖于MonoBehaviour有助于解耦领域逻辑和表现逻辑!
    :::

如果你有多个EntryPoints,你也可以使用以下声明进行分组。

builder.UseEntryPoints(Lifetime.Singleton, entryPoints =>
{entryPoints.Add<GamePresenter>();// entryPoints.Add<OtherSingletonEntryPointA>();// entryPoints.Add<OtherSingletonEntryPointB>();// entryPoints.Add<OtherSingletonEntryPointC>();
})

这使得EntryPoints在设计中得到特殊处理更加清晰。

6. 控制反转(IoC)

它通常响应用户输入等事件调用逻辑。

考虑以下视图组件。


using UnityEngine.UI;
public class HelloScreen : MonoBehaviour
{public Button HelloButton;
}

在普通的Unity编程中,你将逻辑调用嵌入到HelloScreen中,但如果你使用DI,你可以将HelloScreen与任何控制流分离。

namespace MyGame
{
-    public class GamePresenter : ITickable
+    public class GamePresenter : IStartable{readonly HelloWorldService helloWorldService;
+        readonly HelloScreen helloScreen;  public GamePresenter(HelloWorldService helloWorldService,
+            HelloScreen helloScreen){this.helloWorldService = helloWorldService;
+            this.helloScreen = helloScreen;}+        void IStartable.Start()
+        {
+            helloScreen.HelloButton.onClick.AddListener(() => helloWorldService.Hello());
+        }}    
}

通过这样做,我们成功地将领域逻辑 / 控制流 / 视图组件分离开来。

  • GamePresenter: 仅负责控制流。
  • HelloWorldService: 仅负责可以随时调用的功能
  • HelloScreen: 仅负责视图。

在VContainer中,你需要注册依赖的MonoBehaviour。别忘了注册HelloScreen。

    public class GameLifetimeScope : LifetimeScope{
+       [SerializeField]
+       HelloScreen helloScreen;protected override void Configure(IContainerBuilder builder){builder.RegisterEntryPoint<GamePresenter>();builder.Register<HelloWorldService>(Lifetime.Singleton);
+           builder.RegisterComponent(helloScreen);}}

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

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

相关文章

赶上AI的大潮:在VSCode中使用DeepSeek编程的极简方法

1 赶上AI的大潮:在VSCode中使用DeepSeek编程的极简方法 1.1 背景DeepSeek在春节期间突然大行其道,欣喜国力大增的同时,对于普通IT工作者,如何才能享受这一波AI红利,让自己的工作更出彩呢?  很多人在大量的宣传攻势下都知道了DeepSeek官网,然而当大家兴冲冲的来到DeepS…

about/VContainer是什么

VContainer import {BenchmarkGraph} from "../../src/components/BenchmarkGraph" import {GCAllocGraph} from "../../src/components/GCAllocGraph" VContainer 是 Unity 游戏引擎中极快的 DI(依赖注入)工具。"V" 意味着让 Unity 的初始 &q…

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地址就可以。 下面以硅基流动为例进…