Pure C# Entity Component System framework
Readme Languages:
Русский
English
中文The ECS Framework aims to maximize usability, modularity, extensibility and performance for dynamic entity changes, without code generation or external dependencies. Inspired by LeoEcs Lite.
[!Note] Have questions or want to stay updated on development? Join our community! -> Feedback
Versioning semantics - Open
Requirements:
Supported:
Tested with:
It is also recommended to install the Unity engine integration extension Unity integration
Installation as a Unity package is supported by adding the Git URL in PackageManager:
https://github.com/DCFApixels/DragonECS.gitOr add the package entry to Packages/manifest.json:
"com.dcfa_pixels.dragonecs": "https://github.com/DCFApixels/DragonECS.git",The framework sources can be copied directly into the project.
*Your extension? Extensions for DragonECS are welcome, you can share it here.
These two scripts bring together the basic DragonECS workflow: create a world, build a pipeline, inject dependencies, run systems, query entities by an aspect, edit components through pools, and release resources.
EcsRoot.cs
…MovementSystem.cs
…Container for components. Two identifier types are used to reference entities:
int - short-lived identifier, valid within a single tick. Not recommended for long-term storage;entlong - long-term identifier that includes a generation tag, making it unique across entity lifetimes. Suitable for long-term storage.// Creating a new entity in the world.
int entityID = _world.NewEntity();
// Deleting an entity.
_world.DelEntity(entityID);
// Copying components from one entity to another.
_world.CopyEntity(entityID, otherEntityID);
// Cloning an entity.
int newEntityID = _world.CloneEntity(entityID);Working with entlong
// Convert int to entlong.
entlong entity = _world.GetEntityLong(entityID);
// or
entlong entity = (_world, entityID);
// Check that the entity is still alive.
if (entity.IsAlive) { }
// Converting entlong to int. Throws an exception if the entity no longer exists.
int entityID = entity.ID;
// or
var (entityID, world) = entity;
// Converting entlong to int. Returns true and the int identifier if the entity is still alive.
if (entity.TryGetID(out int entityID)) { }Entities cannot exist without components. Removing the last component automatically deletes the entity along with it.
Data for entities.
// IEcsComponent components are stored in regular storage.
struct Health : IEcsComponent
{
public float health;
public int armor;
}
// Components with IEcsTagComponent are stored in tag-optimized storage.
struct PlayerTag : IEcsTagComponent {}Represents the core logic defining entity behaviors. Systems are implemented as user-defined classes that implement one or more process interfaces. Key processes include:
class SomeSystem : IEcsPreInit, IEcsInit, IEcsRun, IEcsDestroy
{
// Called once during EcsPipeline.Init() and before IEcsInit.Init().
public void PreInit () { }
// Called once during EcsPipeline.Init() and after IEcsPreInit.PreInit().
public void Init () { }
// Called each time during EcsPipeline.Run().
public void Run () { }
// Called once during EcsPipeline.Destroy().
public void Destroy () { }
}For implementing additional processes, refer to the Processes section.
Container and engine of systems. Responsible for setting up the system call queue, providing mechanisms for communication between systems, and dependency injection. Implemented as the EcsPipeline class.
Builder is responsible for building the pipeline. Systems are added to the Builder, and the pipeline is built at the end. Example:
EcsPipeline pipeline = EcsPipeline.New() // Creates a pipeline builder.
// Adds System1 to the systems queue.
.Add(new System1())
// Adds System2 to the queue after System1.
.Add(new System2())
// Adds System3 to the queue after System2, as a unique instance.
.AddUnique(new System3())
// Completes the pipeline building and returns its instance.
.Build();
pipeline.Init(); // Initializes the pipeline.class SomeSystem : IEcsRun, IEcsPipelineMember
{
// Gets the pipeline instance to which the system belongs.
public EcsPipeline Pipeline { get ; set; }
public void Run () { }
}For simultaneous building and initialization, use
Builder.BuildAndInit().
The framework implements dependency injection for systems. Injection process occurs during pipeline initialization and injects data passed to the Builder.
Built-in Dependency injection is optional.
…Groups of systems that implement a common feature can be grouped into modules and added to the Pipeline.
using DCFApixels.DragonECS;
class Module1 : IEcsModule
{
public void Import(EcsPipeline.Builder b)
{
b.Add(new System1());
b.Add(new System2());
b.AddModule(new Module2());
// ...
}
}EcsPipeline pipeline = EcsPipeline.New()
// ...
.AddModule(new Module1())
// ...
.BuildAndInit();To manage system order in the pipeline regardless of addition order, use Layers and Sorting Order.
Queues of systems can be segmented into layers. A layer defines a position in the queue for inserting systems. For example, to insert a system at the end of the queue regardless of where it is added, add it to the EcsConsts.END_LAYER layer.
const string SOME_LAYER = nameof(SOME_LAYER);
EcsPipeline pipeline = EcsPipeline.New()
// ...
// Inserts a new layer before the end layer EcsConsts.END_LAYER
.Layers.Add(SOME_LAYER).Before(EcsConsts.END_LAYER)
// Inserts SOME_LAYER_2 layer at the position after EcsConsts.BASIC_LAYER
.Layers.Add(SOME_LAYER).After(EcsConsts.BASIC_LAYER)
// System SomeSystem will be added to the SOME_LAYER layer
.Add(new SomeSystem(), SOME_LAYER)
// ...
.BuildAndInit();The built-in layers are arranged in the following order:
EcsConsts.PRE_BEGIN_LAYEREcsConsts.BEGIN_LAYEREcsConsts.BASIC_LAYER (Systems are added here if no layer is specified during addition)EcsConsts.END_LAYEREcsConsts.POST_END_LAYERThe sort order integer value is used to sort systems within a layer. By default, systems are added with sortOrder = 0.
EcsPipeline pipeline = EcsPipeline.New()
// ...
// System SomeSystem will be inserted into the layer EcsConsts.BEGIN_LAYER
// and placed after systems with sortOrder less than 10.
.Add(new SomeSystem(), EcsConsts.BEGIN_LAYER, 10)
// ...
.BuildAndInit();Processes are queues of systems that implement a common interface, such as IEcsRun. Runners are used to start processes. Built-in processes are started automatically; custom processes can be implemented.
Built-in processes
IEcsPreInit, IEcsInit, IEcsRun, IEcsDestroy - lifecycle processes of EcsPipeline.IEcsInject - Dependency Injection processes.IOnInitInjectionComplete - Similar to the Dependency Injection process, but signals completion of initialization injection.Custom Processes
To add a new process, create an interface inheriting from IEcsProcess and create a runner for it. A runner is a class that implements the process interface and inherits from EcsRunner. Example:
// Process interface.
interface IDoSomethingProcess : IEcsProcess
{
void Do();
}
// Runner implementation. An example of implementation can also be seen in built-in processes.
sealed class DoSomethingProcessRunner : EcsRunner, IDoSomethingProcess
{
public void Do()
{
foreach (var item in Process) item.Do();
}
}// Adding the runner when creating the pipeline
_pipeline = EcsPipeline.New()
//...
.AddRunner()
//...
.BuildAndInit();
// Running the runner if it was added
_pipeline.GetRunner().Do();
// or if the runner was not added (calling GetRunnerInstance will also add the runner to the pipeline).
_pipeline.GetRunnerInstance().Do();Advanced Implementation of a Runner
…Runner requirements:
- Inheritance from
EcsRunnermust be direct.- A runner may only contain one interface (excluding
IEcsProcess).- The inheriting class of
EcsRunnermust also implement theTinterface.
A container for entities and components.
// Creating an instance of the world.
_world = new EcsDefaultWorld();
// Creating and deleting an entity as shown in the Entities section.
var e = _world.NewEntity();
_world.DelEntity(e);NOTICE: Call
EcsWorld.Destroy()on the world instance when it is no longer needed to release resources. Otherwise it will remain in memor
No open issues yet, or sync has not completed.