Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

How to use animation in Pure ECS? Any examples?

Discussion in 'DOTS Animation' started by zhuxianzhi, Sep 13, 2018.

  1. zhuxianzhi

    zhuxianzhi

    Joined:
    Mar 30, 2015
    Posts:
    122
    Thanks in advance
     
  2. NearAutomata

    NearAutomata

    Joined:
    May 23, 2018
    Posts:
    51
    At the moment the methods for the Animator class are not thread safe. For the time being I have a bandaid workaround until the devs release an official ECS component for the Animator:
    • AnimationTriggerComponent containing the integer hash for a trigger
    • AnimationTriggerSystem written in Hybrid ECS (Animator methods cannot be jobified YET) that executes SetTrigger for each entity that has a AnimationTriggerComponent
    Code (CSharp):
    1. [Serializable]
    2. public struct AnimationTrigger : IComponentData
    3. {
    4.     public int Value;
    5. }
    6.  
    7. public class AnimationTriggerSystem : ComponentSystem
    8. {
    9.     [UsedImplicitly] [Inject] private AnimationTriggerGroup _animatedEntities;
    10.  
    11.     protected override void OnUpdate()
    12.     {
    13.         for (var i = 0; i < _animatedEntities.Length; i++)
    14.         {
    15.             var animationTrigger = _animatedEntities.AnimationTriggers[i];
    16.             var animator = _animatedEntities.Animators[i];
    17.             var entity = _animatedEntities.Entities[i];
    18.  
    19.             PostUpdateCommands.RemoveComponent<AnimationTrigger>(entity);
    20.  
    21.             animator.SetTrigger(animationTrigger.Value);
    22.         }
    23.     }
    24.  
    25.     private struct AnimationTriggerGroup
    26.     {
    27.         [UsedImplicitly] [ReadOnly] public ComponentDataArray<AnimationTrigger> AnimationTriggers;
    28.         [UsedImplicitly] [ReadOnly] public ComponentArray<Animator> Animators;
    29.         [UsedImplicitly] [ReadOnly] public EntityArray Entities;
    30.         [UsedImplicitly] public readonly int Length;
    31.     }
    32. }
     
    twobob and PalmGroveSoftware like this.
  3. zhuxianzhi

    zhuxianzhi

    Joined:
    Mar 30, 2015
    Posts:
    122
    Thanks for the reply, I am just learning ECS, how to add Animator to an entity?


    Code (CSharp):
    1.      Entity entity = entityManager.CreateEntity(archetype);
    2.      entityManager.SetComponentData(entity,//Animator);
     
  4. NearAutomata

    NearAutomata

    Joined:
    May 23, 2018
    Posts:
    51
    Since Animator ist not an ECS component, you have to add it to the underlying GameObject as usual. Either via inspector or in code.
     
  5. PalmGroveSoftware

    PalmGroveSoftware

    Joined:
    Mar 24, 2014
    Posts:
    17
    Thank you, new to ECS, this helps a lot to see a bit more clearly..