Baking (Code Generation)
Out of the box, coherence can use C# reflection to sync data at runtime. This is a great way to get started but is very costly performance-wise and has a number of limitations on what features can be used through this system.
For optimal runtime performance and a complete feature set, we need to create a schema and perform code generation specific to our project.
coherence calls this mechanism baking.
Click on coherence / Bake.
This will go through all indexed
CoherenceSyncGameObjects (Resources folders and Prefab Mapper) in the project and generate a schema file based on the selected variables, commands and other settings. It will also take into account any LODs that have been added.
For every Prefab with a
CoherenceSynccomponent attached, the baking process will generate a C# baked script specifically tuned for it.
coherence offers two mechanisms to generate baked scripts: through Assets or through a Source Generator. By default, coherence baked using Assets, but you can change this setting anytime in the coherence Settings window.
Bake modes in the coherence settings window
When you bake using Assets, the generated code will output to Asset/coherence/baked. This is a simple solution, allowing for an easy inspection and debugging of the generated files, but it comes with a few drawbacks:
- You should version the baked files, which can clutter your VCS workflow.
- Since baked scripts access your code, changing your code will get you into compilation errors.
When you bake using the Source Generator, the generated code is fed directly to the compilation pipeline, not generating the files in the Assets folder. In this process, coherence can analyze your code syntactically and semantically. This means it can detect cases where changes in your code can affect the baked files, anticipating compiler errors and avoiding them altogether. This mode comes with a few drawbacks too:
Assets/coherence/Footprint.csis created/updated every bake operation, to trigger a recompile on your code. This file (and its
.meta) can be ignored in your VCS.
- A bake operation is performed on every recompile. For most projects this is not noticeable. But if your project is heavy or your computer is slow, this can take additional time on top of the normal recompilation time.
- Since baked files are not versioned and have to be generated, the protocol code generator (executable bundled with the SDK package) needs to keep its execute permissions and should have write permission on
<project>/Library/coherence. This is usually not a problem, except in continuous integration scenarios, where there might be strict rules on files having the execute permission.
- Harder to debug. Since files are not in Assets anymore, you can't click on them and have proper code completion. The last generation made through the Source Generator is available in
As you can see, there are pros and cons to each mechanism, so we recommend you try both and check what works best for your workflow.
Source generators do not work on Unity version 2021.1. This is a known Unity issue that has no other fix than to upgrade (or downgrade) the version.
Once the baked scripts have been generated, you can make use of it by ticking the checkbox Baked in the CoherenceSync inspector. This is on by default.
When you configure your Prefab to network variables, and then bake, coherence generates baked scripts that access your code directly, without using reflection. This means that whenever you change your code, you might break compilation by accident.
For example, if you have a
Health.csscript which exposes a
public float health;field, and you toggle
healthin the Configure window and bake, the generated baked script will access your component via its type, and your field via field name.
var healthComponent = GetComponent<Health>();
var healthField = healthComponent.health;
When baking via assets, baked scripts will be located in Assets/coherence/baked.
If you decide you want to change your component name (
Health) or any of your bound fields (
health), Unity script recompilation can fail. In this example, we will be removing
health2in its place.
//public float health;
public float health2;
When baking via assets, the watchdog is able to catch compilation problems related with this, and offer you a solution right away.
You can delete the baked folder manually through the coherence Settings window.
It will suggest that you delete the baked folder, and then diagnose the state of your Prefabs. After a few seconds of script recompilation, you will be presented with the Diagnosis window.
In this window, you can easily spot variables in your Prefabs that can't be resolved properly. In our example,
healthis no longer valid since we've moved it elsewhere (or deleted it).
From here, you can access the Configure window, where you can spot the problem.
Now, we can manually rebind our data: unbind
health2. Once we do, we can now safely bake again.
Remember to bake again after you fix your Prefabs.