Search Unity

Resolved ECS Converted Prefab Instantiation Differences

Discussion in 'Entity Component System' started by lndcobra, Nov 28, 2020.

  1. lndcobra

    lndcobra

    Joined:
    Jan 28, 2020
    Posts:
    21
    Does anyone know why when using `GetPrimaryEntity` on an existing Prefab inside the subscene, causes later instantiation to only instantiate the top level prefab entity. with links to existing children (rather than creating whole hierarchy).

    This breaks, and returns only the wrapper entity, pointing at the existing children

    Code (CSharp):
    1. entity = GameObjectConversionSystem.GetPrimaryEntity(Prefab_FromSubScene_HasConvertToEntity);
    2.  
    3. EntityManager.Instantiate(entity);

    This works, and creates a whole new hierarchy
    Code (CSharp):
    1. entity = GameObjectConversionUtility.ConvertGameObjectHierarchy(Prefab_FromSubScene_HasConvertToEntity, settings);
    2.  
    3. EntityManager.Instantiate(entity);
    Is the second method the recommended approach for creating prefab entities?
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    You can use DeclaredLinkedEntityGroup on the primary entity. Is there a reason why you are trying to make prefabs out of subscene entities?
     
    lndcobra likes this.
  3. lndcobra

    lndcobra

    Joined:
    Jan 28, 2020
    Posts:
    21
    Wasn't sure what the best method is, and it is the only way I found that doesn't require ConvertGameObjectHierarchy.

    When I use ConvertGameObjectHierarchy, all the Entity names get lost, and end up just being Entity XXX. Where as subscene converted entities seem to be named the same as their GameObject equivalents. I guess I should at some point create my own conversion helper that uses ConvertGameObjectHierarchy and then goes and fixes up the Entity Names.

    Haven't found any good examples of prefab saving/spawning workflow yet.

    I wish the docs were OSS so we could start conversations / PR's for updates/methods etc. I find it relatively hard to find stuff in the forum.
     
    adammpolak likes this.
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    IDeclareReferencePrefabs is the easiest way to create real entity prefabs. I'm blanking on the GameObjectConversionSystem equivalent and it is a little tricky for me to check right now.
     
  5. lndcobra

    lndcobra

    Joined:
    Jan 28, 2020
    Posts:
    21
    Thanks @DreamingImLatios, worked perfectly! For anyone looking at this in the future:


    Code (CSharp):
    1. public class PrefabComponentAuthoring : MonoBehaviour, IConvertGameObjectToEntity, IDeclareReferencedPrefabs
    2. {
    3.     public GameObject Prefab;
    4.  
    5.     // IDeclareReferencedPrefabs Implementation
    6.     public void DeclareReferencedPrefabs(List<GameObject> referencedPrefabs)
    7.     {
    8.         referencedPrefabs.Add(Prefab);
    9.     }
    10.    
    11.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    12.     {
    13.         var prefabEntity = conversionSystem.GetPrimaryEntity(Prefab);
    14.         // Do whatever you want with this entity. It will be converted correctly including all names.
    15.     }
    16. }
     
    tmonestudio likes this.
  6. adammpolak

    adammpolak

    Joined:
    Sep 9, 2018
    Posts:
    450
    @Indcobra do you put this on the prefab in your assets folder or in a GameObject in your subscene?

    If you have added a few entities with IDeclareReferencedPrefabs, how can you instantiate them in an entirely different system?