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 How do you spawn an entity from GameObject code in the new Baker flow?

Discussion in 'Entity Component System' started by alloystorm, Nov 4, 2022.

  1. alloystorm

    alloystorm

    Joined:
    Jul 25, 2019
    Posts:
    84
    With the runtime conversion flow deprecated, how do I call a Baker to create my entity from a GameObject?
     
  2. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695
  3. alloystorm

    alloystorm

    Joined:
    Jul 25, 2019
    Posts:
    84
    My plan is to use the ECS system just for the physics part, the rest of the game stays with GameObjects for now.

    But I need to create those physics components on the fly, and they need to work with other gameobjects, for example a chain of connected rigidbodies attached to an object that's animated by the animation system.

    All the sample projects I've seen either have entities authored in sub-scenes or created by spawner systems. What I needed is a way to create entities from non-ECS code.
     
  4. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    939
    Split your object in 2.
    One for the presentation (GameObject) on for the simultation (Entity).
    Use prefab to spawn them at runtime.
    This process is described in my latest tutorial video (109). Link to the series in my signature.
     
    alloystorm and bb8_1 like this.
  5. alloystorm

    alloystorm

    Joined:
    Jul 25, 2019
    Posts:
    84
    Thanks a lot!

    You tutorial is very helpful. I get it that you can control game objects from managed systems.
    But what I'm looking for is the other way around. I need game objects to control when and where entities are spawned and how long they live. Or maybe is there an efficient way to send data to entity spawn systems so it knows when and how an entity should be spawned.
     
  6. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    939
    alloystorm likes this.
  7. alloystorm

    alloystorm

    Joined:
    Jul 25, 2019
    Posts:
    84
    That does seem to work. I can create entities and add component data with the EntityManager.
    Do you know if there's a way to use GetEntity(prefab) outside of a Baker? Looking at the entities generated by bakers, there are a lot of components that I don't know what they are. It's going to be a pain to add every component by hand.
     
  8. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    939
    No there is no way that way that I know of.
    ECS philosophy is to process as much work as possible at build/authoring time for the runtime to be the most performant.
    Creating entity at runtime from scratch should be a very rare scenario in my opinion.
    The only work around I can think of is to make a prefab dictionary.

    Make an entity with a lost of prefab game objects.
    Bake it to an entity with a structure to store he go guid and it's entity representation. Dynamic buffer, component with hashap, ...)
    Use that to spawn at runtime
     
  9. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    Take a peek at https://github.com/VergilUa/EntitiesExt / EntityBehaviour

    What you want is to store Entity & EntityCommandBuffer, with that, you can do pretty much any structural change.
     
  10. alloystorm

    alloystorm

    Joined:
    Jul 25, 2019
    Posts:
    84
    Love your solution especially the lifecycle part!

    I'll probably write my own trimmed down version of your EntityBehavior since my usecase is quite specific. Definately going to copy your lifecycle management. Thank you very much for your help!
     
    xVergilx likes this.