• Stars
    star
    1,932
  • Rank 23,991 (Top 0.5 %)
  • Language
    C#
  • License
    MIT License
  • Created over 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The extra fast, minimum code size, GC-free DI (Dependency Injection) library running on Unity Game Engine.

VContainer

Releases openupm

The extra fast DI (Dependency Injection) library running on Unity Game Engine.

"V" means making Unity's initial "U" more thinner and solid ... !

  • Fast Resolve: Basically 5-10x faster than Zenject.
  • Minimum GC Allocation: In Resolve, we have zero allocation without spawned instances.
  • Small code size: Few internal types and few .callvirt.
  • Assisting correct DI way: Provides simple and transparent API, and carefully select features. This prevents the DI declaration from becoming overly complex.
  • Immutable Container: Thread safety and robustness.

Features

  • Constructor Injection / Method Injection / Property & Field Injection
  • Dispatch own PlayerLoopSystem
  • Flexible scoping
    • Application can freely create nested Lifetime Scope with any async way for you like.
  • Accelerated mode with SourceGenerator (Optional)
  • Diagnositcs window on unity editor
  • UniTask Integration
  • ECS Integration beta

Documentation

Visit vcontainer.hadashikick.jp to view the full documentation.

Performance

GC Alloc Result Example

Installation

Requires Unity 2018.4+

Install via UPM (using Git URL)

  1. Navigate to your project's Packages folder and open the manifest.json file.
  2. Add this line below the "dependencies": { line
    • "jp.hadashikick.vcontainer": "https://github.com/hadashiA/VContainer.git?path=VContainer/Assets/VContainer#1.13.2",
  3. UPM should now install the package.

Install via OpenUPM

  1. The package is available on the openupm registry. It's recommended to install it via openupm-cli.
  2. Execute the openum command.
    • openupm add jp.hadashikick.vcontainer
      

Install manually (using .unitypackage)

  1. Download the .unitypackage from releases page.
  2. Open VContainer.x.x.x.unitypackage

Basic Usage

First, create a scope. References are automatically resolved for types registered here.

public class GameLifetimeScope : LifetimeScope
{
    public override void Configure(IContainerBuilder builder)
    {
        builder.RegisterEntryPoint<ActorPresenter>();

        builder.Register<CharacterService>(Lifetime.Scoped);
        builder.Register<IRouteSearch, AStarRouteSearch>(Lifetime.Singleton);

        builder.RegisterComponentInHierarchy<ActorsView>();
    }
}

Where definitions of classes are

public interface IRouteSearch
{
}

public class AStarRouteSearch : IRouteSearch
{
}

public class CharacterService
{
    readonly IRouteSearch routeSearch;

    public CharacterService(IRouteSearch routeSearch)
    {
        this.routeSearch = routeSearch;
    }
}
public class ActorsView : MonoBehaviour
{
}

and

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()
    {
        // Scheduled at Start () on VContainer's own PlayerLoopSystem.
    }
}
  • In this example, the routeSearch of CharacterService is automatically set as the instance of AStarRouteSearch when CharacterService is resolved.
  • Further, VContainer can have a Pure C# class as an entry point. (Various timings such as Start, Update, etc. can be specified.) This facilitates "separation of domain logic and presentation".

Flexible Scoping with async

LifetimeScope can dynamically create children. This allows you to deal with the asynchronous resource loading that often occurs in games.

public void LoadLevel()
{
    // ... Loading some assets

    // Create a child scope
    instantScope = currentScope.CreateChild();

    // Create a child scope with LifetimeScope prefab
    instantScope = currentScope.CreateChildFromPrefab(lifetimeScopePrefab);

    // Create a child with additional registration
    instantScope = currentScope.CreateChildFromPrefab(
        lifetimeScopePrefab,
        builder =>
        {
            // Extra Registrations ...
        });

    instantScope = currentScope.CreateChild(builder =>
    {
        // ExtraRegistrations ...
    });

    instantScope = currentScope.CreateChild(extraInstaller);
}

