ECS For All:進化するUnityのゲームプレイアーキテクチャ

5.1K Views

November 16, 26

スライド概要

profile-image

リアルタイム3Dコンテンツを制作・運用するための世界的にリードするプラットフォームである「Unity」の日本国内における販売、サポート、コミュニティ活動、研究開発、教育支援を行っています。ゲーム開発者からアーティスト、建築家、自動車デザイナー、映画製作者など、さまざまなクリエイターがUnityを使い想像力を発揮しています。

Docswellを使いましょう

(ダウンロード不可)

関連スライド

各ページのテキスト
2.

ROADMAP Safe Harbor Statement The following is intended for informational purposes only, and may not be incorporated into any contract. No purchasing decisions should be made based on the following materials. Unity is not committing to deliver any functionality, features or code. The development, timing and release of all products, functionality and features are at the sole discretion of Unity, and are subject to change. Nyhavn at sunset Source: Moahim, Creative Commons Attribution-Share Alike 4.0 International

3.

Joe Valenzuela Christopher Redden Senior Director, Core Engine Principal Software Engineer

4.

→ ECS for All → Phase 1 ⎯ Data-Oriented Design DOD ⎯ DOD at Unity ⎯ “DOTSˮ and Entities Phase 2 ⎯ TKTK

5.

Data Oriented Design

6.

Data Oriented Design Unite/Tokyo Background Performance relative to 1980 (log scale) 10K +20%/yr CPU 1K 100 10 +52%/yr +25%/yr DRAM +7%/yr 1 1980 1985 1990 1995 2000 2005 2010 Source: J. L. Hennessy and D. A. Patterson, Computer Architecture: A Quantitative Approach, 5th ed., Morgan Kaufmann, 2012 “50 Years of Microprocessor Trend Data” by Karl Rupp, licensed under CC BY 4.0 Source: github.com/karlrupp/microprocessor-trend-data

7.

Data Oriented Design Unite/Tokyo Designing data CORE CORE CORE CORE Common program operations Source: 6it.dev, “Infographics: Operation Costs in CPU Clock Cycles”, https://6it.dev/blog/infographics-operation-costs-in-cpu-clock-cycles-741, September 12, 2016

8.

Data Oriented Design Unite/Tokyo Designing data free CORE CORE CORE CORE Control unit Control unit Control unit Control unit ALU ALU ALU ALU Registers Registers Registers Registers 32 kb (1 ns) Instruction 256kb (3-4 ns) Level 2 cache 16 mb (10-23 ns) 32 GB (~60 ns) data Instruction data Instruction Level 2 cache data Level 2 cache Level 3 shared cache Memory Instruction data Level 2 cache

9.

Data Oriented Design Unite/Tokyo Designing data // x0, y0, z0, x1, y1, z1, ... float[] positions = new float[3 * count]; // scale Y for (int i = 0; i < count; ++i) positions[i * 3 + 1] *= 0.5f; positions 0x00 (x0) 0x04 (y0) 0x08 (z0) 0x0C (x1) 0x10 (y1) 0x14 (z1) 0x18 (x2) 0x1C (y2) 0x20 (z2) 0x24 (x3) 0x28 (y3) 0x2C (z3)

10.

Unite/Tokyo Data Oriented Design Starting with data Ideal solution Input Transform Output

11.

Unite/Tokyo Data Oriented Design Starting with data ⚠ Ideal solution Input Transform Output x0 y0 z0 w0 x0 x1 x2 x3 x1 y1 z1 w1 y0 y1 y2 y3 x2 y2 z2 w2 z0 z1 z2 z3 x3 y3 z3 w3

12.

Unite/Tokyo Data Oriented Design Starting with data Ideal solution Input OOP Solution Input Transform Output Output

13.

Unite/Tokyo Data Oriented Design Starting with data Ideal solution Input OOP Solution Input Transform Output Output

14.

Unite/Tokyo Data Oriented Design Starting with data Ideal solution Input Transform Output Or is it?

15.
[beta]
Unite/Tokyo

Data Oriented Design

Designing data
●

How many instances?

int test_intersection(Sphere s, Plane p)
{
const float num = fabsf(p.x * s.xc + p.y * s.yc + p.z * s.zc);
const float den = sqrtf(p.x * p.x + p.y * p.y + p.z * p.z );
const float dis = num / den;
return dis <= s.radius ? -1 : 1;
}

void test_intersections(int* dest, const Sphere* s,
int num_spheres, Plane p)
{
const float den = sqrtf(p.x * p.x + p.y * p.y + p.z * p.z);
for (int i=0; i<num_spheres; ++i)
{
const float num = fabsf(p.x * s.xc + p.y * s.yc + p.z * s.zc);
const float dis = num / den;
dest[i] = dis <= s->radius ? -1 : 1;
}
}

