Firstly, we need to install Zenject and LeoEcs.
LeoEcs has two versions, one based on structures and one based on classes. Within the framework of this tutorial, it makes no difference which one to use, I will use the version on structures.
After installation, we need to create Scene Context in our Scene.
Right Click inside the Hierarchy tab and select Zenject -> Scene Context
Now, create three scripts: StartupInstaller
, StartupSystemsExecutor
, HelloWorldSystem
.
StartupInstaller will bind our Systems and EcsWorld to DI-container.
using Leopotam.Ecs;
using Zenject;
public class StartupInstaller : MonoInstaller
{
public override void InstallBindings()
{
Container.BindInstance(new EcsWorld());
Container.Bind<HelloWorldSystem>().AsSingle();
Container.BindInterfacesAndSelfTo<StartupSystemsExecutor>()
.AsSingle()
.NonLazy();
}
}
StartupSystemsExecutor will Init()
and Run()
binded systems.
using System;
using Leopotam.Ecs;
using Zenject;
public class StartupSystemsExecutor : IDisposable, ITickable
{
private readonly EcsSystems _systems;
private EcsWorld _world;
public StartupSystemsExecutor(EcsWorld ecsWorld, HelloWorldSystem helloWorldSystem)
{
_systems = new EcsSystems(ecsWorld);
_world = ecsWorld;
_systems.Add(helloWorldSystem);
_systems.Init();
}
public void Dispose()
{
_systems.Destroy();
_world.Destroy();
}
public void Tick()
{
_systems.Run();
}
}
HelloWorldSystem will print "Hello World" in Console.
using Leopotam.Ecs;
using UnityEngine;
public class HelloWorldSystem : IEcsInitSystem
{
public void Init()
{
PrintText("Hello World");
}
private void PrintText(string text)
{
Debug.Log(text);
}
}
We need to add StartupInstaller
to SceneContext which we created on scene.
And click Run