Unity——VContainer的依赖注入

一、IOC控制反转和DI依赖倒置

1、IOC框架核心原理是依赖倒置原则

C#设计模式的六大原则

使用这种思想方式,可以让我们无需关心对象的生成方式,只需要告诉容器我需要的对象即可,而告诉容器我需要对象的方式就叫做DI(依赖注入)
今天主要想研究一下DI(依赖注入) ,这里我选了VContainer

地址:https://github.com/hadashiA/VContainer

文档地址:https://vcontainer.hadashikick.jp/

二、VContainer介绍

由于我们使用的是Unity,而主要的Mono不支持构造函数。所以我们这里选择注入方式主要是特性注入和方法注入

三、VContainer案例

(1)简单方法注入:

1、注册类型

cs 复制代码
public class GameLifetimeScope : LifetimeScope
{
    protected override void Configure(IContainerBuilder builder)
    {
        builder.RegisterEntryPoint<ActorPresenter>();
    }
}

2、方法注入

这里实现接口 IStartableITickable 。它是不依赖于Mono的接口,因此性能上更好,另一方面,IStartable 与Mono的Start,ITickable与Mono的Update都是相同的。

cs 复制代码
public class ActorPresenter : IStartable,ITickable
{
    public void Start()
    {
        Debug.Log("Start ActorPresenter");
    }

    public void Tick()
    {
        Debug.Log("Update ActorPresenter");
    }
}

(2)基础MVC功能:

Model层

cs 复制代码
public class UIModel
{
    public void Hello() 
    {
        Debug.Log("Hello World");
    }
}

View层

cs 复制代码
public class UIView : MonoBehaviour
{
    public Button button;
}

Control层

cs 复制代码
public class UIControl : IStartable
{
    readonly UIModel _model;
    readonly UIView _view;

    public UIControl(UIModel model)
    {
        this._model = model;
    }

    public UIControl(UIModel model, UIView view)
    {
        this._model = model;
        this._view = view;
    }

    public void Start()
    {
        _view.button.onClick.AddListener(() => _model.Hello());
    }
}

通过这样做,我们成功地分离了领域控制/控制流/显示组件

在VContainter中,记得注册

cs 复制代码
public class GameLifetimeScope : LifetimeScope
{
    public UIView helloScreen;
    protected override void Configure(IContainerBuilder builder)
    {
        builder.RegisterEntryPoint<UIControl>();
        builder.Register<UIModel>(Lifetime.Singleton);
        builder.RegisterComponent(helloScreen);
    }
}

(3)构造注入

构造函数里,只需要写一个需要依赖注入的函数,成员变量里就可以随时获得对象。如下例子ClassB构造函数的参数是ClassA,我们的classA变量就可以随时使用

cs 复制代码
class ClassB : IStartable,ITickable
{
    readonly ClassA a;
    public ClassB(ClassA a)
    {
        Debug.Log("ClassA构造函数注入");
        this.a = a;
    }

    public void Start()
    {
        a.Start();
    }

    public void Tick()
    {
        a.Update();
    }
}
cs 复制代码
class ClassA
{
    public ClassA()
    {
        Debug.Log("ClassA构造");
    }

    public void Start()
    {
        Debug.Log("Start");
    }
    public void Update() 
    {
        Debug.Log("Update");
    }
}
cs 复制代码
public class GameLifetimeScope : LifetimeScope
{
    //public UIView helloScreen;

    protected override void Configure(IContainerBuilder builder)
    {

        builder.RegisterEntryPoint<ClassB>();
        builder.Register<ClassA>(Lifetime.Singleton);
    }
}
(4)方法注入(其他的和上边一样)
cs 复制代码
class ClassB : IStartable,ITickable
{
    private ClassA a;

    [Inject]
    public void GetClassA(ClassA a) 
    {
        Debug.Log("方法注入");
        this.a = a;
    }

    public void Start()
    {
        a.Start();
    }

    public void Tick()
    {
        a.Update();
    }
}
(5)字段/属性注入
cs 复制代码
class ClassB : IStartable,ITickable
{
    [Inject]
    private ClassA a;

    public void Start()
    {
        a.Start();
    }

    public void Tick()
    {
        a.Update();
    }
}
相关推荐
死也不注释9 小时前
【第零章编辑器开发与拓展】
unity·编辑器
死也不注释10 小时前
【第一章编辑器开发基础第二节编辑器布局_3GUI元素和布局大小(3/4)】
unity·编辑器
KhalilRuan1 天前
Unity Demo-3DFarm详解-其二
unity·游戏引擎
死也不注释2 天前
【unity编辑器开发与拓展EditorGUILayoyt和GUILayoyt】
unity·编辑器·游戏引擎
Zillionnn3 天前
Unreal Engine 自动设置图像
游戏引擎·虚幻
3 天前
Unity开发中常用的洗牌算法
java·算法·unity·游戏引擎·游戏开发
马特说3 天前
Unity VR手术模拟系统架构分析与数据流设计
unity·系统架构·vr
心前阳光4 天前
Unity WebGL文本输入
unity·游戏引擎·webgl
天涯过客TYGK4 天前
unity A星寻路
unity·游戏引擎
KhalilRuan4 天前
Unity Demo——3D平台跳跃游戏笔记
笔记·游戏·unity·游戏引擎