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 Instantiating entities from prefab is too slow with a blobassetstore.

Discussion in 'Entity Component System' started by Kowarenai, Feb 20, 2021.

  1. Kowarenai

    Kowarenai

    Joined:
    May 27, 2019
    Posts:
    14
    So this is what I have for my code to spawn an entity from a MonoBehaviour right now. It works, although the performance is extremely slow. When I have several scripts doing this, there is a huge lag before the entity prefabs are created.

    Code (CSharp):
    1. _blobAssetStore = new BlobAssetStore();
    2. var settings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, _blobAssetStore);
    3.  
    4. _entity = GameObjectConversionUtility.ConvertGameObjectHierarchy(entityPrefab, settings);
    5.  
    When I remove the blobassetstore and pass null, it still works and the performance is much better.

    Code (CSharp):
    1. var settings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, null);
    2.  
    3. _entity = GameObjectConversionUtility.ConvertGameObjectHierarchy(entityPrefab, settings);
    4.  
    However I get the following error:
    ArgumentNullException: A valid BlobAssetStore must be passed to construct a BlobAssetComputationContext
    Parameter name: blobAssetStore
    Unity.Entities.BlobAssetComputationContext`2[TS,TB]..ctor (Unity.Entities.BlobAssetStore blobAssetStore, System.Int32 initialCapacity, Unity.Collections.Allocator allocator) (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities.Hybrid/GameObjectConversion/BlobAssetComputationContext.cs:33)

    Does anybody know a way around this?
    Cheers
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    Runtime conversion is slow and I wouldn't expect it to get much better so you really should avoid it.
    The whole point of subscenes and pre-conversion workflow is to precompute this costly work.

    Packages like netcode have already deprecated runtime conversion and require instead to use the subscene workflow.

    -edit-

    to answer your actual question though. Not sure why that isn't working, looks fine.
    it's failing a null check which seems unlikely on your code (is this just a snippet and there is other code that could be causing the issue?)
     
    Last edited: Feb 20, 2021
  3. Kowarenai

    Kowarenai

    Joined:
    May 27, 2019
    Posts:
    14
    It's definitely changing that value to null instead of using the blobasset that's causing the error. To be honest I haven't looked into the subscene workflow much yet although it is something I've been planning to implement. Is there a way I can precomute the conversion if I'm wanting to instantiate from a monobehaviour? The code is attached to a button click. Although now that I think about it, it could be worth creating an entity event and instantiating the prefab entity within a system if that would improve performance.
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,984
    Instantiating GameObjects to be immediately converted to entities is the wrong way to go, with or without subscenes. You want to have some Authoring component that declares referenced prefabs for these prefabs. And then puts them on a singleton or something where your MonoBehaviour can find them through the EntityManager.

    For one of my projects, I Convert and Inject the canvas, but I also have a component on there that declares prefabs for sound effects and stores them in a resources IComponentData. I then have a system that iterates through both the canvas and the resources in an Entities.ForEach and instantiates the sound effects during click or navigation events.