UniLeo - Unity Conversion Workflow for Leopotam ECS
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
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",
using Voody.UniLeo;
[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
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
}
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.
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