DragonECS-AutoInjections/README.md
2026-03-29 01:49:14 +08:00

5.3 KiB
Raw Blame History

Version License Discord QQ

Auto Injections for DragonECS

Readme Languages:

Русский

English(WIP)

The extension is designed to reduce the amount of code by simplifying dependency injection by doing injections automatically.

Warning

The project is a work in progress, API may change.

While the English version of the README is incomplete, you can view the Russian version.


Installation

Versioning semantics - Open

Environment

Requirements:

  • Dependency: DragonECS
  • Minimum version of C# 7.3;

Optional:

  • Game engines with C#: Unity, Godot, MonoGame, etc.

Tested with:

  • Unity: Minimum version 2021.2.0.

Unity Installation

  • Unity Package

The package supports installation as a Unity package by adding the Git URL in the PackageManager:

https://github.com/DCFApixels/DragonECS-AutoInjections.git

Or add the package entry to Packages/manifest.json:

"com.dcfa_pixels.dragonecs-auto_injections": "https://github.com/DCFApixels/DragonECS-AutoInjections.git",
  • Source Code

The package source code can also be copied directly into the project.


Integration

Add the AutoInject() call to the pipeline Builder. Example:

_pipeline = EcsPipeline.New()
    .Inject(world)
    .Inject(_timeService)
    .Add(new TestSystem())
    .Add(new VelocitySystem())
    .Add(new ViewSystem())
    .AutoInject() // Done — automatic injections enabled
    .BuildAndInit();

Important

Ensure AutoInject() is called during initialization; otherwise nothing will work.

Dependency Injection

The [DI] attribute replaces the IEcsInject<T> interface. Fields marked with this attribute automatically receive dependencies injected into the pipeline. Example

[DI] EcsDefaultWorld _world;

Injection can also be done via a property or method:

EcsDefaultWorld _world;

//Обязательно наличие set блока.  
[DI] EcsDefaultWorld World { set => _world = value; } 

//Количество аргументов должно быть равно 1.
[DI] void InjectWorld(EcsDefaultWorld world) => _world = world;

Aggressive injection (without the [DI] attribute) is enabled by calling .AutoInject(true).

Code Example

class VelocitySystemDI : IEcsRun
{
    class Aspect : EcsAspectAuto
    {
        [ExcImplicit(typeof(FreezedTag))]
        [Inc] public EcsPool<Pose> poses;
        [Inc] public EcsPool<Velocity> velocities;
    }

    [DI] EcsDefaultWorld _world;
    [DI] TimeService _time;

    public void Run()
    {
        foreach (var e in _world.Where(out Aspect a))
        {
            a.poses.Get(e).position += a.velocities.Read(e).value * _time.DeltaTime;
        }
    }
}
Same code but without AutoInjections
class VelocitySystem : IEcsRun, IEcsInject<EcsDefaultWorld>, IEcsInject<TimeService>
{
    class Aspect : EcsAspect
    {
        public EcsPool<Pose> poses;
        public EcsPool<Velocity> velocities;
        public Aspect(Builder b)
        {
            b.Exclude<FreezedTag>();
            poses = b.Include<Pose>();
            velocities = b.Include<Velocity>();
        }
    }

    EcsDefaultWorld _world;
    TimeService _time;

    public void Inject(EcsDefaultWorld obj) => _world = obj;
    public void Inject(TimeService obj) => _time = obj;

    public void Run()
    {
        foreach (var e in _world.Where(out Aspect a))
        {
            a.poses.Get(e).position += a.velocities.Read(e).value * _time.DeltaTime;
        }
    }
}