UniLeo - Unity Conversion Workflow for Leopotam ECS Lite
Important! This repository is extension to Leopotam ECS Lite - 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 Lite, you can do it with Unity Package Manager
Add new line to Packages/manifest.json
"com.leopotam.ecslite": "https://github.com/voody2506/ecslite.git",
Second install this repository
"com.voody.unileo.lite": "https://github.com/voody2506/UniLeo-Lite.git",
using Voody.UniLeo.Lite;
[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.
GameObject.Instantiate(gameObject, position, rotation);
PhotonNetwork.Instantiate <- works in 3rd party Assets
Every Prefab initialize with new entity. Components will be added automatically
Working with Leo's Unity Editor Extension
Please, add ConvertScene method after UnityEditor extension
#if UNITY_EDITOR
// add debug systems for custom worlds here, for example:
// .Add (new Leopotam.EcsLite.UnityEditor.EcsWorldDebugSystem ("events"))
.Add (new Leopotam.EcsLite.UnityEditor.EcsWorldDebugSystem ())
#endif
.ConvertScene() // <- Need to add this method