Created
May 11, 2021 17:10
-
-
Save JohnnyTurbo/4c87e4a20ab28da2957709b05106e045 to your computer and use it in GitHub Desktop.
Singletons in Unity ECS - Code from tutorial video: https://youtu.be/oSj6w8AeeAg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Unity.Entities; | |
| namespace TMG.ECS_Singletons | |
| { | |
| public class ApplyRadiationSystem : SystemBase | |
| { | |
| private EndSimulationEntityCommandBufferSystem _endSimulationEntityCommandBufferSystem; | |
| protected override void OnCreate() | |
| { | |
| RequireSingletonForUpdate<RadiationTag>(); | |
| _endSimulationEntityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>(); | |
| } | |
| protected override void OnUpdate() | |
| { | |
| var ecb = _endSimulationEntityCommandBufferSystem.CreateCommandBuffer().AsParallelWriter(); | |
| var deltaTime = Time.DeltaTime; | |
| Entities.ForEach((Entity e, int entityInQueryIndex, ref TimeToLiveData timeToLive) => | |
| { | |
| timeToLive.Value -= deltaTime; | |
| if (timeToLive.Value <= 0) | |
| { | |
| ecb.DestroyEntity(entityInQueryIndex, e); | |
| } | |
| }).ScheduleParallel(); | |
| Dependency.Complete(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Unity.Entities; | |
| namespace TMG.ECS_Singletons | |
| { | |
| [GenerateAuthoringComponent] | |
| public struct RadiationTag : IComponentData {} | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Unity.Entities; | |
| using Unity.Mathematics; | |
| using UnityEngine; | |
| using Random = Unity.Mathematics.Random; | |
| namespace TMG.ECS_Singletons | |
| { | |
| public struct SpawnCapsuleData : IComponentData | |
| { | |
| public float SpawnInterval; | |
| public float SpawnTimer; | |
| public Entity EntityPrefab; | |
| public KeyCode FunKey; | |
| public float3 MinSpawnPosition; | |
| public float3 MaxSpawnPosition; | |
| public Random Random; | |
| public float3 RandomSpawnPos => Random.NextFloat3(MinSpawnPosition, MaxSpawnPosition); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Unity.Entities; | |
| using Unity.Mathematics; | |
| using Unity.Transforms; | |
| using UnityEngine; | |
| using Random = Unity.Mathematics.Random; | |
| namespace TMG.ECS_Singletons | |
| { | |
| public class SpawnCapsuleSystem : SystemBase | |
| { | |
| private SpawnCapsuleData _spawnCapsuleData; | |
| protected override void OnStartRunning() | |
| { | |
| _spawnCapsuleData = GetSingleton<SpawnCapsuleData>(); | |
| } | |
| protected override void OnUpdate() | |
| { | |
| var spawnTimer = _spawnCapsuleData.SpawnTimer -= Time.DeltaTime; | |
| if (spawnTimer <= 0) | |
| { | |
| var newEntity = EntityManager.Instantiate(_spawnCapsuleData.EntityPrefab); | |
| var newTranslation = new Translation() | |
| { | |
| Value = _spawnCapsuleData.RandomSpawnPos | |
| }; | |
| EntityManager.SetComponentData(newEntity, newTranslation); | |
| _spawnCapsuleData.SpawnTimer = _spawnCapsuleData.SpawnInterval; | |
| } | |
| if (Input.GetKeyDown(_spawnCapsuleData.FunKey)) | |
| { | |
| var newSpawnData = new SpawnCapsuleData() | |
| { | |
| EntityPrefab = _spawnCapsuleData.EntityPrefab, | |
| FunKey = _spawnCapsuleData.FunKey, | |
| MinSpawnPosition = new float3(12.5f, 0.8f, 12.5f), | |
| MaxSpawnPosition = new float3(12.5f, 0.8f, 12.5f), | |
| Random = Random.CreateFromIndex(1), | |
| SpawnInterval = 0.01f, | |
| SpawnTimer = 0.01f, | |
| }; | |
| SetSingleton(newSpawnData); | |
| _spawnCapsuleData = GetSingleton<SpawnCapsuleData>(); | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Unity.Entities; | |
| namespace TMG.ECS_Singletons | |
| { | |
| [GenerateAuthoringComponent] | |
| public struct TimeToLiveData : IComponentData | |
| { | |
| public float Value; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment