The new ECS

The ECS is a brave and ambitious attempt to redesign the core foundation of Unity's design: the GameObject-MonoBehaviour paradigm. As you can imagine, changing the base design pattern of every object in the game is not an easy task. So you may ask: Why?

There are several reasons for that. Let's look at some of them objectively: 

  • First, as we said before, GameObject and MonoBehaviour are heavy objects; they carry a lot of internal code and data structures. The overhead introduced by GameObject instances and MonoBehaviour is large enough to limit the number of objects  you can have on the screen more than the resources needed to render them. That's not a good thing for an abstraction model.
  • Second, MonoBehaviour instances are scattered in memory. This means that GameObject needs to look around in memory to retrieve all the MonoBehaviour instances it is connected to, and that the system relies on references. This has two problems: it makes caches very inefficient and, more important, it is a problem when we want to use GameObject instances in a massive multithreading application, for instance, by using jobs (we have seen that jobs cannot use references safely).
  • Last, but not least, MonoBehaviour instances have a problem from a code design point of view: they store both data and behavior. This is not a huge problem. After all, a lot of amazing games have been shipped using this paradigm. However, it is common in software architecture to separate the data (often called the model) and the algorithm that uses the data (often called the controller). 

The ECS, on the other hand, goes in the direction of separating the data from the behavior. It is based on three different components:

  • An entity is just defined by its set of components. There is literally zero abstraction here.
  • A component is purely data. A Health component contains only the life points; a Shield component contains only the number of shields; a Rotation component contains only the object orientation, and so on.
  • A system defines the behavior of entities. A system applies a specific behavior to every entity containing a particular set of components. For instance, MoveAndRotateEnemy may apply translation and rotation to every entity with the Rotation, Translation, and Enemy components.

Everything is now in its own place.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.125.7