Over the past year or so, there's been tons of hype over Unity ECS and the new C# Job System. But many details surrounding these two features remain shrouded in mystery.
This post will focus on Unity ECS, and aims to demystify exactly what it is and how it can benefit you. I've also created an accompanying video that contains many of these points, as well.
So let's start with the most obvious question. What is Unity ECS?
The Entity-Component-System Pattern
The "ECS" in Unity ECS stands for Entity-Component-System. ECS is an architectural pattern that follows the composition over inheritance principle.
To break it down, the Entity-Component-System pattern consists of three main elements that must meet the following specifications:
Entity - a general purpose object, that's typically nothing more than a unique ID
Component - a container that holds data pertaining to one aspect of an entity
System - an object that defines behavior for a group of entities with a predefined set of components
One of the biggest advantages of ECS is that it enforces a separation between your game's data and it's logic. This differs from the approach that is usually taken when writing code in Unity, which almost always consists of MonoBehaviours that contain both data and logic.
The Problem with Unity Code
During my time developing C# code for Unity, I've come across (and written) a lot of "spaghetti code".
If you haven't heard the term before now, then you owe it to yourself to look it up. I guarantee you'll get a good laugh. But basically, spaghetti code is any code that is unstructured and hard to maintain.
Unity projects tend to be riddled with this type of code because, for better or worse, there just isn't a single, well defined approach to programming video games. Web applications, on the other hand, have a very well defined approach, which typically consists of some flavor of the MV* pattern.
Sure, there are plenty of design patterns out there. But none have become the defacto standard to writing game code. Until now... maybe.
Unity ECS: The New Standard
I believe that the Entity-Component-System pattern could become the new standard approach to writing game code. It provides the answers to many questions that I've asked myself countless times throughout my "career" as a game developers.
Like, what's the best way to organize my code? How should I handle dependencies? And, how do I avoid giving a single class too much responsibility?
What I've found is that Unity ECS has the answer to most of these questions built-in!
Like, code should be organized into component classes that hold data and system classes that define behavior. Dependencies should be injected in the form of entities that contain a predefined set of components. And responsibilities should be divided evenly among the systems that make up your game logic.
And these answers only scratch the surface of the benefits that Unity ECS can provide.
Performance by Default
ECS actually has one really powerful code optimization built right in: batch processing.
When a system executes it performs logic on a group of entities, iterating over each one that meets the system's component requirements. This allows us to extract expensive calculations and data access calls so they only have to be performed once for a given group of entities.
But that's only beginning, because Unity ECS actually takes code optimization one set further thanks to the new C# Job System.
Systems that derive from JobComponentSystem receive all of the advantage that come with Unity's Job System. We're talking about amazing features like optimized memory layouts, multithreading, and extremely performant machine code compiled by the new Burst compiler.
When all is said and done, Unity ECS will provide performance by default, with very little effort on the part of the developer.
Whether you're hyped or not, it looks like Unity ECS is here to stay. Although we do have some time to prepare before it'll be ready for production code.
In the meantime, feel free to subscribe to my content to stay up to date on everything coming to Unity in 2018!