16.
[beta]
Unite/Tokyo

Data Oriented Design

Designing data
●

How many instances?

int test_intersection(Sphere s, Plane p)
{
const float num = fabsf(p.x * s.xc + p.y * s.yc + p.z * s.zc);
const float den = sqrtf(p.x * p.x + p.y * p.y + p.z * p.z );
const float dis = num / den;
return dis <= s.radius ? -1 : 1;
}

void test_intersections(int* dest, const Sphere* s,
int num_spheres, Plane p)
{
const float den = sqrtf(p.x * p.x + p.y * p.y + p.z * p.z);
for (int i=0; i<num_spheres; ++i)
{
const float num = fabsf(p.x * s.xc + p.y * s.yc + p.z * s.zc);
const float dis = num / den;
dest[i] = dis <= s->radius ? -1 : 1;
}
}

17.

Unite/Tokyo Data Oriented Design Designing data ● What changes and what doesnʼt? struct enemy { char name[32]; int enemy_type; CollisionTris collision; int last_visible; matrix4x4 po; float health; }

18.

Unite/Tokyo Data Oriented Design Designing data ● What changes and what doesnʼt? struct enemy { char name[32]; int enemy_type; CollisionTris collision; int last_visible; matrix4x4 po; float health; }

19.

Unite/Tokyo Data Oriented Design Designing data ● How and how often is your data used? struct marker { matrix4x4 po ; float health; char team_alignment[8]; char* name ; };

20.

Unite/Tokyo Data Oriented Design Designing data ● How and how often is your data used? struct marker { matrix4x4 po ; float health; char team_alignment[8]; char* name ; };

21.

Unite/Tokyo Data Oriented Design Designing data ● How and how often is your data used? struct marker { matrix4x4 po ; float health; char team_alignment[8]; char* name ; };

22.

Unite/Tokyo Data Oriented Design Designing data ● What can be shared? “Mushroom Warrior” struct enemy { char* name; enemy_class * klass; int last_visible ; matrix4x4 po; float health; } struct enemy_class { int type; CollisionTris collision; }

23.

Unite/Tokyo Data Oriented Design Designing data ● Whatʼs the range and distribution? struct foo { int bar; }; // ? ? ? ? ? ?

24.

Unite/Tokyo Data Oriented Design Designing data ● Whatʼs the range and distribution? struct foo { int bar; }; // { 100, 102, 101, 104, 103, 105, 104 } 100 102 101 104 103 105 104 BASE VAL DELTA DELTA DELTA DELTA DELTA DELTA 100 +2 -1 +3 -1 +2 -1

25.

Unite/Tokyo Data Oriented Design Designing data ● How much accuracy? p(x)= 0.99999999999565793 − 0.49999999798761679 * x + 0.04166652049177614 x^2 − 0.0013854855078607044 x^3

26.

Unite/Tokyo Data Oriented Design Designing data ● How much accuracy? p(x)= 0.99999999999565793 − 0.49999999798761679 * x + 0.04166652049177614 x^2 − 0.0013854855078607044 x^3

27.

Unite/Tokyo Data Oriented Design Designing data ● How complex is your data? Batch #0 Batch #1

28.

Unite/Tokyo Data Oriented Design Designing data ● How complex is your data? Batch #0 Batch #1

29.

DOTS milestones DOTS The past pre-DOTS Nov 2017 - Nov 2020 Early DOTS Present Fall 2020 - Nov 2026 Entities ECS For All

30.
[beta]
GameObjects
GameObject

GameObject
Transform

Transform
RigidBody
MeshRenderer

Speed

public class Speed : MonoBehaviour {
public float m_Speed;
public float m_Time;
void Update() {
if (m_Speed > 88.0f)
m_Time -= 30.0f;
}
}

MeshRenderer

31.
[beta]
GameObjects
GameObject

GameObject
Transform

Transform
RigidBody
MeshRenderer

Speed

public class Speed : MonoBehaviour {
public float m_Speed;
public float m_Time;
void Update() {
if (m_Speed > 88.0f)
m_Time -= 30.0f;
}
}

MeshRenderer

32.

DOTS Unite/Tokyo DOTS features and packages Gameplay Physics Rendering [...] Entities Graphics, Netcode for Entities, Unity Physics Entity Component System ECS Architectural Pattern Entities Package Scheduling C# Job System Compilation Burst Compiler Galaxy Game DOTS Samples ECS Character + Vehicle Controller Packages

33.

