Search Unity

Animator Component on Entity

Discussion in 'Entity Component System' started by diXime, Jan 8, 2020.

  1. diXime

    diXime

    Joined:
    Oct 2, 2018
    Posts:
    162
    Hello hello everyone !

    I've been looking around for solutions on how to add an Animator Component to an entity (I need this one for smoother homemade transitions) but found nothing quite clearer than "not implemented yet except in hybrid".
    I may have found a quick workaround :
    Code (CSharp):
    1. entityManager.AddComponentObject(entity, animator);
    But the component remains not found (I get a NullReferenceException), even though I'm in a ComponentSystem (and not an actual job), and made sure that the type is added to the archetype.

    My test code is extremely simple, especially when cleaned of rotations and translations.
    Code (CSharp):
    1.  
    2. public class System_Mover : ComponentSystem
    3. {
    4.     EntityQuery anim_group;
    5.     EntityManager entityManager;
    6.  
    7.  
    8.  
    9.     protected override void OnCreate()
    10.     {
    11.         anim_group = GetEntityQuery(typeof(Animator));
    12.         entityManager = World.Active.EntityManager;  // I'm seeing now this could be better, my bad
    13.     }
    14.  
    15.  
    16.  
    17.     protected override void OnUpdate()
    18.     {
    19. Entities.With(anim_group).ForEach((Entity entity, Animator anim, ref Translation translation) =>
    20.         {
    21.  
    22.  // sparing you unrelated details [...]
    23.  
    24.             if (anim)
    25.             {
    26.                 anim.Play("walk");  // I know keys are faster but this is a test.
    27.             }
    28.             else
    29.             {
    30.                 Debug.Log("Animator not found"); // Execution lands here, meaning anim is false
    31.             }
    32. }
    33. }
    I've tried making a proxy IComponentData to no avail. I've actually tried several things and harassed Google to a point most of advertisement is talking about it.

    I feel like I could learn hybrid instantiation to achieve this, by making a classic GameObject with only an Animator Component and convert it (I haven't quite looked yet at the hybrid way and pure is actually fun and mind-bending, which I kinda like) but I feel it could be done better. Is my feeling true? If not, how to properly do this?

    I'm asking this as a provisional solution, because my teammate doing animations does not dare working with Animator if ECS doesn't support them. I'm guessing a proper support should arrive one day (when?).

    Thank you in advance.
    Xime.
     
    Last edited: Jan 14, 2020
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,684
    I guess your GO with animator has ConvertToEntity with ConvertAndDestroy mode which destroys GO and Animator itself. Only solution is have GO which linked with entity and synch data between them or use ConvertAndInjectGameObject mode for ConvertToEntity.
     
  3. c0ffeeartc

    c0ffeeartc

    Joined:
    Jul 23, 2015
    Posts:
    42
    @eizenhorn This would solve Unity ECS missing features. Is there a code example of such setup? Thanks
     
  4. diXime

    diXime

    Joined:
    Oct 2, 2018
    Posts:
    162
    I do not use GameObjects to craft and spawn entities. I will look that up.

    ^ I would appreciate it too.
     
  5. diXime

    diXime

    Joined:
    Oct 2, 2018
    Posts:
    162
    For those reading and asking the same, here is a solution and a caveat regarding Animating in ECS context.

    Assuming you have a GameObject with an Animator Component and a ConvertToEntity Component set to ConvertAndInject as such:


    you can add the componentData to your pure ECS entity with:
    Code (CSharp):
    1. GameObjectEntity.AddToEntity(entityManager, animatorGO, entity);
    To me, it means I have to keep a "dummy game object" with an empty animator that I properly fill in code with its runtimeAnimatorController.

    CAVEAT : Many of important animations (such as the ones I was trying to make) requires a SkinnedMeshRenderer. These can be added to entities with the same method than the Animator Component BUT they usually requires deep hierarchy with bones and such, which is extra work (I haven't tested those yet) and may be beyond the scope of entities.
     
    Last edited: Jan 26, 2020