Search Unity

Add entity to GameObject

Discussion in 'Entity Component System' started by Vacummus, Nov 11, 2019.

  1. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    Is it possible to add an existing entity to a GameObject (or vise versa)? I know the ConvertToEntity workflow allows you to create a new entity and associate it with a GameObject (and it's representative MonoBehaviour components). But I am looking for ways to associate existing entities and GameObjects with one another.
     
  2. BackgroundMover

    BackgroundMover

    Joined:
    May 9, 2015
    Posts:
    224
    You can make an Entity field in a MonoBehaviour component which sits on a gameobject, but I don't think there is a way to go in reverse, from an ECS component back to a reference in the gameobject world without doing it manually like making a dictionary where the entity is the key that points to a GameObject value.

    Or if specifically you're talking about during Conversion, how to get an already-existing entity that was created for some other object, try GetPrimaryEntity()
     
    Last edited: Nov 11, 2019
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    You probably need some sort of custom tween / tweening mechanism. If you search these terms you will find some suggestions.

    I personal used Native Array in past, to store pairs of GO-Entities. Hash map could work too in certain conditions.
     
  4. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    Figured it out after some digging:

    Code (CSharp):
    1. GameObjectEntity.AddToEntity(EntityManager, gameObject, entity);
     
    JBR-games and Antypodish like this.
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    Looks like some potentially nice features.
     
  6. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    Found a bug when destroying a GameObject with the following method:

    Code (CSharp):
    1. GameObjectEntity.Destroy(gameObject);
    Though it destroys the GameObject and all it's MonoBehaviour components, the MonoBehaviour components are still queryable (though their values in the array are null). As a work around, I found that I had to manually remove these components before destroying the GameObject, like so:

    Code (CSharp):
    1. var components = gameObject.GetComponents<Component>();
    2.  
    3. for (var i = 0; i < components.Length; i++)
    4. {
    5.     EntityManager.RemoveComponent(entity, components[i].GetType());
    6. }
     
    JBR-games likes this.
  7. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    I highly recommend avoiding using anything GameObjectEntity related. Just going to come back and bite you at some point.
     
    MNNoxMortem likes this.
  8. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    How come? What issues have you experienced with using it?
     
  9. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,684
    Main issue is - it will be removed in one of future releases.
     
    MNNoxMortem likes this.
  10. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    Do you know of alternatives to this approach? Or do you know if Unity will provide other ways of doing this when they depreciate GameObjectEntity in future releases?
     
  11. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    Just experimented with directly adding the MonoBehaviour components to the entity without GameObjectEntity, and this works great and is exactly what I am looking for:

    Code (CSharp):
    1. var components = gameObject.GetComponents<Component>();
    2.  
    3. for (var i = 0; i < components.Length; i++)
    4. {
    5.     EntityManager.AddComponentObject(entity, components[i]);
    6. }
    So yeah, no need for GameObjectEntity, it's pretty strait forward and easy to do it yourself manually.
     
    XCO likes this.