Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question What is the purpose of BakeSystem?

Discussion in 'Entity Component System' started by jennal, Nov 8, 2022.

  1. jennal

    jennal

    Joined:
    Oct 28, 2017
    Posts:
    24
    It seems that I can't create Entity in BakeSystem.
    I want to bake all the configs of my game into dynamic buffers, and I write a BakeSystem like this:

    Code (CSharp):
    1. [WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]
    2. public partial class ConfigBakeSystem : SystemBase
    3. {
    4.     protected override void OnUpdate()
    5.     {
    6.         var guids = AssetDatabase.FindAssets("t:ScriptableObject", new string[] {"Assets/Game/Res/Config"});
    7.         foreach (var guid in guids)
    8.         {
    9.             var path = AssetDatabase.GUIDToAssetPath(guid);
    10.             var asset = AssetDatabase.LoadAssetAtPath<ScriptableObject>(path);
    11.             var type = asset.GetType();
    12.             var valuesField = type.GetField("Values");
    13.             var itemType = valuesField.FieldType.GenericTypeArguments[0];
    14.  
    15.             var query = GetEntityQuery(ComponentType.ReadOnly(itemType));
    16.             if (!query.IsEmpty) EntityManager.DestroyEntity(query);
    17.            
    18.             var method = EntityManager.GetType().GetMethod("CreateSingletonBuffer");
    19.             method = method.MakeGenericMethod(itemType);
    20.             method.Invoke(EntityManager, new object[] { new FixedString64Bytes(type.Name) });
    21.            
    22.         }
    23.     }
    24. }
    And conf struct looks like:

    Code (CSharp):
    1. [Serializable]
    2. public partial struct BattleItemConf : IBufferElementData, IQueryTypeParameter
    3. {
    4.     public int id;
    5.     public FixedString32Bytes itemName;
    6. }
    7.  
    8. [Serializable]
    9. [CreateAssetMenu(menuName="ExcelObject/BattleItemConf")]
    10. public class BattleItemConfs : ScriptableObject
    11. {  
    12.    public List<BattleItemConf> Values = new List<BattleItemConf>();
    13. }
    OnUpdate
    is called in edit mode. But Entities are not shown when I hit Play.
    If I can't create Entity by
    BakeSystem
    , what is the purpose of
    BakeSystem
    ?
     
  2. topher_r

    topher_r

    Unity Technologies

    Joined:
    Jun 14, 2019
    Posts:
    36
    The reason this Entity is not appearing is because it is not coming from an authoring object in a SubScene. Are you not getting a warning describing this?

    If it did work, your current approach would result in duplicates of these 'singletons' in every Entity Scene that is loaded in, causing them to not be singleton at all. I'd move this to a config SubScene with a single authoring object that has a Baker, and this can produce your Entities with the data on them you want. That way you can guarantee only one by only loading this one SubScene.
     
    SolidAlloy likes this.