Unite/Tokyo DOTS ECS Archetypes/Chunks Archetype 0 Archetype #0 Chunk #2 Entity #11 Health Archetype 1 Chunk 0 Chunk 0 Chunk 1 Chunk 1 Chunk 2 Chunk 2 Navigation

34.

DOTS Unite/Tokyo ECS gameplay architecture - IComponentData public struct Movement : IComponentData { public float3 Direction; public float Speed; }

35.
[beta]
Unite/Tokyo

DOTS

ECS Systems
Archetype 0

public partial struct HealthSystem : ISystem {
public void OnUpdate(ref SystemState state)
{
// Iterate over all entities with a
// mutable Health component
foreach (var (health, entity) in
SystemAPI.Query<RefRW<Health>>()
.WithEntityAccess())
{
// ...
}
}
}

Archetype 1

Chunk 0

Chunk 0

Chunk 1

Chunk 1

Chunk 2

Chunk 2

36.

Unite/Tokyo DOTS ECS Queries Archetype 0 Archetype 1 Chunk 0 Chunk 0 Chunk 1 Chunk 1 Chunk 2 Chunk 2 With orange, without blue Query X

37.

Unite/Tokyo DOTS Entities 1.0 Results based on testing performed on a laptop with a high-end Intel CPU and NVIDIA GPU, Unity 2022.3, Entities 1.2.0-pre.6. Results may vary.

38.

Unite/Tokyo DOTS Learning ECS be like ⚠ State of things as of Entities 1.x ● Requires installing DOTS and ECS packages ● Requires using a separate Hierarchy window ● There are confusing blinking yellow dots everywhere ● Requires SubScenes to do almost everything ● Canʼt bind between things from main scene to SubScene ● Must use Baking ● Requires intermediate ‘authoringʼ type ● Canʼt use Timeline ● Many built-in types donʼt Bake (not obvious) ● Must use Entities SceneManager ● Player builds use StreamingAssets ● Cannot use addressables ● Asset duplication between Entity Scenes & GameObject Scenes ● Cannot progressively migrate scene content to Entities ● Working between Entities & GameObjects together very difficult ● Many things fail because of all the jank: WebGL, Lights Baking, Fog, etc

39.

Phase II Where are we, and where are we going

40.

Unite/Tokyo Where are we Where we are The past Nov 2017  Nov 2020 Fall 2020  Nov 2026 Present pre-DOTS Early DOTS Entities ECS For All We are here

41.

Unite/Tokyo Where are we What still holds people back Setup Authoring Content Interop • Separate DOTS and ECS packages • A separate Hierarchy window • SubScenes for anything basic • Baking and intermediate ‘authoringʼ types • Many built-in types donʼt bake • No Timeline • Entities SceneManager • StreamingAssets in player builds • No Addressables • Assets duplicated across scene types • No progressive migration • Entities and GameObjects together is hard • WebGL, Lights Baking, Fog break

42.

Unite/Tokyo Where are we Today: a canyon ECS GameObjects Limited, but powerful The full utility of Unity Baking, SubScenes • Burst Compiler • C# Job System • Entities Graphics, Netcode for Entities, Unity Physics • Designed for performance at scale • Every built-in component • Timeline • Addressables • Lights Baking, Fog, WebGL

43.

The original plan ECS was always meant to be the new foundation for the rest of Unity. But the path there had become unclear.

44.

Unite/Tokyo A challenge Where are we

45.

Unite/Tokyo Where are we A challenge “I would believe in this if an Entity and a GameObject could share a Transform system.ˮ

46.

He knew this was in fact very complicated. He was trying to making a point about how far away we were.

47.

He knew this was in fact very complicated. He was trying to making a point about how far away we were. But we took a moment to think about it...

48.

He knew this was in fact very complicated. He was trying to making a point about how far away we were. But we took a moment to think about it... “Actually, why not?ˮ

49.

Instead of bringing the rest of Unity to ECS, we will bring ECS to the rest of Unity. The rest of Unity ECS ECS The rest of Unity

50.

How?

51.

What is a GameObject? Under the hood)

52.

Unite/Tokyo ECS for All What a GameObject really is C# GameObject Thin wrapper (a pointer) C GameObject The real object and its memory

53.

Unite/Tokyo ECS for All What a GameObject really is C# GameObject Thin wrapper (a pointer) GameObject The real object and its memory C GameObject

54.

Unite/Tokyo ECS for All What a GameObject really is C# GameObject GameObject GameObject Transform, Light, Rigidbody… Built-in components Thin wrapper (a pointer) C The real object and its memory

55.

