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

How are we meant to create new entities from prefabs in the middle of program execution?

Discussion in 'Entity Component System' started by strblueshell, Jun 3, 2022.

  1. strblueshell

    strblueshell

    Joined:
    Apr 29, 2022
    Posts:
    9
    This seems like an embarrasingly basic question, yet I can't seem to find an answer for it in any documentation or thread.
    Conversion systems and automatic conversion via IConvertGameObjectToEntity seem to only run when the program starts.
    (I know that EntityManager.CreateEntity can be used to manually create entities anytime, but you can't use this to create an entity from a prefab.)
    GameObjectConversionUtility seems like it works right now but I've read that it is supposed to be deprecated; therefore, there must be another canonical way to do this, right?

    Edit: example use case: I'd like to have a spawner entity, containing a component that references an entity prefab, and instantiate that prefab periodically.
     
    Last edited: Jun 3, 2022
  2. Soaryn

    Soaryn

    Joined:
    Apr 17, 2015
    Posts:
    327
    Im on my phone at the moment so I don't have the links readily available. Yes, you can create from prefabs is the short answer.


    Unity has an entities repository on Github with examples. One of which is exactly spawn from prefab.

    As a test, (tgis used to work) make a new file /struct implementing IComponent Data, make a pu lic field Entity, then add the attribute to the struct `GenerateAuthoringComponent`

    In Unity, a new component should be available whose field is a GameObject. That, again used to, concert to an entity. Though I'd still recommend the github
     
  3. Mayumichi

    Mayumichi

    Joined:
    Mar 12, 2017
    Posts:
    31
    After the above you can use EntityManager.Instantiate() to instantiate it.
     
  4. Soaryn

    Soaryn

    Joined:
    Apr 17, 2015
    Posts:
    327
    EntityManager or what I'd recommend, EntityCommandBuffer as you can jobify it and no cause a sync point unintentionally
     
  5. bb8_1

    bb8_1

    Joined:
    Jan 20, 2019
    Posts:
    98
  6. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Get in line I have more embarrassing DOTS questions than you! :D But seriously, DOTS is really bleeding edge so no questions are bad questions. The answers are super varied at times though.
     
    MNNoxMortem and Anthiese like this.