Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

GameObject To Entity Conversion Asynchronously

Discussion in 'Data Oriented Technology Stack' started by Opeth001, Jun 25, 2019.

  1. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    230
    is there any way to convert Game Objects to Entities Asynchronously ?
    in my game i need to generate a Map converting all GameObjects received as Addressables to Entities and Then i instantiate them using a List of Transforms.

    this way my Scene size went from 300mo to a binary file of 2mo without using any compression also it takes less time to generate the map than the traditional Scene loading.

    my problem is all these steps are taking pratically arround 25s to complete.
    1) Read the Binary File and convert it to the MapInfos Struct : arround 2s
    2) Load the Addressables (690 GameObjects): arround 5-6s
    3) Converting the Game Objects to Entities (690 GameObjects) : 14-18s
    4) Generate the Map instantiating the Entities ( 200k entities ) : arround 1-2s

    when users try to play again i only need to do the 4th step.

    as you can see the 3 step is too heavy and cant be done into another Thread because we are using GameObjects and they are not Thread Safe, i can use the fact that the players are waiting into the lobby to do the conversion part but it's totaly blocking the MainThread which will negatively impact the users experience by making everything freezing during this Task.

    i also tried to Serialize the World and store the Result of the Conversion but the Deserialization part is not working without any error or anything that can let me figure out what is the problem.

    Any ideas/suggestions welcome!
     
  2. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    143
    Can't you store your world in a subscene?
     
  3. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    230
    do you have any example on how to work with subscenes ?
    Thank you!
     
  4. starikcetin

    starikcetin

    Joined:
    Dec 7, 2017
    Posts:
    224
  5. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    709
    Does your gamobjects structure will change if you convert to entities during gameplay?
    Or our is your gameobject structure always the same and static?

    Currently there is no direct way to store or serialize entities or content of an entity manager.
    If you need a static scenery converted to entities or into an enity manager, you can save your gameobject setup into a separate scene. Build up static lists or better arrays where you prewarm all references and all transforms and what ever you need, and store with the scene. If you load the scene aysnc, use this static lists and arrays to prepare your entities by a co-routine or job or other thread to prevent blocking the main thread, while preparing.

    Keep in mind while preparing an entity manger, the ecs rendering process is on wait state for this particular manager while creating a sync point.

    This is a work around, but it cannot beat the performance to have direct chunk serialization store/load of entities or entity manager content async.

    The mega city demo provide a custom solution for that behavior, where the demo team is very proud of performance.
    But there is some courios magic behind.

    Older videos from Joachim Ante 2018 providing info of gameobject creation of 10.000 in about some milliseconds.But syntax and ecs behavior changed, and examples never worked again.
     
    Last edited: Jun 25, 2019
  6. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    230
    I ended up by using Subscenes,
    is there any way to load subscenes in a diffrent world or load / Unload them manually ?
     
  7. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    143
    You can manually load/unload them by adding/removing "RequestSceneLoad" to/from their entity.
     
    Opeth001 likes this.
  8. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    230
    Thank you. @daschatten
    what happens to the entities within a subscene when this one is unloaded ?
    how can i Query for entities that are subscenes ?
    is it possible to use the Conversion pipeline to add CD to Subscene Entities ?

    pls point me to subscenes documentation if there is one.
     
  9. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    143
    @Opeth001

    > what happens to the entities within a subscene when this one is unloaded ?

    They are removed

    > how can i Query for entities that are subscenes ?

    Query for "SubScene"

    > is it possible to use the Conversion pipeline to add CD to Subscene Entities ?

    Yep, just throw GameObjects in the SubScene and add MonoBehaviours with
    IConvertGameObjectToEntity (=Conversion Workflow)

    > pls point me to subscenes documentation if there is one.

    https://docs.unity3d.com/Packages/com.unity.entities@0.0/api/Unity.Scenes.SubScene.html
    https://github.com/Unity-Technologies/EntityComponentSystemSamples/
     
  10. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    230
    Thank you @daschatten You are Awesome!

    the link you gave me is not linking to subscene documentation it's just the Scripting API, there is no explanation for the methodes implementation. like what does the CleanupLiveLink() ?

    a last question ^_^.
    how can i add a CD to the Subscene entity itself ? so i can use the CD to identify the subscene at runtime.
     
  11. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    143
    > the link you gave me is not linking to subscene documentation it's just the Scripting API, there is no explanation for the methodes implementation. like what does the CleanupLiveLink() ?

    Read the source :) (no more information yet)

    > how can i add a CD to the Subscene entity itself ? so i can use the CD to identify the subscene at runtime.

    You can't. I also need this feature and hope it is coming soon! I maintain a list of SubScene MonoBehaviours with additional information (level number in my case) and compare it to the existing ones to find the correct one.
     
    Opeth001 likes this.
  12. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    230
    Thanks!