Getting started with Entity Component System (ECS): Unity Tutorial

Entity Component System (ECS) is the new way of writing code in unity to handle the scene elements and its behavior. With ECS, unity is migrating from object-oriented to data-oriented design. ECS way of coding focuses on data and behavior of the game, which is the main problem for any developer.  Unlike traditional MonoBehaviour approach, it uses entities instead of the GameObjects.

In this article, first we will see an example with current MonoBehaviour approach to rotate an object and then we will implement the same example using hybrid ECS approach.

Advantage of Using Entity Component System

  • Entity Component System allows us to capitalize full advantage of multi core processor using C# job system and Burst compiler.
  • It is data oriented design which means
    • easy to read
    • easy to reuse the code

Current/ Traditional Approach of handling objects

The current approach allows you to create GameObjects and Monobehaviour classes. These MonoBehaviour classes contains the data and handles the behaviour of theses GameObjects.

For example, If we want to rotate a Cube object in the scene then

  • First we will create a Cube GameObject in the scene.
  • After that, we will create and attach a Monobehaviour script that will provide rotation functionality to the GameObject. It will also control the speed of the rotation.

Let’s see a simple script that will allow a GameObject to rotate along y axis

In the above script the Speed variable is the data. The logical code to provide behaviour to the GameObject is written inside the Update method. This behaviour provides rotation functionality to the object.

Entity Component System

Individual roles in the Entity Component System are:

  • Entity: entities are like lightweight GameObjects. It is the collection of components.
  • Component: components contains data only.
  • System: system is responsible for behaviours. It uses data from the components and provides behaviour to the different entities. It contains logical code that is required to process any behaviour.

Entity Component System can be used in two ways

  • Hybrid ECS: In hybrid ECS approach, you can use GameObjects and Monobehaviour scripts along with Entity, Components and System.
  • Pure ECS: In Pure ECS,
    • entities are used as the GameObjects
    • Data is stored in Components
    • Behavior is defined in the System

Add Entities Package in the Project (Unity 2018.2)

  • Change scripting run time version to .NET 4.x in the Configuration section of player settings (Edit -> Project Settings -> Player). It will ask to restart the unity editor.
  • Open package manager panel (Window -> Package Manager). Select All tab and then find and install Entities package.

You can check entity properties in the Entity debugger window (Window -> Debug -> Entity Debugger).

If there is no entity in the scene then this window will be empty.

Hybrid ECS Implementation

Preparing scene for the example

I am adding two primitive cube objects to the scene. We will see, how to rotate these two cube objects using hybrid entity component system approach.

Step 1: Entity

Unity automatically converts GameObjects to the entities. We just have to add Game Object Entity script to the GameObjects.

Once you will add Game Object Entity script to the GameObjects then you can see them as the Entities in the Entity Manager section of Entity Debugger window.

Here, we can see two entities for two Cube objects from the example.

Step 2: Component

MonoBehaviour scripts will be converted to the Components.

First, lets create a simple MonoBehaviour script to store data for controlling rotation speed.

Attach this script to both of the GameObejcts and provide the value of the rotation speed from the editor. For example I have added 100 for Cube1 and -100 for Cube2.

Now, create a struct that will behave as the Component.

In this struct, you can add all of the MonoBehaviour scripts and GameObject components that you want use in the next step. For now, we only need RotationBehaviour to get the speed of the object and Transform component to change its rotation.

Step 3: System

In ECS, the system should be

  • Derived from the ComponentSystem abstract class (present in Unity.Entities namespace).
  • Must implement OnUpdate() method, which is like the MonoBehaviour Update method.

Code for RotationSystem will look like:

Now, when we will play the application, Unity editor will automatically assigns the RotationSystem beahviour to the all entities.

 

Hope you get an idea about Entity Component System in unity. Post your comments for queries and feedback. Thanks for reading.

References

  • https://github.com/Unity-Technologies/EntityComponentSystemSamples
  • https://www.youtube.com/watch?v=_U9wRgQyy6s
  • https://www.youtube.com/watch?v=WLfhUKp2gag
Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Leave a Reply

Your email address will not be published. Required fields are marked *