fabriziospadaro / SpriteSheetRenderer

Spritesheet renderer is a powerful Unity ECS API to render massive numbers of sprites using the new dots stack, taking full advantage of Jobs, DynamicBuffers and ComputeBuffer

Home Page:https://www.linkedin.com/in/fabrizio-spadaro/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[question] is it possible to change spritesheet/material through a system ?

G3z opened this issue · comments

I wanted my entity to change animation so i regestered 2 spritesheet materials for the two animations

spriteSheets = new SpriteSheetMaterial[spriteNames.Length];

            for (int s = 0; s < spriteNames.Length; s++)
            {

                var name = spriteNames[s];

                SpriteSheetManager.RecordSpriteSheet(spriteList[s].sprites, name, spriteList[s].sprites.Length);

                spriteSheets[s] = new SpriteSheetMaterial { material = SpriteSheetCache.GetMaterial(name) };
            }

then i assigned the first to my entities (along with the standard stuff you documented in the readme)

eManager.SetSharedComponentData(e, spriteSheets[0]);

and I was tryng to swap material in a system to change the animation

public class UnitChangeMaterialSystem : SystemBase
{

    protected override void OnUpdate()
    {
        var elapsed = Time.ElapsedTime;

        SpriteSheetMaterial material = new SpriteSheetMaterial { material = SpriteSheetCache.GetMaterial("idle se") };

        Entities.WithAll<SpriteSheetMaterial>().ForEach((Entity entity, BufferHook bufferHook, ref UnitOrientationComponent orientation) =>
        {
            if (elapsed > 5 && orientation.Value == Orientation.NE )
            {

                orientation.Value = Orientation.SE;
                EntityManager.SetComponentData(entity, new BufferHook { bufferID = 10 + bufferHook.bufferID, bufferEnityID = DynamicBufferManager.GetEntityBufferID(material) });
                EntityManager.SetComponentData(entity, new SpriteIndex { Value = 10 + bufferHook.bufferID });
                EntityManager.SetComponentData(entity, new SpriteSheetAnimation { maxSprites = SpriteSheetCache.GetLength("idle se"), play = true, repetition = SpriteSheetAnimation.RepetitionType.Loop, samples = 10 });
                EntityManager.SetSharedComponentData(entity, material);


            }

        })
            .WithStructuralChanges()
            .WithoutBurst()
            .Run();

    }
}

what am I doing wrong ? Excuse the noob question but I'm a week into Unity and not much is clear