public static void SetAnimation(Entity e, SpriteSheetAnimationClip animation)
        {
            int      bufferEntityID = EntityManager.GetComponentData <BufferHook>(e).bufferEntityID;
            int      bufferID       = EntityManager.GetComponentData <BufferHook>(e).bufferID;
            Material oldMaterial    = DynamicBufferManager.GetMaterial(bufferEntityID);
            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, bufferEntityID = DynamicBufferManager.GetEntityBufferID(spriteSheetMaterial)
                };

                EntityManager.SetSharedComponentData(e, spriteSheetMaterial);
                EntityManager.SetComponentData(e, bh);
            }
            EntityManager.SetComponentData(e, new SpriteSheetAnimation {
                frameCount = animation.Sprites.Length, framesPerSecond = animation.FramesPerSecond, playMode = animation.PlayMode, elapsedTime = 0
            });
            EntityManager.SetComponentData(e, new SpriteIndex {
                Value = 0
            });
        }
        public static Entity Instantiate(EntityArchetype archetype, List <IComponentData> componentDatas, SpriteSheetAnimator animator)
        {
            Entity e = EntityManager.CreateEntity(archetype);
            SpriteSheetAnimationClip startAnim = animator.animations[animator.defaultAnimationClipIndex];
            int      maxSprites = startAnim.FrameCount;
            Material material   = SpriteSheetCache.GetMaterial(animator.animations[animator.defaultAnimationClipIndex].AnimationName);
            int      bufferID   = DynamicBufferManager.AddDynamicBuffers(DynamicBufferManager.GetEntityBuffer(material), material);

            foreach (IComponentData Idata in componentDatas)
            {
                EntityManager.SetComponentData(e, Idata);
            }

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

            EntityManager.SetComponentData(e, bh);
            EntityManager.SetComponentData(e, new SpriteSheetAnimation {
                frameCount = maxSprites, framesPerSecond = startAnim.FramesPerSecond, playMode = startAnim.PlayMode
            });
            EntityManager.SetComponentData(e, new SpriteIndex {
                Value = 0
            });
            EntityManager.SetSharedComponentData(e, spriteSheetMaterial);
            animator.managedEntity = e;
            SpriteSheetCache.AddAnimator(e, animator);
            return(e);
        }
        public static void SetAnimation(EntityCommandBuffer commandBuffer, Entity e, SpriteSheetAnimationClip animation, BufferHook hook)
        {
            Material oldMaterial  = DynamicBufferManager.GetMaterial(hook.bufferEntityID);
            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, bufferEntityID = DynamicBufferManager.GetEntityBufferID(spriteSheetMaterial)
                };

                commandBuffer.SetSharedComponent(e, spriteSheetMaterial);
                commandBuffer.SetComponent(e, bh);
            }
            commandBuffer.SetComponent(e, new SpriteSheetAnimation {
                frameCount = animation.FrameCount, framesPerSecond = animation.FramesPerSecond, playMode = animation.PlayMode, elapsedTime = 0
            });
            commandBuffer.SetComponent(e, new SpriteIndex {
                Value = 0
            });
        }
Пример #4
0
        public static void Play(EntityCommandBuffer buffer, Entity e, BufferHook hook, string animationName = null)
        {
            SpriteSheetAnimator animator = SpriteSheetCache.GetAnimator(e);

            if (string.IsNullOrEmpty(animationName))
            {
                animationName = animator.animations[animator.currentAnimationIndex].name;
            }
            for (int i = 0; i < animator.animations.Length; i++)
            {
                var animation = animator.animations[i];
                if (animation.AnimationName == animationName)
                {
                    SpriteSheetManager.SetAnimation(buffer, e, animation, hook);
                    animator.currentAnimationIndex = i;
                    return;
                }
            }
        }
        public static Entity Instantiate(EntityArchetype archetype, List <IComponentData> componentDatas, string spriteSheetName)
        {
            Entity   e        = EntityManager.CreateEntity(archetype);
            Material material = SpriteSheetCache.GetMaterial(spriteSheetName);
            int      bufferID = DynamicBufferManager.AddDynamicBuffers(DynamicBufferManager.GetEntityBuffer(material), material);

            foreach (IComponentData Idata in componentDatas)
            {
                EntityManager.SetComponentData(e, Idata);
            }

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

            EntityManager.SetComponentData(e, bh);
            EntityManager.SetSharedComponentData(e, spriteSheetMaterial);
            return(e);
        }
        public static void DestroyEntity(EntityCommandBuffer commandBuffer, Entity e, BufferHook hook)
        {
            commandBuffer.DestroyEntity(e);
            Material material = DynamicBufferManager.GetMaterial(hook.bufferEntityID);

            DynamicBufferManager.RemoveBuffer(material, hook.bufferID);
        }