public void UnloadLevel()
{
    instantScope.Dispose();
}

In addition, you can create a parent-child relationship with LifetimeScope in an Additive scene.

class SceneLoader
{
    readonly LifetimeScope currentScope;

    public SceneLoader(LifetimeScope currentScope)
    {
        this.currentScope = currentScope; // Inject the LifetimeScope to which this class belongs
    }

    IEnumerator LoadSceneAsync()
    {
        // LifetimeScope generated in this block will be parented by `this.lifetimeScope`
        using (LifetimeScope.EnqueueParent(currentScope))
        {
            // If this scene has a LifetimeScope, its parent will be `parent`.
            var loading = SceneManager.LoadSceneAsync("...", LoadSceneMode.Additive);
            while (!loading.isDone)
            {
                yield return null;
            }
        }
    }

    // UniTask example
    async UniTask LoadSceneAsync()
    {
        using (LifetimeScope.EnqueueParent(parent))
        {
            await SceneManager.LoadSceneAsync("...", LoadSceneMode.Additive);
        }
    }
}
// LifetimeScopes generated during this block will be additionally Registered.
using (LifetimeScope.Enqueue(builder =>
{
    // Register for the next scene not yet loaded
    builder.RegisterInstance(extraInstance);
}))
{
    // Loading the scene..
}

See scoping for more information.

UniTask

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

See integrations for more information.

Diagnostics Window

See diagnostics for more information.

Credits

VContainer is inspired by:

Author

@hadashiA

License

MIT

More Repositories

1

VYaml

The extra fast, low memory footprint YAML library for C#, focued on .NET and Unity.
C#
322
star
2

VitalRouter

The source-generator powered zero-allocation in-memory message passing library for Unity and .NET.
C#
227
star
3

Unio

Unio (short for unity native I/O) is a small utility set of I/O using native memory areas.
C#
159
star
4

UniTaskPubSub

UniTask & IUniTaskAsyncEnumerable baseed pub/sub messaging. this is like the UniTask version of UniRx.MessageBroker.
C#
85
star
5

LiquidUI

C#
31
star
6

UnityURP-ScreenSpaceFluid

Sample project to draw pseudo-fluid by adding own URP Pass
C#
24
star
7

elasticsearch-flavor

Java
19
star
8

RippleLayer

Drawing splash effect
Swift
17
star
9

sexpression

s-expression parser for javascript
JavaScript
16
star
10

sometuke

c++/OpenGL 2D game engine
C++
10
star
11

LiquidShape

Swift
7
star
12

sample-rx-mvvm

JavaScript
6
star
13

onetime_token

Generate a temporary token of secret associated with ActiveRecord. it is stored in redis.
Ruby
5
star
14

dotfiles

Vim Script
5
star
15

testkick

Emacs Lisp command that find the test code and run it.
Emacs Lisp
4
star
16

yasuri

https://yasuri.herokuapp.com
JavaScript
4
star
17

suzuri-actionscript-client

ActionScript
4
star
18

unigeo

[WIP] Geometric algorithm implemented with rust and run with Unity
C#
3
star
19

rakugakijs

node.js with socket.io, synchronized painting demo.
JavaScript
3
star
20

Mrs.Driller

Mr.Driller clone of using unity.
C#
3
star
21

ppswf

pure python swf tool
Python
2
star
22

text2png

very simple imagemagick wrapper. configuration file to be rendered in png.
Ruby
2
star
23

node-sass-brunch

Adds Sass support to brunch. Using node-sass.
CoffeeScript
2
star
24

SampleDelaunayTriangulation

C#
1
star
25

uturn

RxJS based MVVM everywhere
JavaScript
1
star
26

ScrollViewModoki

Swift
1
star
27

graph_path

็ตŒ่ทฏๆŽข็ดข
Ruby
1
star
28

helm-rubygems-local

Find installed rubygems lib directory for Emacs helm.
Emacs Lisp
1
star