Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Pure Entity Prefabs

Discussion in 'Data Oriented Technology Stack' started by orionburcham, Jan 26, 2019.

  1. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    491
    I've run into cases in which I'd like to create several entities in a predefined group, which have linkages to each other.

    The community has found a few way to do this now, but they don't seem ideal - most involve writing multiple systems, which form a series of steps that create, then link the different entities together.

    Most of the entity groups I want to create have a structure similar to a Unity prefab, with a single entity considered the 'root'. For a very hypothetical example, imagine a game character with one entity for it's body, and two more entities - one for each hand.

    In a case like that, I would love to be able to write code like this, in a Burst compiled Job:

    Code (CSharp):
    1. // queueing a command for a later system to spawn multiple entities, with references to each other.
    2. buffer.InstantiateNPCEntityGroup();
    3.  
    4. // queueing a command to set a new Position for the 'root' entity of the group created on the previous line.
    5. buffer.SetComponent(new Position(/*...*/));
    <Those 'buffer's aren't EntityCommandBuffers, since those aren't currently Burst compliant. They're something else, like the NativeQueues people have been using.>

    I've been looking into ways that convenient syntax might be achieved, or a better syntax altogether (If you know one, please comment below!). I do have something like this working, but I'm unhappy with the level of complexity.

    However, since this approach is so close to how people use normal Unity prefabs, I'm wondering if there are plans to create a pure-ECS prefab format in the future. I would imagine something like this might come along when a pure entity-based scene format in released. I'm just writing this post to inquire for more information. :)

    Thank you for any help or info!
     
    Last edited: Jan 26, 2019
    GliderGuy likes this.
  2. Chris_vf

    Chris_vf

    Joined:
    Jul 19, 2015
    Posts:
    9
    Can't you achieve that with archetypes?
     
  3. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    491
    The things (I believe) you can't achieve by just instantiating archetypes:

    - setting default Component values
    - setting references between newly instantiate entities, to each other (in their Component data).

    If both of these can be done purely through archetypes, that would solve everything! Please let me know. :)
     
    Last edited: Jan 26, 2019
    GliderGuy likes this.
  4. Chris_vf

    Chris_vf

    Joined:
    Jul 19, 2015
    Posts:
    9
    - I think you would have to set the component data manually after instantiating, but I'm not sure about that.
    - About the references, there is a TransformParent component in Unity.Transforms that you could use, or it's possible to make your own if you want a more complicated structure/different use.
     
  5. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    1,852
    I haven't tried it yet (I've been using a scriptable object system to create my entities) but have you used

    EntityCommandBuffer.Instantiate(Entity entity)

    You can setup prefab entities using the Prefab component (Unity.Entities.Prefab)
     
    xVergilx likes this.
  6. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    520
    I think op problem is that he wants multiple entities acting as a single prefab (like classic Unity prefab with children).

    it could be done like this:
    setup:
    - you have a PrefabSystem. in OnCreateManager you collect all your (classic) prefabs then:
    - for each prefab, iterate all the children, create an entity per gameobject, then store in an internal NativeMultiHashMap<Entity,Entity> the relationship [root entity -> all children and self]. add Prefab component to everything
    - each prefab should be itself a tree, i.e. use the TransformSystem Attach/Parent
    instantiate:
    - you define a
    struct Instantiate : IComponentData {public Entity prefab;}

    - create an Instantiate entity, passing the target prefab's root entity
    - PrefabSystem picks up the Instantiate command, gets the entities from the hash map, then instantiate all of them.

    it's missing how to set component data to custom values during instantiate, but you could write to the prefab itself (if you don't instantiate twice in a frame) or have a separate initialization step
     
    MostHated likes this.