Search Unity

ConvertToEntity with connected Components

Discussion in 'Entity Component System' started by runner78, Apr 6, 2019.

  1. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    792
    How do I find the entity of a connected component?
    I tried the following code, but it does not work right. Both have the "NodeBehaviour".

    The converter of the parent component using a new empty entity, not the same from "conversionSystem.GetPrimaryEntity(this.parent.gameObject);".

    Code (CSharp):
    1. [DisallowMultipleComponent]
    2. [RequiresEntityConversion]
    3. public class NodeBehaviour : MonoBehaviour, IConvertGameObjectToEntity
    4. {
    5.     [SerializeField]
    6.     private NodeBehaviour parent;
    7.  
    8.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    9.     {
    10.         dstManager.AddComponent(entity, typeof(Node));
    11.         if(this.parent != null)
    12.         {
    13.             var parent = conversionSystem.GetPrimaryEntity(this.parent.gameObject);
    14.             dstManager.AddComponentData(entity, new NodeParent() { Value = parent });
    15.         }
    16.     }
    17. }
    18.  
     
  2. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    You also need to implent IDeclareReferencedPrefabs interface. Here is an example.
    Have a look at the possible workflows here
     
  3. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    792
    In my case, the problem was another.

    I checked the m_GameObjectToEntity NativeHashMap in the GameObjectConversionMappingSystem
    It stores the entity's index in the m_Enteties array with the instance ID of the GameObject.

    On first Node, both the parent and the node isself are in the hashmap
    in this case the child has Entity 1 and the Parent Entitiy 2

    Next step convert the Parent,Entity 1 and Entity 2 not in the Entity array,

    Looks like different instances of GameObjectConversionSystem for each confertor.

    Solution:
    Only one ConvertToEntity on an empty Gameobject as parent for all other Gameobjects.