Search Unity

It lose a component when I use EntityManager.Instantiate to instantiate a entity

Discussion in 'Entity Component System' started by ThreeIce, Aug 20, 2019.

  1. ThreeIce

    ThreeIce

    Joined:
    Aug 15, 2019
    Posts:
    8
    I have a componentdata called Identifiction. I use GameObjectConversationUtility.ConvertGameObjectHierarchy to convert a prefab. There is a identification component in the converted prefab. But when I use it to instantiate a new entity, there is no identification on it.
    Could you please tell me if there is any mistake in my code?
    Thanks.
    Code (CSharp):
    1. [DisableAutoCreation]
    2.     public class PrefabInstantiator : ComponentSystem
    3.     {
    4.      
    5.         public Dictionary<AssetRef, Entity> Prefabs = new Dictionary<AssetRef, Entity>();
    6.         protected override void OnUpdate()
    7.         {
    8.  
    9.         }
    10.        
    11.         public (Entity,GameObject) Instantiate(AssetRef ObjectRef)
    12.         {
    13.          
    14.             if (Prefabs.ContainsKey(ObjectRef))
    15.             {
    16.                 Entity e = EntityManager.Instantiate(Prefabs[ObjectRef]);
    17.                 return (e, null);
    18.             }else
    19.             {
    20.                 GameObject prefab = ObjectRef.Get<GameObject>();
    21.                 GameObject instance = GameObject.Instantiate(prefab);
    22.                 Entity e = GameObjectConversionUtility.ConvertGameObjectHierarchy(instance, World);
    23.              
    24.                 if (instance.GetComponent<WorldObject>().CanBeDelete())
    25.                 {
    26.                     GameObject.Destroy(instance);
    27.                     Entity eprefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(prefab, World);
    28.                     EntityManager.AddComponentData<Prefab>(eprefab, new Prefab());
    29.                     Prefabs.Add(ObjectRef,eprefab);
    30.                     return (e, null);
    31.                 }
    32.                 return (e, instance);
    33.             }
    34.            
    35.         }
    36. }
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    You are instantiating a GameObject, then converting it twice, and adding a prefab component to one of the resulting Entities.

    You really only need to convert the GameObject prefab, add the Prefab component to the resulting Entity, and then instantiate the Prefab entity.
     
  3. ThreeIce

    ThreeIce

    Joined:
    Aug 15, 2019
    Posts:
    8
    Some prefab's instances can't be destroyed after instantiate because there are some monobehaviour can't convert to componentdata. So the system find the prefab that the instance of it can be destroyed and use the entity prefab to instantiate. Other prefabs must use GameObject.Instantiate to instantiate then convert.
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    Identification is a MonoBehaviour, isn't it...
    Hybrid prefab entities aren't really a supported thing.