Access to AnimatorController

What the easiest way to change animator controller variables with ECS?

Use a ComponentSystem to access any monobehaviour based components

How to define monobehaviour based variables?

using Unity.Entities;
using UnityEngine;

public class AlertedAnimationSystem : ComponentSystem{
    EntityQuery anim_group;
    protected override void OnUpdate(){
        Entities.With(anim_group).ForEach((Animator animator, ref AnimatorData anim_trigger) =>{
            if(!animator.GetBool("alerted"))
                animator.SetBool("alerted",true);
        });
    }

    protected override void OnCreate(){
        anim_group = GetEntityQuery(typeof(AnimatorData),typeof(Animator),typeof(Alerted),ComponentType.Exclude<DeadUnit>());
        anim_group.SetFilterChanged(typeof(Alerted));
    }
}
4 Likes

Thanks

One performance hint I’ve gathered from watching videos on performance optimization with animators is that you should call StringToHash on the key that you access your animator with.

Notice in the code above how I’ve added the int kAlerted, I cache a reference to the hash of the string on create and I only access the animator by hash in the main loop.

From everything I understand, the string compares in accessing animators is SLOW while the hashed int is optimal.

The rest of the example is fantastic though! Thank you :slight_smile:

1 Like