First Steps on creating a pure ECS Unity Project

This the very first step of creating your game in unity using pure ECS. I will describe here how i did it and some things i discovered during it

This tutorial contains basic concepts and decisions i made.

Project Configuration

The first thing you must check it is the .NET version in the Scripting Runtime Version, that can be found at the menu Edit -> Project Settings -> Player

A grand difference between classic projects and pure ecs projects as the moment i wrote this post it is that the editor it is not that ready yet. But what this means, it is not that clear how to configure the gameobjects and add ECS components as the ECS architecture needs.

Object Configuration

A project configuration nowaday since we will not configure the game objects directly in the editor needs to be done directly in the code

For things like asset references, we need to create a prototype GameObjects in the Scene, where we can add wrapped IComponentData types. Once we’ve finished the configuration, we can discard these prototype objects.

In the minigame plantjumper i will not use this approach since the level and player will be created procedurally.

Archetypes

As an ECS object, we must configure entities and components. In Unity we have a very interresting feature, archetypes, which is, as we are used in unity something like a prefab of a group of components.

PlayerArchetype = entityManager.CreateArchetype(
typeof(Position3D), 
typeof(PlayerInput), 
typeof(Health), 
typeof(TransformMatrix));

The sample above creates an archetype with pre created components

    public struct Health : IComponentData
    {
        public float Value;
    }

Some of the components are unity defaults, and then we don´t need to create then. In the example i gave here, the Position3D and TransformMatrix.

As you can notice the Health component i described it is a struct and not a class and this is not in vain. This is due to the fact that instances are passed as references and structs as value and then this is a good way to optimize memory because we will not handle unnecessary data.

I decided to create a bunch of component files because i want to reuse then in another games and a Factory of archetypes to create a level of abstractions.

Bootstrap

As the object creation lifecycle, i will overload the unity initialization, that can tipicaly be configured as a script attached to a gameobject.

The bootstrap script it is responsible to create the entities, associate its components and if necessary initialize some systems

References

Two Stick Shooter ECS