Top Related Projects
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
Quick Overview
The Unity-Technologies/EntityComponentSystemSamples repository provides a collection of sample projects that demonstrate the usage of the Entity Component System (ECS) in the Unity game engine. ECS is a design pattern that focuses on data-oriented design, which can lead to significant performance improvements in complex game projects.
Pros
- Performance Optimization: The ECS approach emphasizes data-oriented design, which can result in better performance and scalability for large-scale game projects.
- Modular Design: ECS encourages a modular and composable approach to game development, making it easier to manage and extend complex systems.
- Parallelization: ECS lends itself well to parallelization, as the independent processing of components can be easily distributed across multiple threads.
- Flexibility: The samples showcase the versatility of ECS, demonstrating its applicability to a wide range of game mechanics and systems.
Cons
- Steep Learning Curve: Transitioning from the traditional GameObject-based approach to the ECS paradigm can be challenging, especially for developers new to the concept.
- Tooling and Ecosystem: The ECS tooling and ecosystem in Unity are still evolving, which can lead to some integration and workflow challenges.
- Complexity Management: As the complexity of the game project grows, managing the various systems and their interactions within the ECS framework can become more challenging.
- Compatibility: The ECS implementation in Unity is relatively new, and its compatibility with existing Unity features and third-party libraries may not always be seamless.
Code Examples
Here are a few code examples from the Unity-Technologies/EntityComponentSystemSamples repository:
- Creating an Entity and Adding Components:
// Create a new entity
var entity = EntityManager.CreateEntity();
// Add components to the entity
EntityManager.AddComponent<PositionComponent>(entity);
EntityManager.AddComponent<VelocityComponent>(entity);
EntityManager.AddComponent<RotationComponent>(entity);
This code demonstrates how to create a new entity and add various components to it, which is a fundamental aspect of the ECS pattern.
- Implementing a Movement System:
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
public class MovementSystem : SystemBase
{
protected override void OnUpdate()
{
float deltaTime = Time.DeltaTime;
Entities
.ForEach((ref PositionComponent position, in VelocityComponent velocity) =>
{
position.Value += velocity.Value * deltaTime;
})
.ScheduleParallel();
}
}
This code shows an example of a MovementSystem
that updates the position of entities based on their velocity, leveraging the parallel processing capabilities of ECS.
- Handling Collisions:
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
public class CollisionSystem : SystemBase
{
protected override void OnUpdate()
{
Entities
.WithAll<ColliderComponent, PositionComponent>()
.ForEach((Entity entity, ref ColliderComponent collider, in PositionComponent position) =>
{
// Perform collision detection and response logic
})
.ScheduleParallel();
}
}
This code snippet demonstrates a CollisionSystem
that processes the collider components and positions of entities to handle collision detection and response.
Getting Started
To get started with the Unity-Technologies/EntityComponentSystemSamples repository, follow these steps:
- Clone the repository to your local machine:
git clone https://github.com/Unity-Technologies/EntityComponentSystemSamples.git
-
Open the project in the Unity Editor (version 2020.3 or later).
-
Explore the various sample projects located in the
Assets/Samples
directory, each showcasing different aspects of the ECS framework. -
Review the README.md file in the root directory for more detailed information about the samples and how to use them.
-
Experiment with the samples, modify the code, and create your own ECS-based projects to further your understanding of this design pattern.
Competitor Comparisons
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
Pros of Entitas
- More mature and battle-tested, with a larger community and ecosystem
- Provides a complete ECS framework with code generation tools
- Offers a visual debugger for easier system inspection and debugging
Cons of Entitas
- Less integrated with Unity's latest DOTS (Data-Oriented Technology Stack)
- May have a steeper learning curve for beginners
- Performance might not be as optimized as Unity's native ECS implementation
Code Comparison
Entitas:
public class MovementSystem : IExecuteSystem {
readonly IGroup<GameEntity> _movers = Contexts.sharedInstance.game.GetGroup(GameMatcher.AllOf(GameMatcher.Position, GameMatcher.Velocity));
public void Execute() {
foreach (var e in _movers) {
e.position.value += e.velocity.value * Time.deltaTime;
}
}
}
EntityComponentSystemSamples:
public class MovementSystem : SystemBase {
protected override void OnUpdate() {
float deltaTime = Time.DeltaTime;
Entities.ForEach((ref Translation position, in Velocity velocity) => {
position.Value += velocity.Value * deltaTime;
}).ScheduleParallel();
}
}
The main differences are in syntax and how systems are defined and executed. Entitas uses a more traditional object-oriented approach, while Unity's ECS leverages job system and Burst compiler for better performance.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
DOTS Samples
- Entities samples
- Netcode samples
- Physics samples
- Entities.Graphics HDRP samples
- Entities.Graphics URP samples
Learning DOTS
For those new to DOTS, here's the recommended sequence to follow through the introductory material in the Entities samples project:
A few short videos introduce the basic concepts of the job system and ECS:
- Video: The C# Job system (11 minutes)
- Video: ECS Entities and components (10 minutes)
- Video: ECS Systems (7 minutes)
- Video: ECS Baking (6 minutes)
You may also want to read the Entities API overview, which is briefer and more sequentially structured than the manual.
These starter samples each have an explanatory video:
- The Jobs Tutorial sample (17 minute walkthrough video) demonstrates creation and scheduling of jobs.
- The HelloCube samples (30 minute walkthrough video) demonstrate very basic Entities usage, such as creating and moving rendered entities in systems and jobs.
- The Tanks tutorial (23 minute walkthrough video) puts the basic elements of Entities and jobs together to demonstrate a small simulation.
- The Kickball tutorial (55 minute walkthrough video) also demonstrates a small simulation, but with a bit more depth.
- The StateChange sample (14 minute walkthrough video) demonstrates three different ways to handle state representation in Entities.
Beyond the above starter samples, there are samples covering Baking, Streaming (for large worlds and scene management), and Miscellaneous.
For quick reference of basic API usage, use these example code snippets and cheat sheets:
- Example code: jobs
- Example code: components and systems
- Example code: baking
- Cheat sheet: collections
- Cheat sheet: mathematics
Finally, there's the ECS Network Racing sample, which is a working DOTS game using DOTS Netcode and Physics.
Release notes
This is the samples release for Unity 2022.3 LTS and the 1.2 release of the Entities
, Netcode
, Physics
, and Entities.Graphics
packages.
Top Related Projects
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot