How to make 2D animation on Unity ECS?

Hi, everyone! I have a prefab contaning a regular Animator for Sprite Renderer. I want to be capable of set the new states for the animation in certains moment, pretty basic in regular Unity.

However, I am using ECS. Animators aren’t converting to ComponentData. So, I use Animator as companion components (my baker adding to entity using AddComponentObject and my System get it over GetComponentObject).

The animator comes up, but none of the method works, such as SetBool. None error happens too.

I read a couple of posts on internet and none of them works. Moreover, it seems all written about have at least 3 years old… According to ChatGPT, Unity haven’t ported Animation to ECS so far (I don’t know if it’s true).

Can someone help me?

Some code to support you to support me:

My Baker

    class CurveBaker : Baker<Curve>
    {
        public override void Bake(Curve authoring)
        {
            var entity = GetEntity(TransformUsageFlags.Dynamic);
            AddComponent(entity, new CurveComponent
            {
                totalCircleAngle = authoring.totalCircleAngle,
                startRotationFrom = authoring.startRotationFrom,
                trackSense = authoring.trackSense,
                spentRotation = 0,
            });
            AddComponentObject(entity, authoring.GetComponent<Animator>());
        }
    }

Instantiate the entity from prefab through SystemBase:

                Entity prefab = this.GetPrefab(newPiece, _prefabs);
Entity instantiatedEntity = EntityManager.Instantiate(prefab);

Interacting with animation in some place of SystemBase.OnUpdate

                                var animator = EntityManager.GetComponentObject<Animator>(entity);
animator.SetBool("gotoHalf2", true);

I suspect that this isn’t working because your instantiating with EntityManager so there is no Gameobject instance that has an Animator. The entity is probably referencing the animator on your prefab so there are no errors, but also no animations.

It might work if you put your prefab into a subscene directly without instantiation, but it will animate the gameobject, not the entity.

If your not doing hybrid approach and want the entity to animate you probably need something custom that swaps the sprite over time in the ComponentData version of your SpriteRenderer.

Yeah, I had tried putting the prefab directly on sub-scene and it works indeed.

But I can’t do that approach. I have to create and manage such created entities in runtime.

Is there a way to achieve that in ECS way? Is there a component data that replaces regular Animator?

If you cook up a simple reproduction of your project, we can inspect it ourselves and look for potential issues. That way would be much more helpful than trying to speculate any issue via your description only.

From what I have found there is no existing components for 2D entity animation. There only seems to be some asset store packages for 3D skinned mesh animation.

The only way I can think of to achieve that in a pure ECS would be to turn your animations into sprite sheets and create a system that swaps the entity sprite every frame. Should be relatively easy to get the basics working, but if you need some more advanced features of Animator like events it could become challenging. There is this tutorial that is pretty outdated but might help you get started.

1 Like

It was my conclusion too. There is no built-in way to do it right now (in 2024 at least)

My case was a quite simple animation with just 2 frames (actually, the use of animation was a workaround for other issue, but also it was good as an animation laboratory). I’ve achived it compounding SpriteRenderer to componentData and setting up the sprites during the SystemBase (loading the images using the regular Resource.Load). It works. It might be simple create something more sophisticated from here.

Thanks for your answer. Knowing other point of view is essencial to join the pieces of the puzzle.

1 Like