Giter Club home page Giter Club logo

simple-ecs's Introduction

Simple ECS

This is a simple project that utilizes the core features of the new Entity Component System (ECS) that is currently in preview for Unity. In the project you move a player controlled ball around to collect score boxes. Everything is processed using DOTS and the C# Job System.

Since ECS is still under development, I will keep this project updated as things change and possibly become deprecated, as well as add new functionality.

Functionality

  • Implements the Entity Component System
  • Implements the C# Job System
  • Implements Unity Physics Collisions and Movement with C# Job System
  • Implements Hybrid Renderer
  • Implements Burst Compiler

Planned Features

  • Add NetCode Support
  • Convert More Systems to DOTS (Input, Camera, UI)
  • Add More Game Play Functionality

Changes

To view a log of all the previous changes DOTS and this project have gone through, please view the CHANGES.md file for details.

Getting Started

You can get started by cloning the repository on your desktop. Since ECS is in preview, you will need to follow some prerequisite steps in order to get things running.

Prerequisites

This project requires Unity 2020.2 or later. You will also need the following packages, which can be added through the Package Manager window.

Entities (com.unity.entities)
Hybrid Renderer (com.unity.rendering.hybrid)
Mathematics (com.unity.mathematics)
Physics (com.unity.physics)

Opening the Project

Once you have downloaded the repository to your computer, you can open up the DemoScene located in the Scenes folder.

Prefabs

The Prefabs folder contains game objects with Mesh Render and Mesh Filter scripts added to them. These game objects are used to be converted into entity prefabs by using the ConvertToEntity workflow.

The Prefabs folder contains prefabs for the environment objects, Player, and ScoreBox.

Scripts

The Scripts folder is divided into two sub-folders. One for Components, which are simply containers for data, and the other for Systems which perform the functionality. The SystemManager script is used as a Bootstrap script.

Hierarchy

The project hierarchy contains some simple and standard components. Most of these objects are converted to entities automatically through the ConvertToEntity workflow.

Entity Component System (ECS)

The traditional (or classic) way of working in Unity requires the use of MonoBehaviours on objects to perform some behaviour. They contain both the data and the logic. The problem that is build into the classic system is that the data becomes very scattered in memory. The loading time from memory to cache is very slow, and causes a lot of pointer misses.

With ECS, we can focus on processing only the data we need. There is no extra data that is processed, and it can be done in more than one thread.

C# Job System

ECS and the Job System perform very well together, giving performance by default in your code. ECS separates the data from the logic, by putting it into data containers called Components, and functionality into Systems.

namespace SimpleECS
{
    // Component data for movement speed of an entity.
    [GenerateAuthoringComponent]
    public struct MoveSpeed : IComponentData
    {
        public float Value;
    }
}

ECS

Using ECS requires a different way of thinking than the traditional object oriented system. The way you think about objects and Unity should change. An entity isn't a container, it is just a reference to data.

Systems contain all of the functionality. They process entities based on a filter, and provide performance by default.

namespace SimpleECS
{
    // System to apply a changing rotation to Entities with a RotationSpeed component
    [AlwaysSynchronizeSystem]
    public class RotationSystem : SystemBase
    {
        protected override void OnUpdate()
        {
            float deltaTime = Time.DeltaTime;

            Entities
                .WithBurst()
                .ForEach((ref Rotation rotation, in RotationSpeed rotationSpeed) =>
                {
                    rotation.Value = math.mul(rotation.Value, quaternion.RotateY(deltaTime * rotationSpeed.Value));

                })
                .ScheduleParallel();
        }
    }
}

Final Notes

The project is commented well and should provide detailed explanation on using ECS and the Job System. I think ECS is definitely something that should be transitioned to, as the benefits it offers simply cannot be overlooked. Looking forward to learn more as ECS development progresses. Also, here is a sample of the project running with 50,000 ScoreBox entities. I am pretty sure more could have been handled!

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

simple-ecs's People

Contributors

akanshdivker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

simple-ecs's Issues

Player movement graphics stutter

The player graphic stutters on movement with physics as the movement system. This is most notably visible with higher movement velocities.

Low frame rate for 50'000 scoreboxes

I have increased number of score boxes to 50'000 as you wrote,
you run app like this fluently.

In my case, I have about 3-6 fps on:

Intel(R) Core(TM) i5-9300HF CPU @ 2.40GHz 2.40 GHz
GeForce GTX 1650
32 GB RAM

The Unity version is the same as recommnded by you, burst compiler was
run. Same issue in editor, as in the PC release build.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.