I’ve seen a lot of discussions on the forums and the unity discord about hybrid workflows in Entities 1.0. Without runtime conversion or companion link, our options are pretty limited. Several people (myself included) have come up with various solutions to bridge the gap. Most of the solutions involve one of the following:
-
Runtime instantiation of a game object prefab. This means the prefab cannot have any scene references.
-
Runtime instantiation of an entity from a MonoBehaviour in the scene. Cannot use Baking to populate the non-managed parts of the entity.
-
Runtime setup of static variables. Works for singleton-like data, but is hard to map to corresponding entities.
-
Assembly ref hacks to expose the old companion link workflow in a Baker.
In my opinion, none of these options are great. They all have downsides and most of them are non-trivial to create.
After some discussions with other folks and unity devs on the unity discord, I came up with a mock up that shows what a possible hybrid workflow could look like.
When a game object is placed inside a subscene, the inspector will show an extra “Bake Unmanaged” checkbox for each UnityEngine.Component attached to the game object. By default, all of these check boxes are unchecked. When they are all unchecked, baking works exactly as it does now. I.e. for each UnityEngine.Component, it looks for bakers of that type and bakes the authoring component into the entity data. However, if any UnityEngine.Component has zero Bakers implemented, it will show a warning in the inspector. This is mainly to prevent confusion when a MonoBehaviour is added to a game object in a subscene but it has no effect at runtime because the baker is missing.
You can see this warning in the inspector under the Animator component. This warning also says that the “Bake Managed” checkbox can be checked. When this is checked, unity will keep the authoring game object alive at runtime, instead of deleting at the end of baking. Any UnityEngine.Components with “Bake Managed” checked will stay on the game object during runtime. Additionally, the UnityEngine.Component will automatically be added to the baked entity as a managed component (using AddComponentObject()). Since a game object must have a transform, checking any of the “Bake Managed” check boxes will force the transform to also be added as managed component.
Here is what it looks like with the Animator’s “Bake Managed” checked (and the Transform implicitly):
Personally, I think this would be a big improvement to the user experience for a hybrid workflow. It is entirely opt-in and gives plenty of control and flexibility.
But there are still some things I’m not sure about. The name of the “Bake Managed” checkbox could probably be improved. Unfortunately, the word “component” is very overloaded so its hard to come up with a succinct name to describe what the checkbox will do. Maybe a good tooltip is sufficient? I also am not sure if that is the best place for the checkbox. It feels like it shouldn’t crowd the row with the component name.
I’m also not sure if the underlying companion game object should be hidden in the hierarchy or not. At first I thought it should be, similar to the old companion links. But thinking on it more, I kind of like the idea that the game object in authoring IS the same as the game object at runtime. The only difference is that some MonoBehaviours were baked into entity data and removed from the game object. I also think it is better to show the user that there is still this managed game object floating around instead of tricking the user into thinking everything is fast baked entity data.
You may have also noticed that I changed the “Entity Baking Preview” window in the screenshots above. Personally, I find the current layout very hard to parse and not at all helpful. Prior to 1.0, conversion allowed multiple monobehaviours to contribute to the same set of components. But in 1.0, Bakers cannot modify components added by other Bakers. This means that baking is always one-to-many between MonoBehaviours and IComponentDatas.
By changing the layout to break it down by the different Bakers, it makes easy to understand what is adding each component. This also can be used to show the managed components added by the “Bake Managed” checkboxes as well as the companion game object that may persist at runtime.
After talking to some of the unity devs in discord, I understand that there may be technical reasons why this is infeasible. However, I do hope that the idea or some other improvement to hybrid is considered. I think that would go a long way towards improving the onboarding experience for new ECS users.
Regardless of what happens with hybrid, I think the warning about a lack of a Baker is needed. It is too easy to make something that seems like it should work but is silently not doing what you expect. I also think some love should be given to the Entity Baking Preview window in the inspector.
Let me know what you think or how this could be improved. Cheers!