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(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 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 }); }