Unite/Tokyo ECS for All What a GameObject really is C# GameObject Built-in components Thin wrapper (a pointer) C GameObject GameObject Transform, Light, Rigidbody Transform, Light, Rigidbody… The real object and its memory

56.

Unite/Tokyo ECS for All What a GameObject really is C# GameObject Built-in components MonoBehaviour Thin wrapper (a pointer) C GameObject GameObject Transform, Light, Rigidbody Transform, Light, Rigidbody… MonoBehaviour The real object and its memory

57.

Unite/Tokyo ECS for All What a GameObject really is C# GameObject Built-in components MonoBehaviour Thin wrapper (a pointer) C GameObject GameObject Transform, Light, Rigidbody Transform, Light, Rigidbody… MonoBehaviour The real object and its memory MonoBehaviour

58.

Unite/Tokyo ECS for All Where a GameObjectʼs components live C# managed objects C native memory MonoBehaviour Transform Transform wrapper Light Light wrapper Rigidbody Garbage-collected heap Native allocations

59.

Unite/Tokyo ECS for All Where a GameObjectʼs components live What if we added ECS components to that? C# managed objects C native memory MonoBehaviour Transform Transform wrapper Light Light wrapper Rigidbody ECS chunks struct Movement : IComponentData { float3 Direction; float Speed; } Chunk 0 Movement Garbage-collected heap Native allocations Chunk-based IComponentData

60.

What is a Unity Object?

61.

Unite/Tokyo ECS for All What is a Unity Object? UnityObject UnityObject GameObject Component Light Arrows point to the base class. Inheritance simplified.

62.

Unite/Tokyo ECS for All What is a Unity Object? UnityObject class Object { int32 InstanceID; } InstanceID maps to an Object*, so any Object can be found from its InstanceID. Used all over the engine.

63.

Unite/Tokyo ECS for All What is a Unity Object? UnityObject class Object { int32 InstanceID; } InstanceID maps to an Object*, so any Object can be found from its InstanceID. Used all over the engine. A unique 32-bit integer for every Object.

64.

Unite/Tokyo ECS for All What is a Unity Object? UnityObject class Object { int32 InstanceID; } InstanceID maps to an Object*, so any Object can be found from its InstanceID. Used all over the engine. A unique 32-bit integer for every Object. Allocated with a simple bump allocator. next 1 2 3 4 5

65.

Unite/Tokyo ECS for All What is a Unity Object? UnityObject class Object { int32 InstanceID; } InstanceID maps to an Object*, so any Object can be found from its InstanceID. Used all over the engine. A unique 32-bit integer for every Object. Allocated with a simple bump allocator. Only valid at runtime. next 1 2 3 4 5

66.

What is an Entity?

67.

Unite/Tokyo ECS for All What is an Entity? Entity struct Entity { int Index; int Version; } Entity maps to a Chunk* and IndexInChunk, so any entityʼs components can be found from its Entity.

68.

Unite/Tokyo ECS for All What is an Entity? Entity struct Entity { int Index; int Version; } Entity maps to a Chunk* and IndexInChunk, so any entityʼs components can be found from its Entity. A unique versioned handle.

69.

Unite/Tokyo ECS for All What is an Entity? Entity struct Entity { int Index; int Version; } Entity maps to a Chunk* and IndexInChunk, so any entityʼs components can be found from its Entity. A unique versioned handle. Allocated with a thread-safe block store.

70.

Unite/Tokyo ECS for All What is an Entity? Entity struct Entity { int Index; int Version; } Entity maps to a Chunk* and IndexInChunk, so any entityʼs components can be found from its Entity. A unique versioned handle. Allocated with a thread-safe block store. Only valid at runtime.

71.

Unite/Tokyo ECS for All What is an Entity? Entity struct Entity { int Index; int Version; } A unique versioned handle. Allocated with a thread-safe block store. Only valid at runtime. Entity maps to a Chunk* and IndexInChunk, so any entityʼs components can be found from its Entity. Curious how Index and Version work? The source is public in the Entities package. Weʼll skip explaining it today.

72.

Unite/Tokyo ECS for All InstanceID vs Entity InstanceID Entity int32 InstanceID; struct Entity { int Index; int Version; }

73.

Unite/Tokyo ECS for All InstanceID vs Entity InstanceID Entity int32 InstanceID; struct Entity { int Index; int Version; } ● Bump allocator

74.

Unite/Tokyo ECS for All InstanceID vs Entity InstanceID Entity int32 InstanceID; struct Entity { int Index; int Version; } ● Bump allocator ● Index / Version handle

75.

Unite/Tokyo ECS for All InstanceID vs Entity InstanceID Entity int32 InstanceID; struct Entity { int Index; int Version; } ● Bump allocator ● 4 bytes ● Index / Version handle

