using Unity.Collections; using Unity.Entities; using Unity.Mathematics; using Unity.Transforms; namespace Unity.TinyGems { public class PlayAreaSystem : SystemBase { private Scenes m_CurrentScene = Scenes.None; private EntityQuery m_BackgroundQuery; private EntityQuery m_CellQuery; protected override void OnCreate() { base.OnCreate(); RequireSingletonForUpdate(); RequireSingletonForUpdate(); m_BackgroundQuery = GetEntityQuery(ComponentType.ReadWrite()); m_CellQuery = GetEntityQuery(ComponentType.ReadWrite()); } protected override void OnUpdate() { var activeScene = GetSingleton(); if (activeScene.Value == m_CurrentScene) return; if (activeScene.Value == Scenes.Title) DestroyPlayArea(); else if(activeScene.Value == Scenes.Game) SpawnPlayArea(); m_CurrentScene = activeScene.Value; } private void DestroyPlayArea() { EntityManager.DestroyEntity(m_BackgroundQuery); EntityManager.DestroyEntity(m_CellQuery); } private void SpawnPlayArea() { var cmdBuffer = new EntityCommandBuffer(Allocator.TempJob); var cellManager = GetSingleton(); for (var x = 0; x < cellManager.MaxCol; x++) { for(var y = 0; y < cellManager.MaxRow; y++) { var backgroundEntity = cmdBuffer.Instantiate(cellManager.CellBackground); cmdBuffer.AddComponent(backgroundEntity); cmdBuffer.SetComponent(backgroundEntity, new Translation { Value = new float3(x, y, 0) }); } } cmdBuffer.Playback(EntityManager); cmdBuffer.Dispose(); } } }