Search Unity

How do i use the Attach Component in Hybrid Mode ?

Discussion in 'Entity Component System' started by Held0fTheWelt, Jan 30, 2019.

  1. Held0fTheWelt

    Held0fTheWelt

    Joined:
    Sep 29, 2015
    Posts:
    173
    Yes - That's a question ...

    I can do it in code, but in hybrid, i don't know how to do this...
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    You're asking about how to add the component to the gameobject?

    Add the Component monobehavior like any other script to the gameobject in the scene view / project view.

    If it's missing from the list, then you need to implement ComponentDataWrapper, or shared wrapper based on what type of component you're trying to add.

    Simplest thing is to convert the gameobject with a bunch of components on it I found is the:
    Code (CSharp):
    1. Entity entity = EntityManager.Instantiate(gameobject);
     
  3. Held0fTheWelt

    Held0fTheWelt

    Joined:
    Sep 29, 2015
    Posts:
    173
    You cannot put a gameObject in there, as it looks.
    Has to be an entity
     
  4. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    If you mean create it in the editor without code you can't just because Entities won't serialize in the inspector.
    What i did was to create a monobehaviour for the Attach component.
    Code (CSharp):
    1. [ExecuteInEditMode]
    2. public class AttachHybridComponent : MonoBehaviour {
    3.   public GameObjectEntity Parent;
    4.   public GameObjectEntity Child;
    5.  
    6.   private void Start() {
    7.     var em = World.Active.GetOrCreateManager<EntityManager>();
    8.     var e = em.CreateEntity();
    9.     em.AddComponentData(e, new Attach {
    10.       Child = Child.Entity,
    11.       Parent = Parent.Entity
    12.     });
    13.   }
    14. }
    Then you must create 3 game objects:
    1- Parent with at least a Position/Rotation/Scale Component
    2- Child with at least Position/Rotation/Scale Component
    3- Attach with the monobehaviour AttachHybridComponent and both parent and child enities assigned

    In alternative you can just add LocalToWorld instead of Position/Rotation/Scale.

    Hope it helps!
     
    Held0fTheWelt likes this.
  5. Held0fTheWelt

    Held0fTheWelt

    Joined:
    Sep 29, 2015
    Posts:
    173
    That's looking quite good !
    But why do you have to ExecuteInEditMode, as the entity manager runs on runtime ?

    Last - doesn't want to smart aleck, but i choose to set also [RequireComponent(typeof(LocalToWorld))] or sth. like this on top for mandatory things !
     
  6. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    The ExecuteInEditMode is for allowing you to see the attachment without having to hit play, it is deprecated now so you should use ExecuteAlways instead.
    The [RequireComponent(typeof(LocalToWorld))] makes no sense here because you don't want to add that to the attach gameobject but rather to the Parent and Child gameobjects.
    You could add this to the Start or Reset function:
    Code (CSharp):
    1.     if (!Parent.GetComponent<LocalToWorldComponent>())
    2.       Parent.gameObject.AddComponent<LocalToWorldComponent>();
    3.     if (!Child.GetComponent<LocalToWorldComponent>())
    4.       Child.gameObject.AddComponent<LocalToWorldComponent>();
     
    Last edited: Feb 1, 2019
    Held0fTheWelt likes this.
  7. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    You can. (p23) Just attach components (!) to the gameobject. Sure default monobehaviours won't fit.

    EntityManager.Instantiate(gameObject) creates an entity that has every component data / shared data attached to it as in prefab.

    Entities won't serialize in inspector. But ComponentData / SharedData will. I'm using default prefabs as templates, and prepare them as entities in bootstrap phase. Works like a charm for me.
     
  8. Held0fTheWelt

    Held0fTheWelt

    Joined:
    Sep 29, 2015
    Posts:
    173
    In my current preview, it only sais:

    EntityManager.Instantiate(Entity entity), and he really doesn't like me to put a gameobject in, so i don't get, what you are saying.
     
  9. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,684
    upload_2019-2-1_19-57-40.png

    It's from EntityManagerExtensions class. From Unity.Entities.Hybrid
    upload_2019-2-1_19-58-31.png
     
    Held0fTheWelt and xVergilx like this.