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 Help needed with Prefab Conversation in combination with BlobArrays

Discussion in 'Entity Component System' started by davidus2010, Feb 11, 2023.

  1. davidus2010

    davidus2010

    Joined:
    Dec 4, 2017
    Posts:
    72
    Hello, I'm trying to store a list of Prefabs, along with some other configuration, in a shared component. To do so, I'm using a BlobArray, which is a field of the final shared component. This all works fine, but when actually instantiating the entity at runtime, it always seems to just instantiate a random other entity, but not the actual prefab entity that i assigned.
    Do I perhaps need to do some additional configuration on the prefab that should be spawned? I just assumed that calling "GetEntity" during the Baking process would also create a baked version of the prefab. Or is it just not possible to store the references to the baked prefab inside a blobasset? Here's the code I'm using:

    Code (CSharp):
    1. // shortened version of some parts
    2. [Serializable]
    3. public struct VegetationListContainer {
    4.   public BlobArray<VegetationConfiguration> Data;
    5. }
    6.    
    7. [Serializable]
    8. public struct VegetationSettingInput {
    9.   public GameObject Prefab;
    10.   public VegetationConfiguration Settings;
    11. }
    12.    
    13. [Serializable]
    14. public struct VegetationConfiguration {
    15.   public Entity Prefab;
    16.   public float2 HeightRange;
    17.   // ...
    18. }
    19.  
    20. private BlobAssetReference<VegetationListContainer> GenerateDataList(List<VegetationSettingInput> inputs) {
    21.      
    22.   var builder = new BlobBuilder(Allocator.Temp);
    23.   ref var configList = ref builder.ConstructRoot<VegetationListContainer>();
    24.   var arrayBuilder = builder.Allocate(ref configList.Data, inputs.Count);
    25.      
    26.   for (var i = 0; i < inputs.Count; i++) {
    27.     var input = inputs[i];
    28.     var output = input.Settings;
    29.     output.Prefab = GetEntity(input.Prefab);
    30.     arrayBuilder[i] = output;
    31.   }
    32.  
    33.   var result = builder.CreateBlobAssetReference<VegetationListContainer>(Allocator.Persistent);
    34.   return result;
    35. }
    36.  
    37. public struct VegetationSettings : ISharedComponentData, IEquatable<VegetationSettings> {
    38.   public BlobAssetReference<FlatTerrainBake.VegetationListContainer> VegetationList;
    39.   // ...
    40. }
    If anyone had a similar problem or a better idea on how to approach this, any help would be appreciated
     
    bb8_1 likes this.
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,984
    This. Same applies for shared components too.
     
    bb8_1 likes this.
  3. davidus2010

    davidus2010

    Joined:
    Dec 4, 2017
    Posts:
    72
    Ah okay, that explains the behavior. Too bad, having baked prefabs inside shared components or BlobAssets would really make a few things easier.
    When I tested a few more things, it seems like all Entity references that are creating during bake-time don't work with these data types.
     
    One1Guy likes this.