Skip to content

g0dzZz-coder/ecslite.baking

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LeoECS Lite Baking - Unity Conversion Workflow for Leopotam ECS Lite

License Last Commit Code Size

Table of Contents

🧾 Introduction

This package extends the functionality of the Leopotam ECS Lite library with tools for configuring entities through the Unity Inspector in scenes and prefabs.

💡 Features

  • Open Source: This library is open source and free to use.
  • Easy to use: Simply add AuthoringComponent to your component and add ConvertScene method to your IEcsSystems.
  • 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.

📥 Installation

📦 Using UPM:

  1. Open the Unity Package Manager window.
  2. Click the + button in the upper right corner of the window.
  3. Select Add package from git URL....
  4. Enter the Leopotam ECS Lite repository link.
  5. Click Add.
  6. Repeat steps 2-5 for this repository.

⚙️ Manual:

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"

📋 Usage Examples

Create a custom component

[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.

Create a new AuthoringComponent.

  1. With the standard baker:
public sealed class HealthAuthoringComponent : AuthoringComponent<HealthComponent> { }
  1. 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.
        }
    }
}
Inspector preview

Health Authoring Component

  1. If you don't like the nesting of Value, you can create your own implementation of IAuthoring:
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);
            }
        }
    }
}
Inspector preview

Health Authoring Component

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.

Selecting conversion mode

You can choose how to convert GameObjects to Entity. Currently, there are 3 modes available:

Inspector preview

Conversion Mode

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;
}

Converting Your GameObjects to Entity

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.

Spawning Prefabs

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(...)

Working with Unity Editor Extension

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.

🖇️ Dependencies

🤝 Collaboration

I welcome feature requests and bug reports in the issues section, and I also accept pull requests.

🫂 Support

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!

🔐 License

This project is distributed under the Apache-2.0 license

Copyright (c) 2023 Nikolay Melnikov n.melnikov@depra.org

Languages

  • C# 100.0%