Unity Dots network data docking

Date: 2023-8-7

A while ago, the sub-project was being tested, and the iteration was relatively fast, so there was no time to update it.

Let me mention a common topic, data configuration.

Generally speaking, it is done through planning, or Excel, or the server throws it over.

Here we mainly discuss the connection of network data, and the same applies to local Excel.

So, how does Dots do it?

The example of the official website is Bake.

Unity divides Bake into two processes, creation mode and run mode.

Creative mode is easy for humans to read and modify.

Running mode is better for performance.

The particularity of the Bake mode is where the difficulty lies.

In creative mode, external static Static cannot be used, only constant Const can be used.

At the same time, Bake mode cannot be modified after packaging. It only takes effect in the editor and is processed during the EditorBuild process.

So how to apply the data planned and edited?

Break the problem into 2 steps. 1. How to access and read data. 2. Application stage.

1. First, think about what kind of external data Dots can read.

Constants, static data.

It also includes the data in PlayerPrefs. Yes, save the data locally using Json and then read it.

2. So what stage is applied.

In Bake, it is impossible. You can only choose after Bake

Think about a question, what do we do when we instantiate an object.

EntityManager.Instance (Entity), right.

We copied the Entity, but the Entity does not contain data. It only contains the index. Then, what we actually want to modify is the data associated with the Entity, which is CompoentData.

The next idea is how to get the ComponentData corresponding to the Entity and modify its associated data.

1. When Bake, we convert Prefab into Entity and concentrate it in a singleton component, for example: PrefabComponentData.

2. After the network data is loaded, it is first converted to Json and stored in PlayPrefs.

The key of PlayPrefs can be int and is associated with the enumeration corresponding to Prefab.

3. Obtain Entity through singleton component 

(SystemAPI. GetSingle<T>().GetEntity())

4. Get the corresponding Component . 

( Component DataT = EntityManager.GetCompoentData())

5. Correspond to the data in PlayerPrefs, modify the copied Component

6. Re-associate the modified ComponentData to the Entity in the singleton component.

 ( EntityManager.SetCompoentData( DataT,Entity))

Let’s talk about why we need to re-associate:

Because ComponentData is value type data, not a reference type.

Due to the efficient memory management of ECS, the acquisition process of EntityManager.GetCompoentData() is a copy, not a reference, and will not affect the original data.

Therefore, we also need to re-associate the modified Component to the Entity.

Guess you like

Origin blog.csdn.net/qq_35623058/article/details/132138417