Search Unity

Discussion Entity Baking - Useful Information and Discussion [1.0]

Discussion in 'Entity Component System' started by TriceHelix, Jan 20, 2023.

  1. TriceHelix

    TriceHelix

    Joined:
    Mar 6, 2019
    Posts:
    34
    Dear ECS enthusiasts,

    This post is meant to provide some additional information on Entity Baking that is not mentioned within the Documentation. Here's what I've found by examining the Baking process in a test project:

    1) No Entity Baking is performed at Runtime in a Standalone Build
    Pretty self-explanatory. This was something I've been curious about ever since I read the docs about entity conversion. Whatever code you write inside of your
    Baker<T>.Bake()
    implementation will never be executed at runtime, only within the Editor Application. Instead, every Entity Subscene is serialized into binary files after baking. These get shipped with the build in a dedicated subfolder (<ProjectName>_Data/StreamingAssets/EntityScenes).
    The fact that some sort of streaming happens at runtime is mentioned in the docs, though they also say that baking is triggered when one of these files gets "outdated", without specifying that this only happens in the Editor. When Entity Scenes get shipped with a build, they are immutable and never have to be rebaked at runtime.​

    2) UNITY_EDITOR is always defined when baking executes
    Even when
    IBaker.IsBakingForEditor() == false
    (reference), the actual assembly used to execute the baking is the same which gets recompiled every time you change your scripts and gets loaded when opening your project. This assembly is fundamentally different than the one which is compiled when building a project, as everyone that is familar with compiler flags should be aware of.
    Since the UNITY_EDITOR flag is always defined for the baking assembly, we can theoretically exclude any
    Baker<T>
    derived classes entirely from a Standalone Build by wrapping it in a
    #if UNITY_EDITOR
    preprocessor block. Sometimes this is even necessary, for example when working with Scene Asset references (
    EditorApplication.SceneAsset
    ). Anything in the
    UnityEditor
    namespace is allowed as long as we exclude this code from builds using preprocessor defines as mentioned.
    3) You should not rely on IsBakingForEditor() / consistent rebaking for a Standalone Build
    This one I'm not entirely sure about because I may have simply made an error during testing. However, as far as I know, Entity Scenes are not necessarily rebaked for a build. If a scene's cached binary file is already up-to-date, Unity will simply grab that file during the build process without baking it again. If you, for whatever reason, wrote code in a baker which is only supposed to be executed when baking for a build (aka
    IBaker.IsBakingForEditor() == false
    ), this may not always be executed.
    To ensure that all entities are rebaked, navigate to Edit > Preferences > Entities and press "Clear Entity Cache". This deletes any cached binary Entity Scene files and ensures that all Scenes are baked from scratch when you build the project. Beware that opening any subscene or a normal scene with a subscene inside of it will immediately bake that scene after clearing the cache, so you may have to create an extra scene to "evacuate" from the content which you want to rebake.

    Discussion Time
    If anything here is incorrect, or if you wish to provide even more insight, feel free to reply. There are also some questions/topics on my mind about the things I wrote.

    Concerning 1) and 2)
    I've read somewhere that GameObject-to-Entity conversion at runtime similar to the pre-1.0 conversion workflow may be coming in the future. If that is true, this could (in theory) utilize existing entity bakers, in which case both statements 1) and 2) should be taken with a grain of salt. I personally like that entites are now packed into binary files in the Editor instead of the old performance-draining runtime conversion (even if bakers currently feel slightly more boilerplate-y because of the lack of the
    GenerateAuthoringComponent
    attribute).
    Of course there are still plenty of uses for runtime conversion, though I wonder if existing baker classes should even be utilized for that. After all, some bakers may need access to Asset GUIDs and/or other APIs exclusive to the Unity Editor. This makes me curious about how a runtime Entity Converter might function. Then again, this is all speculation and would only be relevant in the case such a converter is in the works.
    Concerning 3)
    I personally haven't utilized IsBakingForEditor() at all, though I am sure someone out there has a good use for it. In case you depend on bakers behaving differently based on whether they are baking Entities for a build, feel free to share that below. Maybe triggering a complete rebake of all Entity Scenes when a project is built would even be a good idea for the people that depend on it, in which case you can consider this a Feature Request.

    Thanks for reading, everyone!
    Cheers.
     
    Last edited: Sep 10, 2023
  2. voxelltech

    voxelltech

    Joined:
    Oct 8, 2019
    Posts:
    44
    Thank you for the explanation, this resolve alot of my issue that I am currently facing.
     
    TriceHelix likes this.