Nano ECS by SinyavtsevIlya - 1

Frameworks

c#-Unity ECS framework

Unknown VersionOtherUpdated 67 days agoCreated on March 27th, 2019
Go to source

NanoECS

Discord

NanoECS - C#/Unity entity-component-system framework

Features

  • Code Generation (provides a really clean API, reducing boiler-plate code)
  • Reactive Components (gives you the ability to change component values in a pure manner, still providing reactive groups-triggering)
  • Visual Debugging (you can create/change contexts, entities and components inside the editor (Optional))
  • Unique components (singleton-like accessing components via contexts)

First look

Entity creation

        var player = contexts.Game.CreateEntity()	
            .AddPlayerView(view)
            .AddCollider(collider)
            .AddDefeatsCounter(0)
            .AddSkin("Butterfly");

Group

creation:

        GameGroup group = contexts.Game.GetGroup()
            .With.Position
            .With.View
            .Without.Movable;

usage:

	foreach (e in group) 
	{	
		Print(e.Position.Value);
		Print(e.View.Value);
		Print(e.View.IsMovable);
	}

Collector

creation:

        GameCollector collector = contexts.Game.GetGroup()
	    .With.Speed
            .With.Position
            .OnPositionChange();

usage:

	foreach (e in collector) 
	{	
		Print("My position has changed! : {0}", e.Position.Value);
	}
	collector.Clear();

Accessing component values

example 1:

        e.Position.Value += e.Direction.Value.ToVector3() * e.Speed.Value * delta;

example 2:

        foreach (var player in defeatedPlayers)
        {
            player.IsDestroyed = true;
            player.DefeatsCounter.Value++;

            if (!player.IsAIDriven)
            {
                contexts.Request.CreateEntity()
                    .AddDelay(0.6f)
                    .IsGameOverRequest = true;
            }
        }

Visual Debugging

entity editor entity editor

  • Reactive editing support (modifiyng component values gives the same result as “in code”)
  • GameObject linking (jump from the entity to the view game object and vice versa)
  • Lists, custom types, enums, Unity Objects are supported
  • Natural auto-complete workflow for adding new components, Foldouts, Create/Destroy buttons
  • Doesn’t affect realease builds performance. (And can be disabled / enabled manually in the settings)

Generation

entity editor

  • Generation works without reflection, so you can generate even if your project doesn’t compile at all
  • Doesn’t requere any manual actions from user to trigger generation. Just write the code and see how the new API appears. (Optional)
  • Customizable generation snippets.

Reactive components

Here’s how it works - you only create a template (or blueprint) of a component that looks like this:

class Score
{
    int value;
}

The generator parses this class, and creates a completely new component, with reactive properties instead of fields:

public partial class ScoreComponent : ComponentEcs 
{
	int value;
	
	public System.Action<int, int>OnValueChange;

	public int Value 
	{
		get {  return value; }
		set 
		{
			if (this.value == value) return;
			
			var cached = this.value;
			this.value = value;
			if (_InternalOnValueChange != null) 
			{	
				_InternalOnValueChange(0);
			}
			
			if (OnValueChange != null) 
			{
				OnValueChange(cached, value);
			}
		}
	}
}

When changing the value property, related entity is added to all .OnScoreChange collectors.

Inspiration

The framework is inspired a lot by such projects as Entitas, Actors, LeoECS. I like Entitas so much, but I found it’s really tricky to write “Replace” and filter-methods every-time. So the goal was to reduce boiler-plate and keep performance as well.

Documentation

Wiki

How to install

  • Create a new Unity Project
  • Open the manifest.json file in the Packages folder inside of the Project
  • Add "com.nanory.nanoecs": "https://github.com/SinyavtsevIlya/NanoECS.git", next to "dependencies": {
  • Go to Packages -> NanoECS -> Install and import ProjectStructure.unitypackage

Still in progress

  • It’s a very early version of framework. So bear with me 😃
  • Performance is tested in the mobile project (middle tier) with a lot of AI, render stuff etc. and it shows ~60 fps. I’m going to optimize it further, but it’s good enough for most purposes.
  • If you find a bug, let me know:
Show all projects by SinyavtsevIlya