76.

Unite/Tokyo ECS for All InstanceID vs Entity InstanceID Entity int32 InstanceID; struct Entity { int Index; int Version; } ● Bump allocator ● Index / Version handle ● 4 bytes ● 8 bytes

77.

Unite/Tokyo ECS for All InstanceID vs Entity InstanceID Entity int32 InstanceID; struct Entity { int Index; int Version; } ● Bump allocator ● Index / Version handle ● 4 bytes ● 8 bytes ● Only valid at runtime

78.

Unite/Tokyo ECS for All InstanceID vs Entity InstanceID Entity int32 InstanceID; struct Entity { int Index; int Version; } ● Bump allocator ● Index / Version handle ● 4 bytes ● 8 bytes ● Only valid at runtime ● Only valid at runtime

79.

Unite/Tokyo ECS for All InstanceID vs Entity InstanceID Entity int32 InstanceID; struct Entity { int Index; int Version; } ● Bump allocator ● Index / Version handle ● 4 bytes ● 8 bytes ● Only valid at runtime ● Only valid at runtime ● Uniquely identifies an Object

80.

Unite/Tokyo ECS for All InstanceID vs Entity InstanceID Entity int32 InstanceID; struct Entity { int Index; int Version; } ● Bump allocator ● Index / Version handle ● 4 bytes ● 8 bytes ● Only valid at runtime ● Only valid at runtime ● Uniquely identifies an Object ● Uniquely identifies an Entity

81.

What if we replaced InstanceID with Entity?

82.

Unite/Tokyo ECS for All What if we replaced InstanceID with Entity? UnityObject class Object { int32 InstanceID; }

83.

Unite/Tokyo ECS for All What if we replaced InstanceID with Entity? UnityObject class Object { int32 InstanceID; Entity EntityId; }

84.

Unite/Tokyo ECS for All What if we replaced InstanceID with Entity? UnityObject class Object { int32 InstanceID; Entity EntityId; } Entities and GameObjects share the same Id system.

85.

Unite/Tokyo ECS for All What if we replaced InstanceID with Entity? UnityObject class Object { int32 InstanceID; Entity EntityId; } Entity and GameObject share the same Id system. A GameObject is implicitly an Entity. Yes, that also includes all UnityObjects, so only GameObjects will get valid ECS Chunks.

86.

Unite/Tokyo ECS for All What if we replaced InstanceID with Entity? UnityObject class Object { int32 InstanceID; Entity EntityId; } Entity and GameObject share the same Id system. A GameObject is implicitly an Entity. Yes, that also includes all UnityObjects, so only GameObjects will get valid ECS Chunks. An Entity maps to both an Object* and Chunk* + IndexInChunk.

87.

What if we replaced InstanceIDs with Entity? Do we get ECS on GameObjects for free?

88.

What if we replaced InstanceIDs with Entity? Do we get ECS on GameObjects for free? Ye…

89.

What if we replaced InstanceIDs with Entity? Do we get ECS on GameObjects for free? Ye…no.

90.

What if we replaced InstanceIDs with Entity? Do we get ECS on GameObjects for free? Ye…no. But it becomes possible! → So letʼs do it.

91.

Unite/Tokyo ECS for All If youʼve followed 6.0  6.7, youʼve felt some of this 1 2 3 4 The type became opaque InstanceID became 8 bytes Renamed to EntityId The bump allocator replaced by the ECS Entity allocator int32 ↓ struct InstanceID { int32 value; } struct InstanceID { int32 value; } ↓ struct InstanceID { int64 value; } So nobody could treat the value as a number and do… weird stuff struct InstanceID { int64 value; } ↓ struct EntityId { int64 value; } struct EntityId { int64 value; } ↓ struct EntityId { int Index; int Version; }

92.

So now, in 6.7, a GameObject and an Entity are addressed with the same EntityId. A GameObject is, at its core, an Entity! GameObject EntityId Entity

93.

ECS For All 7.X

94.

Unite/Tokyo Whatʼs in 7.0 ECS For All

95.

Unite/Tokyo ECS For All Whatʼs in 7.0 ● Entities core functionality is built in The package still exists for optional features: Baking, SubScenes and source-gen APIs IJobEntity, idiomatic foreach, …)

96.

Unite/Tokyo ECS For All Whatʼs in 7.0 ● Entities core functionality is built in The package still exists for optional features: Baking, SubScenes and source-gen APIs IJobEntity, idiomatic foreach, …) ● IComponentData and DynamicBuffer on GameObjects

97.

