Search Unity

Convert to entity is so slow

Discussion in 'Entity Component System' started by davenirline, Jun 30, 2019.

  1. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    Our game agents are originally prefabs with lots of components and some hierarchy of child objects. We used GameObjectEntity to make them work with ECS. However, when I used ConvertToEntity, the game spikes whenever such prefab is instantiated. Maybe because the prefab is too big with lots of components.

    Maybe I did it wrong. I applied ConvertToEntity to the child GameObjects because they're easier to convert. For now, there's like three of them. When I realized that it's slow, I surmised that maybe I don't need three ConvertToEntity components. I just need one at the parent. But when I tried this with a simple test, it doesn't work. A ConvertToEntity component added to the parent does not convert the children at all.

    What are my other options? I want to get rid of GameObjectEntity because they will be deprecated soon.
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    You’re instantiating at start (automatically)? Or in process you calling ConvertGameObjectHierarchy? In second case every call creates conversion world, systems, converts thing and destroying world with systems.
     
  3. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    No we're not instantiating at start. We just have a prefab that we instantiate during some interaction, say player wants to make a worker. They will click on Worker button then click on the map. That's when we instantiate the prefab. We're using a pooling asset but internally, it just uses plain old Instantiate(). Basically, I'm not sure if we're using ConvertGameObjectHierarchy. We just added ConvertToEntity component and some MBs implementing IConvertGameObjectToEntity. The current systems work fine. It just causes really high spikes.
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Ah you just instantiate already converted prefab? I told about conversion case :)
     
  5. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    492
    In most cases, you'd want to convert all traditional prefabs into entity prefabs at once at the start. And just use the entity prefabs from then on.

    The IConvertGameObjectToEntity API can be slow because of all the individual AddComponentData() calls.
     
  6. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    966
    Try to prevent SharedComponentData, I had huge spikes when I instantiated many entities even from a pre-converted gameobject. Instantiating around 4.6k entities took some seconds, without the SharedComponentData it was in the milliseconds again.
     
  7. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    My problem with this is that custom MonoBehaviours are not added. I tried it and seems like it only adds standard components like Transform and MeshRenderer to the converted entity. This won't do because most other systems in the game rely on those MBs.

    Edit: Not entirely sure if my custom MBs are not added to the converted entity but their Awake(), Start(), and Update() are not called at all.
     
    Last edited: Jul 1, 2019
  8. tarahugger

    tarahugger

    Joined:
    Jul 18, 2014
    Posts:
    129
    You can't just have game logic in MonoBehaviors converted into ECS. Any game logic you have on the prefabs needs to be manually converted into ECS Systems.

    The ConvertToEntity will handle setting up default components where possible such as Transform, Rendering, Entity Parenting, LocalToWorld calculations etc. The resulting entities are then able to be automatically updated by default ECS transform systems so you don't have to write that stuff yourself.

    What you can do is implement IConvertGameObjectToEntity and in the conversion method add any components and set the values you need from the prefab.

    see example here : https://github.com/Unity-Technologi...ssets/Common/Scripts/EntityKillerBehaviour.cs
     
    SugoiDev likes this.
  9. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    That's a bummer then because it will take a big rewrite or refactoring to existing systems. If this is the direction of hybrid, it's not practical for existing projects that are already big.

    I hope GameObjectEntity will not be eventually removed then if there's no other simple alternative.