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); }
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 void DestroyEntity(Entity e, string materialName) { Material material = SpriteSheetCache.GetMaterial(materialName); int bufferID = EntityManager.GetComponentData <BufferHook>(e).bufferID; DynamicBufferManager.RemoveBuffer(material, bufferID); EntityManager.DestroyEntity(e); }
public void Convert(Entity entity, EntityManager eManager, GameObjectConversionSystem conversionSystem) { EntityArchetype archetype = eManager.CreateArchetype( typeof(Position2D), typeof(Rotation2D), typeof(Scale), //required params typeof(SpriteIndex), typeof(SpriteSheetAnimation), typeof(SpriteSheetMaterial), typeof(SpriteSheetColor), typeof(SpriteMatrix), typeof(BufferHook) ); NativeArray <Entity> entities = new NativeArray <Entity>(spriteCount, Allocator.Temp); eManager.CreateEntity(archetype, entities); //only needed for the first time to bake the material and create the uv map SpriteSheetManager.RecordSpriteSheet(sprites, "emoji", entities.Length); Rect area = GetSpawnArea(); Random rand = new Random((uint)UnityEngine.Random.Range(0, int.MaxValue)); int cellCount = SpriteSheetCache.GetLength("emoji"); SpriteSheetMaterial material = new SpriteSheetMaterial { material = SpriteSheetCache.GetMaterial("emoji") }; for (int i = 0; i < entities.Length; i++) { Entity e = entities[i]; eManager.SetComponentData(e, new SpriteIndex { Value = rand.NextInt(0, cellCount) }); eManager.SetComponentData(e, new Scale { Value = 10 }); eManager.SetComponentData(e, new Position2D { Value = rand.NextFloat2(area.min, area.max) }); eManager.SetComponentData(e, new SpriteSheetAnimation { maxSprites = cellCount, 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.SetComponentData(e, col); eManager.SetComponentData(e, new BufferHook { bufferID = i, bufferEnityID = DynamicBufferManager.GetEntityBufferID(material) }); eManager.SetSharedComponentData(e, material); } }
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); }