Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

I still don't get BlobAsset

Discussion in 'Entity Component System' started by davenirline, Nov 6, 2020.

  1. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    969
    I thought there's some kind of central BlobAsset database/storage somewhere that you can access. You create data once then access it anywhere. This doesn't seem to be the case. My current understanding is that you build BlobAssetReference instances and it's up to you how to store it. I made a basic test for this:

    Code (CSharp):
    1. [TestFixture]
    2. [Category("CommonEcs")]
    3. public class BlobAssetTest : ECSTestsFixture {
    4.     private struct TestData {
    5.         public int value;
    6.     }
    7.  
    8.     private struct TestComponent : IComponentData {
    9.         public BlobAssetReference<TestData> reference;
    10.     }
    11.  
    12.     private const int TEST_DATA_VALUE = 111;
    13.    
    14.     private class BlobAssetSystem : SystemBase {
    15.         private BlobAssetReference<TestData> reference;
    16.        
    17.         protected override void OnCreate() {
    18.             base.OnCreate();
    19.            
    20.             // Prepare the blob asset
    21.             BlobBuilder builder = new BlobBuilder(Allocator.TempJob);
    22.             ref TestData data = ref builder.ConstructRoot<TestData>();
    23.             data.value = TEST_DATA_VALUE;
    24.             this.reference = builder.CreateBlobAssetReference<TestData>(Allocator.Persistent);
    25.             builder.Dispose();
    26.  
    27.             Entity entity = this.EntityManager.CreateEntity(typeof(TestComponent));
    28.             this.EntityManager.SetComponentData(entity, new TestComponent() {
    29.                 reference = this.reference
    30.             });
    31.         }
    32.  
    33.         protected override void OnDestroy() {
    34.             this.reference.Dispose();
    35.         }
    36.  
    37.         protected override void OnUpdate() {
    38.             this.Entities.ForEach(delegate(in TestComponent component) {
    39.                 Debug.Log($"value: {component.reference.Value.value}");
    40.                 Assert.IsTrue(component.reference.Value.value == TEST_DATA_VALUE);
    41.             }).WithoutBurst().Run();
    42.         }
    43.  
    44.         public void CreateNewEntity() {
    45.             Entity entity = this.EntityManager.CreateEntity(typeof(TestComponent));
    46.             this.EntityManager.SetComponentData(entity, new TestComponent() {
    47.                 reference = this.reference
    48.             });
    49.         }
    50.     }
    51.  
    52.     [Test]
    53.     public void BasicUsageTest() {
    54.         BlobAssetSystem system = this.World.GetOrCreateSystem<BlobAssetSystem>();
    55.         system.Update();
    56.         system.CreateNewEntity();
    57.         system.Update();
    58.     }
    59. }
    In this test, the BlobAssetReference is maintained as a member variable. What I'm imagining is that every time a new entity is created that requires this data, it has to use this member variable. To access this data from another system, I have to inject this system then access the member variable. I was hoping there was another way to access a specific BlobAssetReference without injecting a system or some other class where all BlobAssetReferences are stored. Or is there a method for what I want? Some like BlobAsset.GetReference<T>().
     
    lndcobra likes this.
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,222
    There is no single storage. So yes you have to manage their lifetimes yourself. In the case of GameObjectConversion, there's BlobAssetStore. There's one per each subscene as well as one for runtime conversion in the ConvertToEntitySystem. With some internal hacking, you might be able to piggyback off of those.
     
    lndcobra and davenirline like this.