LeoECS Lite Baking - Unity Conversion Workflow for Leopotam ECS Lite
Table of Contents
This package extends the functionality of the Leopotam ECS Lite library with tools for configuring entities through the Unity Inspector in scenes and prefabs.
- Open Source: This library is open source and free to use.
- Easy to use: Simply add
AuthoringComponent
to your component and addConvertScene
method to yourIEcsSystems
. - Convert Modes: You can choose how to convert **GameObjects to Entity.
- Prefab support: You can spawn Prefabs with
AuthoringComponent
and it will be converted to Entity after spawn. - Extensibility: Flexible architecture for extending functionality according to your needs.
- Entities-like: This library is similar to Unity.Entities conversion workflow.
- Lightweight: The library contains a small amount of code and has only one dependency.
- Declarative: You can control your component values within the Unity Inspector.
- Open the Unity Package Manager window.
- Click the + button in the upper right corner of the window.
- Select Add package from git URL....
- Enter the Leopotam ECS Lite repository link.
- Click Add.
- Repeat steps 2-5 for this repository.
Add the following lines to Packages/manifest.json
in the dependencies
section:
"com.leopotam.ecslite": "https://github.com/Leopotam/ecslite.git",
"com.leopotam.ecslite.baking": "https://github.com/g0dzZz-coder/ecslite.baking.git"
[Serializable] // <- Important to add Serializable attribute!
public struct Health
{
public float Value;
}
Now you need to control health value within the Unity Inspector,
but Unity Engine works only with MonoBehaviour
classes.
That mean you need to create AuthoringComponent
for our component.
- With the standard baker:
public sealed class HealthAuthoringComponent : AuthoringComponent<HealthComponent> { }
- Or with a custom one:
public sealed class HealthAuthoringComponent : AuthoringComponent<HealthComponent>
{
public override IBaker<HealthComponent> CreateBaker(EcsPackedEntityWithWorld entity) => new Baker(entity);
private sealed class Baker : IBaker<HealthComponent>
{
public void Bake(IAuthoring authoring)
{
// Implement your logic here.
}
}
}
- If you don't like the nesting of
Value
, you can create your own implementation ofIAuthoring
:
public sealed class HealthAuthoringComponent : MonoBehaviour, IAuthoring
{
[Min(0)] [SerializeField] private float _value;
public IBaker CreateBaker(PackedEntityWithWorld entity) => new Baker(_value, entity);
private readonly struct Baker : IBaker
{
private readonly float _value;
private readonly PackedEntityWithWorld _entity;
public Baker(float value, PackedEntityWithWorld entity)
{
_value = value;
_entity = entity;
}
void IBaker.Bake(IAuthoring authoring)
{
if (_entity.Unpack(out var world, out var entity))
{
world.Pool<Health>().Replace(entity, _value);
}
}
}
}
Add HealthAuthoringComponent
to the Inspector.
AuthoringEntity
will be automatically added to the GameObject.
This component is necessary for finding baked roots in the scene and store the packed entity from the ECS world.
Now you can configure component values within the Inspector. Congratulations!
⚠️ Currently, you cannot control values from the Inspector during Runtime.
You can choose how to convert GameObjects to Entity. Currently, there are 3 modes available:
Mode | Description |
---|---|
Convert and Inject | Simply creates entities with components based on GameObjects. |
Convert and Destroy | Deletes the GameObject after conversion. |
Convert and Save | Stores the associated GameObject as an entity in the AuthoringEntity Script. |
You can also retrieve the value from AuthoringEntity
:
if (_authoringEntity.TryGetEntity().HasValue)
{
_authoringEntity.TryGetEntity().Value;
}
To automatically convert GameObjects to Entity,
create (or use existing) IEcsSystems
and add the ConvertScene
method:
private void Start()
{
_world = new EcsWorld();
_systems = new EcsSystems(_world);
_systems
.ConvertScene() // <- Need to add this method.
.Add(new ExampleSystem());
_systems.Init();
}
ConvertScene
automatically scans the scene,
finds GameObjects with AuthoringEntity
and IAuthoring
,
creates an entity, and adds components to the Entity from the ECS world.
You can create prefabs with AuthoringComponent
,
and they will be converted to Entity
after creation.
Object.Instantiate(gameObject, position, rotation);
// Also works with 3rd party Assets:
PhotonNetwork.Instantiate(...)
Please, add ConvertScene
method after UnityEditor extensions:
#if UNITY_EDITOR
// Add debug systems for custom worlds here, for example:
.Add(new Leopotam.EcsLite.UnityEditor.EcsWorldDebugSystem())
#endif
.ConvertScene() // <- Need to add this method.
- Leopotam ECS Lite - the base ECS library.
I welcome feature requests and bug reports in the issues section, and I also accept pull requests.
I am an independent developer, and most of the development of this project is done in my free time. If you are interested in collaborating or hiring me for a project, please check out my portfolio and contact me!
This project is distributed under the Apache-2.0 license
Copyright (c) 2023 Nikolay Melnikov n.melnikov@depra.org