Unite/Tokyo ECS For All Whatʼs in 7.0 ● Entities core functionality is built in The package still exists for optional features: Baking, SubScenes and source-gen APIs IJobEntity, idiomatic foreach, …) ● IComponentData and DynamicBuffer on GameObjects ● Editor support: Inspector, Prefabs, Copy + Paste, Undo

98.

Unite/Tokyo ECS For All Whatʼs in 7.0 ● Entities core functionality is built in The package still exists for optional features: Baking, SubScenes and source-gen APIs IJobEntity, idiomatic foreach, …) ● IComponentData and DynamicBuffer on GameObjects ● Editor support: Inspector, Prefabs, Copy + Paste, Undo ● Scene serialisation and build support

99.

Unite/Tokyo ECS For All Whatʼs in 7.0 ● Entities core functionality is built in The package still exists for optional features: Baking, SubScenes and source-gen APIs IJobEntity, idiomatic foreach, …) ● IComponentData and DynamicBuffer on GameObjects ● Editor support: Inspector, Prefabs, Copy + Paste, Undo ● Scene serialisation and build support ● Unified lifecycles Destroying the GameObject destroys its ECS data; destroying the Entity destroys the GameObject

100.
[beta]
Unite/Tokyo

ECS For All

Example: moving ships
public class Ship : MonoBehaviour
{
public float3 Velocity;
[SerializeField] ShipLoadout loadout;
[SerializeField] Faction faction;
HealthPool hull;
WeaponBank weapons;
void Start() => (hull, weapons) = loadout.Build(this);
void Update()
{
UpdateTargeting(faction.Enemies);
weapons.Tick(Time.deltaTime);
if (hull.IsCritical) EmitDistressSignal();
RegenerateShields();
transform.position +=
(Vector3)(Velocity * Time.deltaTime);
}
void OnCollisionEnter(Collision c) => hull.ApplyImpact(c);
void OnDockRequested(Station s) { /* ... */ }
// ... 400 more lines of gameplay
}

One ship: fine.

101.
[beta]
Unite/Tokyo

ECS For All

Example: moving ships
public class Ship : MonoBehaviour
{
public float3 Velocity;
[SerializeField] ShipLoadout loadout;
[SerializeField] Faction faction;
HealthPool hull;
WeaponBank weapons;
void Start() => (hull, weapons) = loadout.Build(this);
void Update()
{
UpdateTargeting(faction.Enemies);
weapons.Tick(Time.deltaTime);
if (hull.IsCritical) EmitDistressSignal();
RegenerateShields();
transform.position +=
(Vector3)(Velocity * Time.deltaTime);
}
void OnCollisionEnter(Collision c) => hull.ApplyImpact(c);
void OnDockRequested(Station s) { /* ... */ }
// ... 400 more lines of gameplay
}

One ship: fine.
10,000 ships:
• 10,000 Update() calls every frame
• Each one a call from C into C#
• Each Transform moved one at a time

Can be too slow. But donʼt rewrite the Ship:
move just the hot part to ECS.

102.
[beta]
Unite/Tokyo

ECS For All

Migrate just the velocity
The MonoBehaviour hands velocity to ECS
public struct Velocity : IComponentData { public float3 Value; }
public class Ship : MonoBehaviour
{
public float3 StartVelocity;
[SerializeField] ShipLoadout loadout;
[SerializeField] Faction faction;
HealthPool hull;
WeaponBank weapons;
void Start()
{
(hull, weapons) = loadout.Build(this);
AddComponentData(this.gameObject,
new Velocity { Value = StartVelocity });
}
void Update()
{
UpdateTargeting(faction.Enemies);
weapons.Tick(Time.deltaTime);
if (hull.IsCritical) EmitDistressSignal();
RegenerateShields();

transform.position +=
(Vector3)(Velocity * Time.deltaTime);
}
void OnCollisionEnter(Collision c) => hull.ApplyImpact(c);
// ... 400 more lines of gameplay
}

The gameplay code stays in the MonoBehaviour. Only
the movement leaves Update(): the Ship hands its
velocity to ECS once.

103.
[beta]
Unite/Tokyo

ECS For All

Migrate just the velocity
The MonoBehaviour hands velocity to ECS
public struct Velocity : IComponentData { public float3 Value; }
public class Ship : MonoBehaviour
{
public float3 StartVelocity;
[SerializeField] ShipLoadout loadout;
[SerializeField] Faction faction;
HealthPool hull;
WeaponBank weapons;
void Start()
{
(hull, weapons) = loadout.Build(this);
AddComponentData(this.gameObject,
new Velocity { Value = StartVelocity });
}
void Update()
{
UpdateTargeting(faction.Enemies);
weapons.Tick(Time.deltaTime);
if (hull.IsCritical) EmitDistressSignal();
RegenerateShields();
}
void OnCollisionEnter(Collision c) => hull.ApplyImpact(c);
// ... 400 more lines of gameplay
}

A system moves every ship
partial struct MoveSystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
}
}

104.
[beta]
Unite/Tokyo

ECS For All

Migrate just the velocity
The MonoBehaviour hands velocity to ECS
public struct Velocity : IComponentData { public float3 Value; }
public class Ship : MonoBehaviour
{
public float3 StartVelocity;
[SerializeField] ShipLoadout loadout;
[SerializeField] Faction faction;
HealthPool hull;
WeaponBank weapons;
void Start()
{
(hull, weapons) = loadout.Build(this);
AddComponentData(this.gameObject,
new Velocity { Value = StartVelocity });
}
void Update()
{
UpdateTargeting(faction.Enemies);
weapons.Tick(Time.deltaTime);
if (hull.IsCritical) EmitDistressSignal();
RegenerateShields();
}
void OnCollisionEnter(Collision c) => hull.ApplyImpact(c);
// ... 400 more lines of gameplay
}

A system moves every ship
partial struct MoveSystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
foreach (var (transform, velocity) in
SystemAPI.Query<TransformRef, RefRO<Velocity>>())
{
}
}
}

105.
[beta]
Unite/Tokyo

ECS For All

Migrate just the velocity
The MonoBehaviour hands velocity to ECS
public struct Velocity : IComponentData { public float3 Value; }
public class Ship : MonoBehaviour
{
public float3 StartVelocity;
[SerializeField] ShipLoadout loadout;
[SerializeField] Faction faction;
HealthPool hull;
WeaponBank weapons;
void Start()
{
(hull, weapons) = loadout.Build(this);
AddComponentData(this.gameObject,
new Velocity { Value = StartVelocity });
}
void Update()
{
UpdateTargeting(faction.Enemies);
weapons.Tick(Time.deltaTime);
if (hull.IsCritical) EmitDistressSignal();
RegenerateShields();
}
void OnCollisionEnter(Collision c) => hull.ApplyImpact(c);
// ... 400 more lines of gameplay
}

A system moves every ship
partial struct MoveSystem : ISystem
{
public void OnUpdate(ref SystemState state)
{
foreach (var (transform, velocity) in
SystemAPI.Query<TransformRef, RefRO<Velocity>>())
{
transform.Position +=
velocity.ValueRO.Value *
SystemAPI.Time.DeltaTime;
}
}
}

106.
[beta]
Unite/Tokyo

ECS For All

Migrate just the velocity
The MonoBehaviour hands velocity to ECS
public struct Velocity : IComponentData { public float3 Value; }
public class Ship : MonoBehaviour
{
public float3 StartVelocity;
[SerializeField] ShipLoadout loadout;
[SerializeField] Faction faction;
HealthPool hull;
WeaponBank weapons;
void Start()
{
(hull, weapons) = loadout.Build(this);
AddComponentData(this.gameObject,
new Velocity { Value = StartVelocity });
}
void Update()
{
UpdateTargeting(faction.Enemies);
weapons.Tick(Time.deltaTime);
if (hull.IsCritical) EmitDistressSignal();
RegenerateShields();
}
void OnCollisionEnter(Collision c) => hull.ApplyImpact(c);
// ... 400 more lines of gameplay
}

A Burst system moves every ship
[BurstCompile]
partial struct MoveSystem : ISystem
{
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
foreach (var (transform, velocity) in
SystemAPI.Query<TransformRef, RefRO<Velocity>>())
{
transform.Position +=
velocity.ValueRO.Value *
SystemAPI.Time.DeltaTime;
}
}
}

Main thread, Burst-compiled, one system for every ship.
Gameplay stays in the MonoBehaviour.

107.
[beta]
Unite/Tokyo

ECS For All

Go wide with IJobEntity
The system from before
[BurstCompile]
partial struct MoveSystem : ISystem
{
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
foreach (var (transform, velocity) in
SystemAPI.Query<TransformRef, RefRO<Velocity>>())
{
transform.Position +=
velocity.ValueRO.Value *
SystemAPI.Time.DeltaTime;
}
}
}

The same loop as an IJobEntity
[BurstCompile]
partial struct MoveJob : IJobEntity
{
public float DeltaTime;
void Execute(TransformRef transform, in Velocity velocity)
{
transform.Position += velocity.Value * DeltaTime;
}
}

108.
[beta]
Unite/Tokyo

ECS For All

