Created
September 24, 2021 07:17
-
-
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
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.BatchedJobs | |
{ | |
[GenerateAuthoringComponent] | |
public struct DoubleSpeedTag : 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.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; | |
} | |
} | |
} | |
} | |
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.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