public void Convert(Entity entity, EntityManager eManager, GameObjectConversionSystem conversionSystem) { instance = this; 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 first tiem material baking KeyValuePair <Material, float4[]> atlasData = SpriteSheetCache.BakeSprites(sprites, "emoji"); SpriteSheetMaterial material = new SpriteSheetMaterial { material = atlasData.Key }; DynamicBufferManager.manager = eManager; DynamicBufferManager.GenerateBuffers(material, entities.Length); DynamicBufferManager.BakeUvBuffer(material, atlasData); Rect area = GetSpawnArea(); Random rand = new Random((uint)UnityEngine.Random.Range(0, int.MaxValue)); int cellCount = atlasData.Value.Length; 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 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, spriteSheetName, entities.Length); Rect area = GetSpawnArea(); Random rand = new Random((uint)UnityEngine.Random.Range(0, int.MaxValue)); int cellCount = SpriteSheetCache.GetLength(spriteSheetName); SpriteSheetMaterial material = new SpriteSheetMaterial { material = SpriteSheetCache.GetMaterial(spriteSheetName) }; 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 { frameCount = cellCount, playMode = PlayMode.Loop, framesPerSecond = 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, bufferEntityID = DynamicBufferManager.GetEntityBufferID(material) }); eManager.SetSharedComponentData(e, material); } }
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); }
public void Convert(Entity entity, EntityManager eManager, GameObjectConversionSystem conversionSystem) { var archetype = eManager.CreateArchetype( typeof(Position2D), typeof(Rotation2D), typeof(Scale), //typeof(Bound2D), typeof(SpriteSheet), typeof(SpriteSheetAnimation), typeof(SpriteSheetMaterial), typeof(UvBuffer), typeof(SpriteSheetColor), typeof(RenderData) ); NativeArray <Entity> entities = new NativeArray <Entity>(spriteCount, Allocator.Temp); eManager.CreateEntity(archetype, entities); KeyValuePair <Material, float4[]> atlasData = SpriteSheetCache.BakeSprites(sprites); int cellCount = atlasData.Value.Length; Random rand = new Random((uint)UnityEngine.Random.Range(0, int.MaxValue)); Rect area = GetSpawnArea(); SpriteSheetMaterial material = new SpriteSheetMaterial { material = atlasData.Key }; for (int i = 0; i < entities.Length; i++) { Entity e = entities[i]; SpriteSheet sheet = new SpriteSheet { spriteIndex = rand.NextInt(0, cellCount), maxSprites = cellCount }; Scale scale = new Scale { Value = rand.NextFloat(minScale, maxScale) }; Position2D pos = new Position2D { Value = rand.NextFloat2(area.min, area.max) }; SpriteSheetAnimation anim = new SpriteSheetAnimation { play = true, repetition = SpriteSheetAnimation.RepetitionType.Loop, samples = 10 }; var color = UnityEngine.Random.ColorHSV(.15f, .75f); SpriteSheetColor col = new SpriteSheetColor { value = new float4(color.r, color.g, color.b, color.a) }; eManager.SetComponentData(e, sheet); eManager.SetComponentData(e, scale); eManager.SetComponentData(e, pos); eManager.SetComponentData(e, anim); eManager.SetComponentData(e, col); eManager.SetSharedComponentData(e, material); // Fill uv buffer var buffer = eManager.GetBuffer <UvBuffer>(entities[i]); for (int j = 0; j < atlasData.Value.Length; j++) { buffer.Add(atlasData.Value[j]); } } }
void DoSpawn(SpriteSpawnerTest.SpawnData spawner, Material mat) { ComponentType[] type = new ComponentType[] { typeof(Position2D), typeof(Rotation2D), typeof(Scale), typeof(SpriteSheetAnimation), typeof(SpriteSheetMaterial), typeof(SpriteSheetColor), typeof(UVCell) }; var em = EntityManager; var archetype = em.CreateArchetype(type); NativeArray <Entity> entities = new NativeArray <Entity>(spawner.spriteCount, Allocator.Temp); em.CreateEntity(archetype, entities); int cellCount = CachedUVData.GetCellCount(mat); Random rand = new Random((uint)UnityEngine.Random.Range(0, int.MaxValue)); Rect area = spawner.GetSpawnArea(); SpriteSheetMaterial material = new SpriteSheetMaterial { material = mat }; var maxFrames = CachedUVData.GetCellCount(mat); //Debug.Log("Spawning entities and setting components"); for (int i = 0; i < entities.Length; i++) { Entity e = entities[i]; Scale scale = new Scale { Value = rand.NextFloat(spawner.minScale, spawner.maxScale) }; float2 p = rand.NextFloat2(area.min, area.max); Position2D pos = spawner.origin.xy + p; Rotation2D rot = new Rotation2D { angle = spawner.rotation }; int numFrames = rand.NextInt(3, maxFrames / 2); int minFrame = rand.NextInt(0, maxFrames - numFrames); SpriteSheetAnimation anim = new SpriteSheetAnimation { play = true, repetition = SpriteSheetAnimation.RepetitionType.Loop, fps = rand.NextFloat(spawner.minFPS_, spawner.maxFPS_), frameMin = minFrame, frameMax = minFrame + numFrames }; SpriteSheetColor color = UnityEngine.Random.ColorHSV(.35f, .75f); UVCell cell = new UVCell { value = rand.NextInt(0, maxFrames) }; em.SetComponentData(e, scale); em.SetComponentData(e, pos); em.SetComponentData(e, anim); em.SetComponentData(e, color); em.SetComponentData(e, cell); em.SetComponentData(e, rot); em.SetSharedComponentData(e, material); } }