public static void SetAnimation(EntityCommandBuffer commandBuffer, Entity e, SpriteSheetAnimationData animation, BufferHook hook)
    {
        Material oldMaterial  = DynamicBufferManager.GetMaterial(hook.bufferEnityID);
        string   oldAnimation = SpriteSheetCache.GetMaterialName(oldMaterial);

        if (animation.animationName != oldAnimation)
        {
            Material material            = SpriteSheetCache.GetMaterial(animation.animationName);
            var      spriteSheetMaterial = new SpriteSheetMaterial {
                material = material
            };

            //clean old buffer
            DynamicBufferManager.RemoveBuffer(oldMaterial, hook.bufferID);

            //use new buffer
            int        bufferID = DynamicBufferManager.AddDynamicBuffers(DynamicBufferManager.GetEntityBuffer(material), material);
            BufferHook bh       = new BufferHook {
                bufferID = bufferID, bufferEnityID = DynamicBufferManager.GetEntityBufferID(spriteSheetMaterial)
            };

            commandBuffer.SetSharedComponent(e, spriteSheetMaterial);
            commandBuffer.SetComponent(e, bh);
        }
        commandBuffer.SetComponent(e, new SpriteSheetAnimation {
            maxSprites = animation.sprites.Length, play = animation.playOnStart, samples = animation.samples, repetition = animation.repetition, elapsedFrames = 0
        });
        commandBuffer.SetComponent(e, new SpriteIndex {
            Value = animation.startIndex
        });
    }
    public static Entity Instantiate(EntityArchetype archetype, SpriteSheetAnimator animator)
    {
        Entity e = EntityManager.CreateEntity(archetype);

        animator.currentAnimationIndex = animator.defaultAnimationIndex;
        SpriteSheetAnimationData startAnim = animator.animations[animator.defaultAnimationIndex];
        int      maxSprites = startAnim.sprites.Length;
        Material material   = SpriteSheetCache.GetMaterial(animator.animations[animator.defaultAnimationIndex].animationName);
        int      bufferID   = DynamicBufferManager.AddDynamicBuffers(DynamicBufferManager.GetEntityBuffer(material), material);

        var spriteSheetMaterial = new SpriteSheetMaterial {
            material = material
        };
        BufferHook bh = new BufferHook {
            bufferID = bufferID, bufferEnityID = DynamicBufferManager.GetEntityBufferID(spriteSheetMaterial)
        };

        EntityManager.SetComponentData(e, bh);
        EntityManager.SetComponentData(e, new SpriteSheetAnimation {
            maxSprites = maxSprites, play = startAnim.playOnStart, samples = startAnim.samples, repetition = startAnim.repetition
        });
        EntityManager.SetComponentData(e, new SpriteIndex {
            Value = startAnim.startIndex
        });
        EntityManager.SetSharedComponentData(e, spriteSheetMaterial);
        animator.managedEntity = e;
        SpriteSheetCache.entityAnimator.Add(e, animator);
        return(e);
    }
Пример #3
0
    public static void SetAnimation(Entity e, SpriteSheetAnimationData animation)
    {
        int      bufferEnityID = EntityManager.GetComponentData <BufferHook>(e).bufferEnityID;
        int      bufferID      = EntityManager.GetComponentData <BufferHook>(e).bufferID;
        Material oldMaterial   = DynamicBufferManager.GetMaterial(bufferEnityID);
        string   oldAnimation  = SpriteSheetCache.GetMaterialName(oldMaterial);

        if (animation.animationName != oldAnimation)
        {
            Material material            = SpriteSheetCache.GetMaterial(animation.animationName);
            var      spriteSheetMaterial = new SpriteSheetMaterial {
                material = material
            };

            DynamicBufferManager.RemoveBuffer(oldMaterial, bufferID);

            //use new buffer
            bufferID = DynamicBufferManager.AddDynamicBuffers(DynamicBufferManager.GetEntityBuffer(material), material);
            BufferHook bh = new BufferHook {
                bufferID = bufferID, bufferEnityID = DynamicBufferManager.GetEntityBufferID(spriteSheetMaterial)
            };

            EntityManager.SetSharedComponentData(e, spriteSheetMaterial);
            EntityManager.SetComponentData(e, bh);
        }
        EntityManager.SetComponentData(e, new SpriteSheetAnimation {
            maxSprites = animation.sprites.Length, play = animation.playOnStart, samples = animation.samples, repetition = animation.repetition, elapsedFrames = 0
        });
        EntityManager.SetComponentData(e, new SpriteIndex {
            Value = animation.startIndex
        });
        MarkDirty <SpriteSheetColor>(e);
        MarkDirty <SpriteIndex>(e);
        MarkDirty <SpriteMatrix>(e);
    }
    public static Entity Instantiate(EntityArchetype archetype, string spriteSheetName)
    {
        Entity   e        = EntityManager.CreateEntity(archetype);
        Material material = SpriteSheetCache.GetMaterial(spriteSheetName);
        int      bufferID = DynamicBufferManager.AddDynamicBuffers(DynamicBufferManager.GetEntityBuffer(material), material);

        var spriteSheetMaterial = new SpriteSheetMaterial {
            material = material
        };
        BufferHook bh = new BufferHook {
            bufferID = bufferID, bufferEnityID = DynamicBufferManager.GetEntityBufferID(spriteSheetMaterial)
        };

        EntityManager.SetComponentData(e, bh);
        EntityManager.SetSharedComponentData(e, spriteSheetMaterial);
        return(e);
    }
        public static Entity SpawnEntity(EntityCommandBuffer eManager, string materialName)
        {
            var e = eManager.CreateEntity(instance.archetype);
            //only needed for first tiem material baking
            Material material = SpriteSheetCache.materialNameMaterial[materialName];

            int    bufferID   = DynamicBufferManager.AddDynamicBuffers(DynamicBufferManager.GetEntityBuffer(material), material);
            Entity uvBuffer   = DynamicBufferManager.GetEntityBuffer(material);
            int    maxSprites = DynamicBufferManager.manager.GetBuffer <UvBuffer>(uvBuffer).Length;

            Rect   area = instance.GetSpawnArea();
            Random rand = new Random((uint)UnityEngine.Random.Range(0, int.MaxValue));

            eManager.SetComponent(e, new SpriteIndex {
                Value = 0
            });
            eManager.SetComponent(e, new Scale {
                Value = 20
            });
            eManager.SetComponent(e, new Position2D {
                Value = rand.NextFloat2(area.min, area.max)
            });
            eManager.SetComponent(e, new SpriteSheetAnimation {
                maxSprites = maxSprites, play = true, repetition = SpriteSheetAnimation.RepetitionType.Loop, samples = 10
            });
            var color            = UnityEngine.Random.ColorHSV(.15f, .75f);
            SpriteSheetColor col = new SpriteSheetColor {
                color = new float4(color.r, color.g, color.b, color.a)
            };

            eManager.SetComponent(e, col);
            var spriteSheetMaterial = new SpriteSheetMaterial {
                material = material
            };

            eManager.SetComponent(e, new BufferHook {
                bufferID = bufferID, bufferEnityID = DynamicBufferManager.GetEntityBufferID(spriteSheetMaterial)
            });
            eManager.SetSharedComponent(e, spriteSheetMaterial);
            return(e);
        }