Skip to content

Instantly share code, notes, and snippets.

@JohnnyTurbo
Created September 24, 2021 07:17
Show Gist options
  • Save JohnnyTurbo/d439050aa458c0e077f64b69ba5923d8 to your computer and use it in GitHub Desktop.
Save JohnnyTurbo/d439050aa458c0e077f64b69ba5923d8 to your computer and use it in GitHub Desktop.
Code used in IJobEntityBatch tutorial video on the Turbo Makes Games YouTube Channel - https://youtu.be/nu_HyPuIBPA
using Unity.Entities;
namespace TMG.BatchedJobs
{
[GenerateAuthoringComponent]
public struct DoubleSpeedTag : IComponentData {}
}
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
namespace TMG.BatchedJobs
{
public class MovementSystem : SystemBase
{
private EntityQuery _entityQuery;
protected override void OnCreate()
{
_entityQuery = GetEntityQuery(ComponentType.ReadWrite<Translation>(),
ComponentType.ReadOnly<OscillateMagnitudeData>());
}
protected override void OnUpdate()
{
var movementJob = new MovementJob
{
TranslationTypeHandle = GetComponentTypeHandle<Translation>(false),
MagnitudeTypeHandle = GetComponentTypeHandle<OscillateMagnitudeData>(true),
DoubleSpeedTypeHandle = GetComponentTypeHandle<DoubleSpeedTag>(true),
CurrentTime = (float) Time.ElapsedTime
};
Dependency = movementJob.ScheduleParallel(_entityQuery, 1, Dependency);
}
}
public struct MovementJob : IJobEntityBatch
{
public ComponentTypeHandle<Translation> TranslationTypeHandle;
[ReadOnly] public ComponentTypeHandle<OscillateMagnitudeData> MagnitudeTypeHandle;
[ReadOnly] public ComponentTypeHandle<DoubleSpeedTag> DoubleSpeedTypeHandle;
public float CurrentTime;
[BurstCompile]
public void Execute(ArchetypeChunk batchInChunk, int batchIndex)
{
var frequency = batchInChunk.Has(DoubleSpeedTypeHandle) ? 2f : 1f;
var translationArray = batchInChunk.GetNativeArray(TranslationTypeHandle);
var magnitudeArray = batchInChunk.GetNativeArray(MagnitudeTypeHandle);
for (var i = 0; i < batchInChunk.Count; i++)
{
var magnitude = magnitudeArray[i].Value;
var translation = translationArray[i];
translation.Value.y = magnitude * math.sin(CurrentTime * frequency + magnitude + 1f);
translationArray[i] = translation;
}
}
}
}
using Unity.Entities;
namespace TMG.BatchedJobs
{
[GenerateAuthoringComponent]
public struct OscillateMagnitudeData : IComponentData
{
public float Value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment