You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Docs/baking.md
+30-18
Original file line number
Diff line number
Diff line change
@@ -1,47 +1,59 @@
1
1
# Baking and entity scenes
2
2
3
-
**Baking** is a build-time process that transforms **Sub Scenes** into **Entity Scenes** using **Bakers** and **baking systems**:
3
+
**Baking** is a build-time process that transforms **sub scenes** into **entity scenes** using **bakers** and **baking systems**:
4
4
5
-
- A **Sub Scene** is a Unity scene asset that's embedded in another scene by the [SubScene](https://docs.unity3d.com/Packages/com.unity.entities@latest?subfolder=/api/Unity.Entities.SubScene.html) MonoBehaviour.
6
-
- An **Entity Scene** is a serialized set of entities and components that can be loaded at runtime.
7
-
- A **Baker** is a class extending [`Baker<T>`](https://docs.unity3d.com/Packages/com.unity.entities@latest?subfolder=/api/Unity.Entities.Baker-1.html), where T is a MonoBehaviour. A MonoBehaviour with a Baker is called an **authoring component**.
5
+
- A **sub scene** is a Unity scene asset that's embedded in another scene by the [SubScene](https://docs.unity3d.com/Packages/com.unity.entities@latest?subfolder=/api/Unity.Entities.SubScene.html) MonoBehaviour.
6
+
- An **entity scene** is a serialized set of entities and components that can be loaded at runtime.
7
+
- A **baker** is a class extending [`Baker<T>`](https://docs.unity3d.com/Packages/com.unity.entities@latest?subfolder=/api/Unity.Entities.Baker-1.html), where T is a MonoBehaviour. A MonoBehaviour with a Baker is called an **authoring component**.
8
8
- A **baking system** is a normal system marked with the `[WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]` attribute. (Baking systems are fully optional and generally only required for advanced use cases.)
9
9
10
-
Baking a Sub Scene is done in a few main steps:
10
+
Baking a sub scene is done in a few main steps:
11
11
12
-
1. For each GameObject of the Sub Scene, a corresponding entity is created.
13
-
2. The Baker of each authoring component in the Sub Scene is executed. Each Baker can read the authoring component and add components to the corresponding entity.
14
-
2. The baking systems execute. Each system can read and modify the baked entities in any way: set components, add components, remove components, create additional entities, or destroy entities. Unlike Bakers, baking systems should not access the original GameObjects of the Sub Scene.
12
+
1. For each GameObject of the sub scene, a corresponding entity is created.
13
+
2. The baker of each authoring component in the sub scene is executed. Each baker can read the authoring component and add components to the corresponding entity.
14
+
2. The baking systems execute. Each system can read and modify the baked entities in any way: set components, add components, remove components, create additional entities, or destroy entities. Unlike bakers, baking systems should not access the original GameObjects of the sub scene.
15
15
16
-
🕹*[See examples of authoring components, Bakers, and baking systems.](./examples/baking.md)*
16
+
🕹*[See examples of authoring components, bakers, and baking systems.](./examples/baking.md)*
17
17
18
-
When modified, a Sub Scene is re-baked:
18
+
When modified, a sub scene is re-baked:
19
19
20
-
1. Only the Bakers that read the modified authoring components are re-executed.
20
+
1. Only the bakers that read the modified authoring components are re-executed.
21
21
1. The baking systems are always re-executed in full.
22
-
1. The live entities in Edit mode or Play mode are updated to match the results of Baking. (This is possible because Baking tracks the correspondence of baked entities to live entities.)
22
+
1. The live entities in Edit mode or Play mode are updated to match the results of baking. (This is possible because baking tracks the correspondence of baked entities to live entities.)
23
23
24
24
<br>
25
25
26
-
## Accessing data in a Baker
26
+
## Creating and editing sub scenes
27
27
28
-
Incremental baking requires Baker's to track all the data they read. The fields of a Baker's authoring component are automatically tracked, but other data read by a Baker must be added to its list of dependencies through the Baker methods:
28
+
A GameObject with the `SubScene` MonoBehaviour has a checkbox that opens and closes the sub scene for editing. While a sub scene is open, its GameObjects are loaded and take up resources in the Unity editor, so you may want to close sub scenes that you're not currently editing.
29
+
30
+

31
+
32
+
The convenient way to create a new sub scene is to right click within the Hierarchy window and select `New Subscene > Empty Scene...`. This creates both a new scene file and a GameObject with a `SubScene` component that references the new scene file:
33
+
34
+

35
+
36
+
<br>
37
+
38
+
## Accessing data in a baker
39
+
40
+
Incremental baking requires bakers to track all the data they read. The fields of a baker's authoring component are automatically tracked, but other data read by a baker must be added to its list of dependencies through the `Baker` methods:
29
41
30
42
|**Baker method**|**Description**|
31
43
|---|---|
32
44
|[`GetComponent<T>()`]()| Accesses any component of any GameObject in the Sub Scene. |
33
-
|[`DependsOn()`]()| Tracks an asset for this Baker. |
34
-
|[`GetEntity()`]()| Returns the id of an entity baked in the Sub Scene or baked from a prefab. (The entity will not yet have been fully baked, so you should not attempt to read or modify the components of the entity.) |
45
+
|[`DependsOn()`]()| Tracks an asset for this `Baker`. |
46
+
|[`GetEntity()`]()| Returns the id of an entity baked in the sub scene or baked from a prefab. (The entity will not yet have been fully baked, so you should not attempt to read or modify the components of the entity.) |
35
47
36
48
<br>
37
49
38
-
# Loading and unloading entity scenes
50
+
##Loading and unloading entity scenes
39
51
40
52
For streaming purposes, the entities of a scene are split into **sections** identified by index number. Which section an entity belongs to is designated by its [`SceneSection`]() shared component. By default, an entity belongs to section 0, but this can be changed by setting `SceneSection` during baking.
41
53
42
54
|⚠ IMPORTANT |
43
55
| :- |
44
-
| During baking, entities in a Sub Scene can only reference other entities of the same section or section 0 (which is a special case because section 0 is always loaded before the other sections and only *un*loaded when the scene itself is unloaded). |
56
+
| During baking, entities in a sub scene can only reference other entities of the same section or section 0 (which is a special case because section 0 is always loaded before the other sections and only *un*loaded when the scene itself is unloaded). |
45
57
46
58
When a scene is loaded, it is represented by an entity with metadata about the scene, and its sections are also each represented by an entity. An individual section is loaded and unloaded by manipulating its entity's [`RequestSceneLoaded`](https://docs.unity3d.com/Packages/com.unity.entities@latest?subfolder=/api/Unity.Entities.ICleanupComponent.html) component: the `SceneSectionStreamingSystem` in the `SceneSystemGroup` responds when this component changes.
Copy file name to clipboardExpand all lines: Docs/entities-components.md
+16-24
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,7 @@ Entity component types are defined by implementing these interfaces:
31
31
|[`ISharedComponent`](https://docs.unity3d.com/Packages/com.unity.entities@latest?subfolder=/api/Unity.Entities.ISharedComponent.html)| Defines a shared component type, whose values can be shared by multiple entities.|
32
32
|[`ICleanupComponent`](https://docs.unity3d.com/Packages/com.unity.entities@latest?subfolder=/api/Unity.Entities.ICleanupComponent.html)| Defines a cleanup component type, which facilitates proper setup and teardown of resources.|
33
33
34
-
There are also two additional interfaces ([`ICleanupSharedComponent`](https://docs.unity3d.com/Packages/com.unity.entities@latest?subfolder=/api/Unity.Entities.ICleanupSharedComponent.html) and [`ICleanupBufferElementData`](https://docs.unity3d.com/Packages/com.unity.entities@latest?subfolder=/api/Unity.Entities.ICleanupBufferElementData.html)) and [chunk components](additional-entities-features.md#chunk-components) (which are defined with `IComponentData` but added and removed from entities by a distinct set of methods).
34
+
There are also two additional interfaces ([`ICleanupSharedComponent`](https://docs.unity3d.com/Packages/com.unity.entities@latest?subfolder=/api/Unity.Entities.ICleanupSharedComponent.html) and [`ICleanupBufferElementData`](https://docs.unity3d.com/Packages/com.unity.entities@latest?subfolder=/api/Unity.Entities.ICleanupBufferElementData.html)) and [chunk components](additional-entities-features.md#chunk-components) (which are defined with `IComponentData` but added and removed from entities by a different set of methods).
35
35
36
36
A component type defined with `IComponentData` or `IBufferElementData` can be made **'enableable'** by also implementing [`IEnableableComponent`](additional-entities-features.md#enableable-components).
37
37
@@ -58,7 +58,7 @@ The entities in a world are created, destroyed, and modified through the world's
58
58
59
59
|📝 NOTE |
60
60
| :- |
61
-
|All of the above methods, except `GetComponent` and `SetComponent`, are [structural change](concepts-structural-changes.md) operations. |
61
+
|`CreateEntity`, `Instantiate`, `DestroyEntity`, `AddComponent`, and `RemoveComponent` are [structural change](https://docs.unity3d.com/Packages/[email protected]/manual/concepts-structural-changes.html) operations. |
62
62
63
63
<br>
64
64
@@ -84,7 +84,7 @@ Archetypes are created by the `EntityManager` as you create and modify entities,
84
84
85
85
# Chunks
86
86
87
-
The entities of an archetype are stored in 16KiB blocks of memory belonging to the archetype called *chunks*. Each chunk stores up to 128 entities (with the precise number depending upon the number and size of the archetype's component types).
87
+
The entities of an archetype are stored in 16KiB blocks of memory belonging to the archetype called *chunks*. Each chunk stores up to 128 entities (with the precise quantity depending upon the number and size of the component types in the archetype).
88
88
89
89
The entity ID's and components of each type are stored in their own separate array within the chunk. For example, in the archetype for entities which have component types *A* and *B*, each chunk will store three arrays:
90
90
@@ -104,17 +104,17 @@ The creation and destruction of chunks is handled by the `EntityManager`:
104
104
- The `EntityManager` creates a new chunk only when an entity is added to an archetype whose already existing chunks are all full.
105
105
- The `EntityManager` only destroys a chunk when the chunk's last entity is removed.
106
106
107
-
Any `EntityManager` operation that adds, removes, or moves entities within a chunk is called a *structural change*. Such changes should generally be made only on the main thread, not in jobs. (An [`EntityCommandBuffer`](https://docs.unity3d.com/Packages/com.unity.entities@latest?subfolder=/api/Unity.Entities.EntityCommandBuffer.html) can be used to work around this limitation.)
107
+
Any `EntityManager` operation that adds, removes, or moves entities within a chunk is called a *structural change*. Such changes should generally be made only on the main thread, not in jobs (though an [`EntityCommandBuffer`](https://docs.unity3d.com/Packages/com.unity.entities@latest?subfolder=/api/Unity.Entities.EntityCommandBuffer.html) can be used as a work around).
108
108
109
109
<br>
110
110
111
111
# Queries
112
112
113
-
An [`EntityQuery`](https://docs.unity3d.com/Packages/com.unity.entities@latest?subfolder=/api/Unity.Entities.EntityQuery.html) efficiently finds all entities having a specified set of component types. For example, if a query looks for all entities having component types *A* and *B*, then the query will gather the chunks of all archetypes having those two component types, regardless of whatever other component types those archetypes might have. So such a query would match the entities with component types *A* and *B*, but the query would also match the entities with component types *A*, *B*, and *C*.
113
+
An [`EntityQuery`](https://docs.unity3d.com/Packages/com.unity.entities@latest?subfolder=/api/Unity.Entities.EntityQuery.html) efficiently finds all entities having a specified set of component types. For example, if a query looks for all entities having component types *A* and *B*, then the query will gather the chunks of all archetypes which include *A* and *B*, regardless of whatever other component types those archetypes might have. Such a query would match the entities with component types *A* and *B*, but the query would also match, say, the entities with component types *A*, *B*, and *C*.
114
114
115
115
|📝 NOTE |
116
116
| :- |
117
-
| The archetypes matching a query will get cached until the next time a new archetype is added to the world. Because the set of existing archetypes in a world tends to stabilize early in the lifetime of a program, this caching tends to improve performance. |
117
+
| The archetypes matching a query will get cached until the next time a new archetype is added to the world. Because the set of existing archetypes in a world tends to stabilize early in the lifetime of a program, this caching usually helps make the queries much cheaper. |
118
118
119
119
A query can also specify component types to *exclude* from the matching archetypes. For example, if a query looks for all entities having component types *A* and *B* but *not* having component type *C*, the query would match entities with component types *A* and *B*, but the query would *not* match entities with component types *A*, *B*, and *C*.
120
120
@@ -127,12 +127,12 @@ A query can also specify component types to *exclude* from the matching archetyp
127
127
128
128
An entity ID is represented by the struct [`Entity`](https://docs.unity3d.com/Packages/com.unity.entities@latest?subfolder=/api/Unity.Entities.Entity.html).
129
129
130
-
In order to look up entities by ID, the world’s `EntityManager` maintains an array of entity metadata. Each entity ID has an index value denoting a slot in this metadata array, and the slot stores a pointer to the chunk where that entity is stored, as well as the index of the entity within the chunk. When no entity exists for a particular index, the chunk pointer at that index is null. Here, for example, no entities with indexes 1, 2, and 5 currently exist, so the chunk pointers in those slots are all null.
131
-
132
-
In order to allow entity indexes to be reused after an entity is destroyed, each entity ID also contains a *version number*. When an entity is destroyed, the version number stored at its index is incremented, and so if an ID’s version number doesn’t match the one currently stored, then the ID must refer to an entity that has already been destroyed or perhaps never existed.
130
+
In order to look up entities by ID, the world’s `EntityManager` maintains an array of entity metadata. Each entity ID has an index value denoting a slot in this metadata array, and the slot stores a pointer to the chunk where that entity is stored, as well as the index of the entity within the chunk. When no entity exists for a particular index, the chunk pointer at that index is null. Here, for example, no entities with indexes 1, 2, and 5 currently exist, so the chunk pointers in those slots are all null:
In order to allow entity indexes to be reused after an entity is destroyed, each entity ID also contains a *version number*. When an entity is destroyed, the version number stored at its index is incremented, and so if an ID’s version number doesn’t match the one currently stored, then the ID must refer to an entity that has already been destroyed or perhaps never existed.
135
+
136
136
<br>
137
137
138
138
# `IComponentData`
@@ -152,26 +152,22 @@ An `IComponentData` struct is expected to be unmanaged, so it cannot contain any
152
152
*[Fixed array](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/fixed-statement) (only allowed in an [unsafe](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/unsafe) context)
153
153
* Struct types that conform to these same restrictions.
154
154
155
-
<br>
156
-
157
-
## Tag components
158
-
159
-
An `IComponentData` struct with no fields is called a **tag component**. Although tag components store no data, they can still be added and removed from entities like any other component type, and they can be useful to mark entities for queries. For example, if all of our entities representing monsters have a *Monster* tag component, a query for the *Monster* component type will match all the monster entities.
155
+
An `IComponentData` struct with no fields is called a ***tag component***. Although tag components store no data, they can still be added and removed from entities like any other component type, which is useful for queries. For example, if all of our entities representing monsters have a *Monster* tag component, a query for the *Monster* component type will match all the monster entities.
160
156
161
157
<br>
162
158
163
159
## Managed `IComponentData` components
164
160
165
161
A class implementing `IComponentData` is a *managed* component type. Unlike the unmanaged `IComponentData` structs, these managed components can store any managed objects.
166
162
167
-
In general, managed component types should be used only when really needed because, compared to *unmanaged* components, managed components incur some heavy costs:
163
+
In general, managed component types should be used only when really needed because, compared to *unmanaged* components, they incur some heavy costs:
168
164
169
165
- Like all managed objects, managed components *cannot* be used in [Burst](https://docs.unity3d.com/Packages/com.unity.burst@latest)-compiled code.
170
-
- Managed objects can only be safely used in [jobs](https://docs.unity3d.com/Manual/JobSystem.html) with some special considerations.
171
-
- Managed components are not stored directly in the chunks: instead, all managed components of a world are all stored in one big array, and the chunks store just indexes of this array.
166
+
- Managed objects cannot normally be used safely in [jobs](https://docs.unity3d.com/Manual/JobSystem.html).
167
+
- Managed components are not stored directly in the chunks: instead, all managed components of a world are all stored in one big array while the chunks merely store indexes of this array.
172
168
- Like all managed objects, creating managed components incurs garbage collection overhead.
173
169
174
-
If a managed component type implements `ICloneable`, then any resources it contains can be properly copied when an instance is itself copied. Likewise, if a managed component type implements `IDisposable`, then it can properly dispose any resources it may contain when an instance is removed from an entity or the entity is destroyed. Example:
170
+
If a managed component type implements `ICloneable`, then any resources it contains can be properly copied when an instance is itself copied. Likewise, if a managed component type implements `IDisposable`, then it can properly dispose any resources it may contain when an instance is removed from an entity or the entity is destroyed.
175
171
176
172
<br>
177
173
@@ -242,11 +238,7 @@ An aspect is defined as a readonly partial struct implementing [`IAspect`](https
242
238
|`DynamicBuffer<T>`|The wrapped entity's dynamic buffer T component.|
243
239
|Another aspect type| The containing aspect will encompass all the fields of the 'embedded' aspect. |
244
240
245
-
<br>
246
-
247
-
## Creating instances of an aspect
248
-
249
-
These EntityManager methods create instances of an aspect:
241
+
These `EntityManager` methods create instances of an aspect:
250
242
251
243
|**Method**|**Description**|
252
244
|----|---|
@@ -257,7 +249,7 @@ Aspect instances can also be retrieved by [`SystemAPI.GetAspectRW<T>`](https://d
257
249
258
250
|⚠ IMPORTANT |
259
251
| :- |
260
-
|In a system, you should get aspect instances *via*`SystemAPI.GetAspectRW<T>` and `SystemAPI.GetAspectRO<T>`rather than *via*the `EntityManager` methods. Unlike the `EntityManager` methods, the `SystemAPI` methods register the underlying component types of the aspect with the system, which is [required for the systems to schedule jobs with every needed dependency](./entities-jobs.md#systemstatedependency). |
252
+
|You should generally get aspect instances *via*`SystemAPI`rather than the `EntityManager`: unlike the `EntityManager` methods, the `SystemAPI` methods register the underlying component types of the aspect with the system, which is [necessary for the systems to properly schedule jobs with every dependency they need](./entities-jobs.md#systemstatedependency). |
0 commit comments