Last active
November 6, 2024 18:55
-
-
Save sidsbrmnn/4c1b96f07f036a1b6569309fb0d67d6f to your computer and use it in GitHub Desktop.
Usage of Additional Entities in Unity DOTS
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.Burst; | |
| using Unity.Entities; | |
| using Unity.Transforms; | |
| [UpdateAfter(typeof(TimerSystem))] | |
| public partial struct SpawnerSystem : ISystem | |
| { | |
| [BurstCompile] | |
| public void OnCreate(ref SystemState state) | |
| { | |
| state.RequireForUpdate<BeginSimulationEntityCommandBufferSystem.Singleton>(); | |
| } | |
| [BurstCompile] | |
| public void OnUpdate(ref SystemState state) | |
| { | |
| var ecbSingleton = SystemAPI.GetSingleton<BeginSimulationEntityCommandBufferSystem.Singleton>(); | |
| var ecb = ecbSingleton.CreateCommandBuffer(state.WorldUnmanaged); | |
| new SpawnJob { ECB = ecb }.Schedule(); | |
| } | |
| [BurstCompile] | |
| private partial struct SpawnJob : IJobEntity | |
| { | |
| public EntityCommandBuffer ECB; | |
| public void Execute(in LocalTransform transform, in Spawner spawner, ref Timer timer) | |
| { | |
| if (timer.Value > 0) | |
| return; | |
| timer.Value = spawner.Interval; | |
| var entity = ECB.Instantiate(spawner.Prefab); | |
| ECB.SetComponent(entity, LocalTransform.FromPosition(transform.Position)); | |
| } | |
| } | |
| } |
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.Burst; | |
| using Unity.Entities; | |
| public struct Timer : IComponentData | |
| { | |
| public float Value; | |
| } | |
| public partial struct TimerSystem : ISystem | |
| { | |
| [BurstCompile] | |
| public void OnUpdate(ref SystemState state) | |
| { | |
| var deltaTime = SystemAPI.Time.DeltaTime; | |
| foreach (var timer in SystemAPI.Query<RefRW<Timer>>()) | |
| { | |
| timer.ValueRW.Value = timer.ValueRO.Value - deltaTime; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment