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 }); }
//only use this when you didn't bake the uv yet public static void BakeUvBuffer(SpriteSheetMaterial spriteSheetMaterial, KeyValuePair <Material, float4[]> atlasData) { Entity entity = GetEntityBuffer(spriteSheetMaterial.material); var buffer = EntityManager.GetBuffer <UvBuffer>(entity); for (int j = 0; j < atlasData.Value.Length; j++) { buffer.Add(atlasData.Value[j]); } }
public static void RecordSpriteSheet(Sprite[] sprites, string spriteSheetName, int spriteCount = 0) { KeyValuePair <Material, float4[]> atlasData = SpriteSheetCache.BakeSprites(sprites, spriteSheetName); SpriteSheetMaterial material = new SpriteSheetMaterial { material = atlasData.Key }; DynamicBufferManager.GenerateBuffers(material, spriteCount); DynamicBufferManager.BakeUvBuffer(material, atlasData); renderInformation.Add(new RenderInformation(material.material, DynamicBufferManager.GetEntityBuffer(material.material))); }
public static void GenerateBuffers(SpriteSheetMaterial material, int entityCount = 0) { if (!materialEntityBufferID.ContainsKey(material.material)) { CreateBuffersContainer(material); availableEntityID.Add(material.material, new List <int>()); for (int i = 0; i < entityCount; i++) { availableEntityID[material.material].Add(i); } MassAddBuffers(bufferEntities.Last(), entityCount); } }
public static void RecordAnimator(SpriteSheetAnimator animator) { foreach (SpriteSheetAnimationClip animation in animator.animations) { KeyValuePair <Material, float4[]> atlasData = SpriteSheetCache.BakeSprites(animation.Sprites, animation.AnimationName); SpriteSheetMaterial material = new SpriteSheetMaterial { material = atlasData.Key }; DynamicBufferManager.GenerateBuffers(material); DynamicBufferManager.BakeUvBuffer(material, atlasData); renderInformation.Add(new RenderInformation(material.material, DynamicBufferManager.GetEntityBuffer(material.material))); } }
//use this when it's the first time you are using that material //use this just to generate the buffers container public static void CreateBuffersContainer(SpriteSheetMaterial material) { var archetype = EntityManager.CreateArchetype( typeof(SpriteIndexBuffer), typeof(MatrixBuffer), typeof(SpriteColorBuffer), typeof(SpriteSheetMaterial), typeof(UvBuffer) ); Entity e = EntityManager.CreateEntity(archetype); bufferEntities.Add(e); EntityManager.SetSharedComponentData(e, material); materialEntityBufferID.Add(material.material, materialEntityBufferID.Count); }
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 int GetEntityBufferID(SpriteSheetMaterial material) { return(materialEntityBufferID[material.material]); }
public static BufferHook GetBufferHook(SpriteSheetMaterial material) { return(new BufferHook { bufferEntityID = materialEntityBufferID[material.material], bufferID = NextIDForEntity(material.material) }); }