Skip to content

Latest commit

 

History

History
97 lines (73 loc) · 2.48 KB

README.MD

File metadata and controls

97 lines (73 loc) · 2.48 KB

How to write Hello World using LeoEcs & Zenject

Preparation

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.

Usage

After installation, we need to create Scene Context in our Scene. Right Click inside the Hierarchy tab and select Zenject -> Scene Context

Create-Scene-Context.png

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.

02-Startup-installer-added-to-scene-context.png

And click Run

Console-output.png