Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

DeclareReferencedPrefabs(List<GameObject> gameObjects) not working, for me.

Discussion in 'Entity Component System' started by digitaliliad, Mar 21, 2019.

  1. digitaliliad

    digitaliliad

    Joined:
    Jul 1, 2018
    Posts:
    64
    I've been trying to implement HelloECS_06, but I can't get my prefabs registered to GameObjectConversionMappingSystem's m_ReferencedPrefabs through DeclareReferencedPrefabs. This function doesn't seem to do anything, out of the box.

    Here's my simpler implementation of HelloECS_06 (without the spawner stuff).

    Code (CSharp):
    1. [RequiresEntityConversion]
    2. public class Converter : MonoBehaviour, IDeclareReferencedPrefabs, IConvertGameObjectToEntity
    3. {
    4.     public GameObject prefab;
    5.    
    6.     public void DeclareReferencedPrefabs(List<GameObject> gameObjects)
    7.     {
    8.         gameObjects.Add(prefab);
    9.     }
    10.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    11.     {
    12.         //Do something, if you like.
    13.     }
    14. }
    Upon inspection, I get my converted entity, and if it's a hierarchy, the correct parent-child relationships, but nothing is tagged as Prefab. What am I missing? I really don't want to make my own GameObjectConversionSystem subclass...

    Also, I'd like to have my hierarchy as a LinkedEntityGroup. I know I could do this with GameObjectConversionUtility.ConvertGameObjectHierarchy(GameObject root, World dstEntityWorld), but I could also do it inside the Convert function with conversionSystem.AddReferencedPrefab(GameObject gameObject), right?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Is the gameobject that is referenced actually a prefab? Currently the system actually checks if it is a prefab before marking it as such in the conversion process.
     
  3. temps12

    temps12

    Joined:
    Nov 28, 2014
    Posts:
    41
    I'm getting the same issue if I have nested references. If I have one gameobject that gets converted and that one declare referenced prefabs, that works. But when those declared prefabs convert they wont have DeclareReferencedPrefabs called so they cant declare more prefabs.
     
  4. digitaliliad

    digitaliliad

    Joined:
    Jul 1, 2018
    Posts:
    64
    I am fairly certain that the parent, atleast, is an actual prefab. The referenced GO is supposed to be an object in the scene, right? And not just a prefab asset? I have not tried establishing each child as an individual prefab before assembling the hierarchy, but as it stands, I can't even get the converted root entity recognized as a prefab, let alone its children.
     
  5. digitaliliad

    digitaliliad

    Joined:
    Jul 1, 2018
    Posts:
    64
    I think I realize what I'm doing wrong: the referenced prefab field is supposed to be filled with the actual prefab asset, not any in-scene instance, right? I feel dumb...

    EDIT: hmm, I'm referencing a GameObject, and I don't see how I can distinguish the prefab itself from any instance of it.
     
    Last edited: Mar 22, 2019
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Yes, in scene hierarchy you have only prefab instances, prefab itself exists only in project folder.
     
  7. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
  8. digitaliliad

    digitaliliad

    Joined:
    Jul 1, 2018
    Posts:
    64
    The HelloECS_06 sample stores the result of GetPrimaryEntity() within component data on the Convert function's parametric entity (as far as I can tell, this entity is identical to the result of GetPrimaryEntity()). It later reads through this data, instantiates copies of the entity, and then deletes the original entity. Let's say I'm doing something similar, my prefab entity and its children are still barren of prefab tags.

    After the convert function, I can see the prefab entity hierarchy in the entity debugger, just fine, so they're clearly there. However, they don't have a prefab tag on them. If I add the tag, manually, they will show up with prefab tags, but I'd like to rely on DeclareReferencedPrefabs().

    I have a single instance of a prefab hierarchy, in-scene, attached with a copy of my Converter class, and a ConvertToEntity(mode set to convert and destroy) component. The instance of the prefab refers to itself in the DeclareReferencedPrefabs(), but its converted entity doesn't gain that status. I'm at a loss.