- Component is a structure that consist of one or more computable element, for example: Position, Sprite, Velocity, Input, AI, etc.
- Entity is collection of Components, for example:
- Player (consist of Position, Velocity, Sprite, Input),
- Enemy (consist of Position, Velocity, Sprite, AI),
- Tree/Pillar (consist of Position, Sprite), etc.
- System is a function that process components in a batch, for example:
- Movement (uses Position, Velocity),
- Render (uses Position, Sprite),
- PlayerControl (uses Player.Velocity, Input)
- Bot (uses Enemy.Velocity, Player.Position)
In Unity, the latest stable version (2018.1.0, 2018-05-02) at the time of this article written have built in support for ECS. The benefit of using built-in ECS is the new Job System (utilizing multi-core and cache-friendly, automatic scheduling, prevent race condition) and Burst Compiler that able to utilize SIMD. There's another alternative for ECS that known long time ago if you don't want to use Unity's, such as Entitas framework (which has best documentation among others), Artemis (C# version), Leopotam, and EgoECS.
To use the Unity ECS, there's two method that can be used:
- Hybrid, to create hybrid ECS, all you need to do is add an EntityGameObject script on the prefab.
- Pure, you must create an entity manually.
To start any type of ECS project, all you must do is edit Packages/manifest.json, update to something like this:
{
"dependencies": {
"com.unity.incrementalcompiler": "0.0.38",
"com.unity.entities": "0.0.12-preview.1"
},
"registry": "https://staging-packages.unity.com",
"testables": [
"com.unity.collections",
"com.unity.entities",
"com.unity.jobs"
]
}
Then you must go to Edit > Project Settings > Player, Other Settings, Scripting Runtime Version to 4.x. Also it's better to use il2cpp than Mono for the scripting backend.
To start a pure ECS, first you must decompose all Component to structs, for example:
Then create a system to manipulate the values:
Add the GameBootstrap to the main camera or the canvas (don't forget to add the canvas). Set these values on your Unity:
- Main camera > projection to "perspective"
- Canvas
- Canvas Scaler reference resolution to 800 x 600 (or whatever you like)
- Screen match ratio to 0.5
- X rotation to 90, render mode to "screen space - camera"
- Drag your main camera to the "Render Camera"
- Create a directory "Resources/" inside "Assets/" and add a ball.png image with transparency, in my case I used 600x600 image.
- Install or git clone SpriteInstanceRenderer component to render the sprite using ECS. This component requires TransformMatrix, Heading2D and Position/Position2D component to render the sprite.
ecs examples
ReplyDeletehttps://youtu.be/KzFHG7PlTU8
both hybrid and pure ecs
unity ecs physics | movement and adding force to rigidbody
ReplyDeletehttps://youtu.be/wCmz0tahRO0
if you're new to ECS (Pure):
ReplyDeletehttps://youtu.be/VgeVpCYzrSM