目录
Baking and entity scenes
Creating and editing sub scenes
Accessing data in a baker
Loading and unloading entity scenes
Baking and entity scenes
Baking 是一个把 sub scenes 转变成 entity scenes 的过程,使用 bakers和 baking systems:
- A sub scene is a Unity scene asset that's embedded in another scene by the SubScene MonoBehaviour.
- An entity scene is a serialized set of entities and components that can be loaded at runtime.
- A baker is a class extending Baker<T>, where T is a MonoBehaviour. A MonoBehaviour with a Baker is called an authoring component.
- 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.)
Baking a sub scene is done in a few main steps:
- Sub scene的每一个GameObject,创建对应的实体
- 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.
- 执行baking systems. Each system can read and modify the baked entities in any way: set components, add components, remove components, create additional entities, or destroy entities.
当sub-scene更改时,就会重新baked:
- 只有 读取更改的 authoring components 的bakers,才会re-executed.
- baking systems 会 re-executed
- 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.)
Creating and editing sub scenes
勾选框表示是否可以编辑,勾选上打开该场景时,就会加载对应的资源,如果不在该场景后,可以取消勾选,降低消耗
Accessing data in a baker
baker的 authoring component可以自动获取,但是其它的数据,需要添加到dependencies 中才可以访问:
Baker method | Description |
---|---|
GetComponent<T>() | Accesses any component of any GameObject in the Sub Scene. |
DependsOn() | Tracks an asset for this Baker . |
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.) |
Loading and unloading entity scenes
为了流加载,场景被分为多个sections ,通过index 识别. 每一个section属于哪一个 entity 根据 SceneSection shared component 组件决定, By default, an entity belongs to section 0, but this can be changed by setting SceneSection
during baking.
⚠ IMPORTANT |
---|
在bake过程中,子场景中的实体只能引用同一section或section 0的其他实体( section 0 总是在其他section之前加载 and 只有当场景本身被卸载时才会被卸载). |
当一个场景被加载时,它由一个metadata的实体来表示,它的每个部分也都由一个实体来表示. 单个section通过操作 entity's RequestSceneLoaded component 来实现load、unload, 当该组件发生变化时,SceneSectionStreamingSystem
in the SceneSystemGroup
会收到回调。
The SceneSystem 提供了 loading and unloading entity scenes 的静态方法,不是section scene哦:
SceneSystem method | Description |
---|---|
LoadSceneAsync() | Initiates loading of a scene. Returns an entity that represents the loaded scene. |
LoadPrefabAsync() | Initiates loading of a prefab. Returns an entity that references the loaded prefab. |
UnloadScene() | Destroys all entities of a loaded scene. |
IsSceneLoaded() | Returns true if a scene is loaded. |
IsSectionLoaded() | Returns true if a section is loaded. |
GetSceneGUID() | Returns the GUID representing a scene asset (specified by its file path). |
GetScenePath() | Returns the path of a scene asset (specified by its GUID). |
GetSceneEntity() | Returns the entity representing a scene (specified by its GUID). |
⚠ IMPORTANT |
---|
Entity scene and section加载总是 asynchronous,并且不能保证在请求之后需要多长时间才能加载数据.在大多数情况下,代码应该检查从场景加载的特定数据是否存在,而不是检查场景本身的加载状态 这种方法避免了将代码绑定到特定场景。 |