Convert Figma logo to code with AI

Unity-Technologies logoEntityComponentSystemSamples

No description available

7,083
1,595
7,083
62

Top Related Projects

7,081

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:

  1. 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.

  1. 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.

  1. 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:

  1. Clone the repository to your local machine:
git clone https://github.com/Unity-Technologies/EntityComponentSystemSamples.git
  1. Open the project in the Unity Editor (version 2020.3 or later).

  2. Explore the various sample projects located in the Assets/Samples directory, each showcasing different aspects of the ECS framework.

  3. Review the README.md file in the root directory for more detailed information about the samples and how to use them.

  4. Experiment with the samples, modify the code, and create your own ECS-based projects to further your understanding of this design pattern.

Competitor Comparisons

7,081

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 Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

DOTS 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:

  1. Video: The C# Job system (11 minutes)
  2. Video: ECS Entities and components (10 minutes)
  3. Video: ECS Systems (7 minutes)
  4. 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:

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:

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.