Go wide with IJobEntity
The system schedules the job
[BurstCompile]
partial struct MoveSystem : ISystem
{
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
foreach (var (transform, velocity) in
SystemAPI.Query<TransformRef, RefRO<Velocity>>())
{
transform.Position +=
velocity.ValueRO.Value *
SystemAPI.Time.DeltaTime;
new MoveJob { DeltaTime = SystemAPI.Time.DeltaTime }
}
.ScheduleParallel();
}
}

The same loop as an IJobEntity
[BurstCompile]
partial struct MoveJob : IJobEntity
{
public float DeltaTime;
void Execute(TransformRef transform, in Velocity velocity)
{
transform.Position += velocity.Value * DeltaTime;
}
}

Burst-compiled and spread across worker threads.
Gameplay still stays in the MonoBehaviour.

109.

Unite/Tokyo ECS For All IComponentData in the Inspector ● Add it with Add Component ● Edit its fields like any other component ● Works with Prefabs, Copy + Paste and Undo ● Saved in Scenes In development. Subject to change.

110.

Unite/Tokyo ECS For All Whatʼs next 7.X

111.

Unite/Tokyo ECS For All Whatʼs next 7.X ● Systems for MonoBehaviours Control when MonoBehaviours update relative to Systems

112.

Unite/Tokyo ECS For All Whatʼs next 7.X ● Systems for MonoBehaviours Control when MonoBehaviours update relative to Systems ● Chunk serialisation in Scenes Optimised Entity loading when you load your Scene

113.

Unite/Tokyo ECS For All Whatʼs next 7.X ● Systems for MonoBehaviours Control when MonoBehaviours update relative to Systems ● Chunk serialisation in Scenes Optimised Entity loading when you load your Scene ● GameObject Components in Entity Archetypes

114.

Unite/Tokyo ECS For All Whatʼs next 7.X ● Systems for MonoBehaviours Control when MonoBehaviours update relative to Systems ● Chunk serialisation in Scenes Optimised Entity loading when you load your Scene ● GameObject Components in Entity Archetypes ● Pure Entities authoring directly in the Scene

115.

Unite/Tokyo ECS For All Whatʼs next 7.X ● Systems for MonoBehaviours Control when MonoBehaviours update relative to Systems ● Chunk serialisation in Scenes Optimised Entity loading when you load your Scene ● GameObject Components in Entity Archetypes ● Pure Entities authoring directly in the Scene ● Mixed hierarchies of Entities and GameObjects

116.

Unite/Tokyo ECS For All Whatʼs next 7.X ● Systems for MonoBehaviours Control when MonoBehaviours update relative to Systems ● Chunk serialisation in Scenes Optimised Entity loading when you load your Scene ● GameObject Components in Entity Archetypes ● Pure Entities authoring directly in the Scene ● Mixed hierarchies of Entities and GameObjects ● Optional Transform on GameObjects

117.

The future vision

118.

Unite/Tokyo ECS For All The vision: a scene that is 90% Entities The scene today In a complex scene, most things are made entirely of Unity types: Transform, Renderer, MeshFilter, Rigidbody. No MonoBehaviours. (conceptual)

119.

Unite/Tokyo ECS For All The vision: a scene that is 90% Entities The scene today In a complex scene, most things are made entirely of Unity types: Transform, Renderer, MeshFilter, Rigidbody. No MonoBehaviours. So what if all of those were backed entirely by pure Entity data? (conceptual)

120.

Unite/Tokyo ECS For All The vision: a scene that is 90% Entities Entities in grey (conceptual)

121.

Unite/Tokyo ECS For All The vision: a scene that is 90% Entities Entities in grey Shipping Unity features powered by ECS Rendering, Lighting, Physics, … (conceptual)

122.

Unite/Tokyo ECS For All The vision: a scene that is 90% Entities Entities in grey Shipping Unity features powered by ECS Rendering, Lighting, Physics, … All Entity data can load faster via chunks (conceptual)

123.

Unite/Tokyo ECS For All The vision: a scene that is 90% Entities Entities in grey Shipping Unity features powered by ECS Rendering, Lighting, Physics, … All Entity data loads faster via chunks Same types, all usable from normal MonoBehaviours Remember: in C# theyʼre wrappers, so they keep working (conceptual)

124.

Unite/Tokyo ECS For All The vision: a scene that is 90% Entities Entities in grey Shipping Unity features powered by ECS Rendering, Lighting, Physics, … All Entity data loads faster via chunks Same types, all usable from normal MonoBehaviours Remember: in C# theyʼre wrappers, so they keep working Keep using MonoBehaviours as you like …while passively benefiting from ECS internally (conceptual)

125.

Thank you!