Unity incorporates the component based architecture in a seamless manner. However for some games a more data driven approach (entity system architecture) fits better. We had very good experience using entity system architecture for simulation and even match three games. Therefore when we switched our development to Unity, we created a small library and a couple of Unity editor extensions to make Unity work seamlessly with entity system architecture. We called it Entitas-CSharp. The code is open source and available on github. In this talk we would like to show why entity system architecture fits us better and how you can use Entitas-CSharp in your own Unity project.
19. Create Blocker Entity
public static Entity CreateBlocker(this Pool pool, int x, int y)
{
return pool.CreateEntity()
.IsGameBoardElement(true)
.AddPosition(x, y)
.AddResource(Res.Blocker);
}
23. +------------------+
| Pool |
|------------------|
| e e | +-----------+
| e e---|----> | Entity |
| e e | |-----------|
| e e e | | Component |
| e e | | | +-----------+
| e e | | Component-|----> | Component |
| e e e | | | |-----------|
| e e e | | Component | | Data |
+------------------+ +-----------+ +-----------+
|
|
| +-------------+
| | e | Groups
| | e e |
+---> | +------------+
| e | | |
| e | e | e |
+--------|----+ e |
| e |
| e e |
+------------+
32. Render Position System
public class RenderPositionSystem : IReactiveSystem {
public IMatcher GetTriggeringMatcher() {
return Matcher.AllOf(Matcher.View, Matcher.Position);
}
public GroupEventType GetEventType() {
return GroupEventType.OnEntityAdded;
}
public void Execute(Entity[] entities) {
foreach (var e in entities) {
var pos = e.position;
e.view.gameObject.transform.position = new Vector3(pos.x, pos.y);
}
}
}
50. Advantages
• Straightforward to achieve Determinism and therefore
Replay
• Simulation Speed (2x, 4x)
• Headless Simulation, just remove systems which rely on
GameObjects (render systems)
• Save Game (Serialization / Deserialization) send data to
backend on change
• For us it replaced all OO Design Patterns