Unity's latest DOTS series "Detailed Explanation of Baking and Baker"

Detailed explanation of Unity DOTS Baking and Baker

Recently, DOTS has finally released its official version. Let’s share the key concepts of Baking and Baker in DOTS so that everyone can learn and master Unity DOTS development.

Yes, there is a . I hope everyone can click in to exchange development experiences together!

Unity DOTS development model allows everyone to use the original component method to edit game scenes and resources when "creating" games. At the same time, Unity provides a Baking mechanism to convert ordinary GameObject data into entity data in ecs mode. The program When running, the converted ECS mode data is directly loaded. Before we understand the Baking and Baker mechanisms in depth, we first get familiar with a few concepts:

Authoring:  The English word means "creation". As mentioned above, when developing in ECS mode, you can use the original method to create game nodes and then convert them through baking. So those GameObjects and Components to be converted to ecs mode are called Authoring.

Baking:  refers to the process of converting traditional GameObject+ component data into data required in ecs mode;

Baker:  refers to the specific executor and execution logic of a certain Authoring to ECS mode;

With these concepts in mind, let's analyze Baking and Baker of Unity DOTS in detail.

Core analysis of Unity DOTS Baking mechanism

Authoring Data:  When you edit your game project application, such as traditional scripts, resources, and other game data dependencies, this editing method is very flexible and convenient for users to view and use. These edited data are called Authoring data.

Runtime Data  is the data required when running in ECS mode. When you run the program, it is processed as game data. Data in this mode has efficient performance and efficient storage. It is designed for computer operation and processing. In other words, it is not as intuitive to developers as the traditional GameObject mode.

Developers need to load the Authoring Scene as a SubScene in Unity. When editing the Authoring object in the Authoring scene, the Baking process will be triggered and completed in the background.

Baking has two modes:

Full Baking (global Baking): Perform Baking operations on all objects in the Authoring Scene;

Incremental Baking: Only perform Baking operations on changed objects;

1: If the entity scene data under the baked ECS model is not on the disk, Full Baking will occur;

2: The content of the Authoring Scene is modified, and the baked entity scene data (Runtime Data) expires;

3: If the baking code is found to contain a [BakingVersion] attribute after compiling the Baker code, this means that the baking code has been changed and the entity scene has expired, which will trigger full baking;

7: If you clear the baking cache in the editor's Preferences, it will cause full baking;

Full Baking will output some files to be stored on the hard disk, and these files will be loaded when the editor runs the program.

When using the subscene mechanism to load a "creative scene", incremental baking is also initialized. When performing an incremental baking process in a scene, it means that when you edit a "creative scene", you can directly access and use the results of your baking. It only Bake for changed data and components.

Core analysis of Unity DOTS Baker mechanism

The Baker of each component data also needs to register its execution dependencies with the system, so that when the data it depends on changes, the Baker call of this component will be triggered. By default, a Baker can only access one authoring component, but sometimes it still needs to access from other components, GameObjects or various resources, so the entire baking process needs to know these dependencies. If these other objects change, then This baker also needs to be re-executed. For example, if an authoring GameObject is a cube, Unity will render the entity into a cube when baking. If the authoring GameObject is subsequently modified into a sphere, then ECS must also make changes accordingly. To render this entity into a sphere, this means that Unity must delete the previous Cube Entity and create a new sphere entity, or change the entity to display a sphere. Another example is that a GameObject has a material that depends on a scriptable object. Baker needs to define a resource dependency to ensure that when the resource scriptable object is changed, the object will be re-Baker. The entire process happens automatically, and we ordinary developers don’t have to worry about it.

Finally, I will show you a Baker with a common MonoBehaviour Component. The code is as follows:

ECS Component:

publicstructDependentData:IComponentData

{

publicfloatDistance;

publicintVertexCount;

}

Authoring Component:

publicclassDependentDataAuthoring:MonoBehaviour

{

publicGameObjectOther;

publicMeshMesh;

}

Define Baker from Authoring Compongt to ECS Component:

publicclassGetComponentBaker:Baker<DependentDataAuthoring>

{

// Call the Bake function when baking

publicoverridevoidBake(DependentDataAuthoringauthoring)

{

// Declare the required Baker dependencies

DependsOn(authoring.Other);

DependsOn(authoring.Mesh);

if(authoring.Other == null)return;

if(authoring.Mesh == null)return;

vartransform = GetComponent<Transform>();

vartransformOther = GetComponent<Transform>(authoring.Other);

if(transform == null)return;

if(transformOther == null)return;

varentity = GetEntity(TransformUsageFlags.Dynamic);

AddComponent(entity,newDependentData

{

Distance = Vector3.Distance(transform.position,transformOther.position),

VertexCount = authoring.Mesh.vertexCount

});

}

}

Share it here today, follow us, and learn more about the latest Unity DOTS advanced and practical knowledge.

Dear prospective VIP customers:

Our Unity DOTS course has also been officially released. After more than 9 years of updates and iterations, our course has covered most of the problems encountered in Unity development, including the systems needed to advance the Unity main program and get promoted and raised salary. Knowledge system, technical solutions to key and difficult problems in mainstream game types. Our teachers provide real-time answers and replies from 10:00~23:00, including but not limited to client + server. We believe that the game development technical services we provide can help you well. Choose our VIP course, you won’t regret it!

follow me!

Boyi Chuang has advanced technology in the gaming industry and received promotions and salary increases

Guess you like

Origin blog.csdn.net/voidinit/article/details/133982245