UniLeo - Unity Conversion Workflow for Leopotam ECS
March 17, 2022 ยท View on GitHub
Easy convert GameObjects to Entity
Important! This repository is extension to Leopotam ECS - Engine independent ECS that works with any Game Engine. But Unity Engineers often ask how to integrate Leo with Unity Inspector and deal with Prefabs. This lightweight repository is intended to help with this.
Thanks to SinyavtsevIlya and Leopotam
How to start
First you need to install Leopotam ECS, you can do it with Unity Package Manager
Add new line to Packages/manifest.json
"com.leopotam.ecs": "https://github.com/voody2506/ecs.git",
Second install this repository
"com.voody.unileo": "https://github.com/voody2506/UniLeo.git",
How to add through Package Manager
Unity Editor -> Window -> Package Manager
Don't forget NameSpace
using Voody.UniLeo;
Create your first component
[Serializable] // <- Important to add Serializable attribute
public struct PlayerComponent {
public float health;
}
Now you need to control health value within the Unity Inspector, but Unity Engine works only with MonoBehavior classes. Thats mean you need to create MonoBehavior Provider for our component.
Create new script with Mono provider.
public sealed class PlayerComponentProvider : MonoProvider<PlayerComponent> { }
Add PlayerComponentProvider into Inspector
Inspector Preview

Now you can control component values within the Inspector. Congratulations!
At this moment you can not control values from Inspector at Runtime
Choose conversion method

Convert And Inject - Just creates entitie with components based on GameObject
Convert And Destroy - Deletes GameObject after conversion
Convert And Save - Stores associated GameObject entity in ConvertToEntity Script
// Than just get value from ConvertToEntity MonoBehavior
if (ConvertToEntity.TryGetEntity().HasValue) {
ConvertToEntity.TryGetEntity().Value
}
Convert your GameObjects to Entity
If you read the Leo's documentation, you know that for successful work with Leo ECS, you should to create Startup ECS Monobehavior. To Automatically convert GameObjects to Entity add ConvertScene() method.
void Start()
{
_world = new EcsWorld ();
_systems = new EcsSystems (_world);
_systems
.ConvertScene() // <- Need to add this method
.Add(new ExampleSystem())
// Other ECS Systems
.Init();
}
ConvertScene - method that automatically scan world, finds GameObjects with MonoProvider, creates entity and adds initial Components to the Entity.
Spawn Prefabs
Starting from 1.0.2 spawning prefabs aviabale from any Instantiate method, including 3rd party Assets
GameObject.Instantiate(gameObject, position, rotation);
PhotonNetwork.Instantiate <- works in 3rd party Assets
Every Prefab initialize with new entity. Components will be added automatically