Created
February 12, 2019 15:28
-
-
Save tsubaki/ef2387e1b61c36dac351b37fa0527224 to your computer and use it in GitHub Desktop.
This file contains 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using Unity.Jobs; | |
using UnityEngine.Jobs; | |
using Unity.Collections; | |
using Unity.Mathematics; | |
public class IJobParallelForFilterTest : MonoBehaviour | |
{ | |
[SerializeField] Transform[] enemies; | |
[SerializeField] Transform player; | |
[SerializeField] ParticleSystem particle; | |
TransformAccessArray enemyTransforms; | |
NativeArray<int> coolTimes; | |
NativeQueue<float3> explCommands; | |
NativeList<int> indices; | |
NativeArray<float3> positions; | |
JobHandle handle; | |
private void OnEnable() | |
{ | |
enemyTransforms = new TransformAccessArray(enemies); | |
coolTimes = new NativeArray<int>(enemyTransforms.length, Allocator.Persistent); | |
explCommands = new NativeQueue<float3>(Allocator.Persistent); | |
indices = new NativeList<int>(Allocator.Persistent); | |
positions = new NativeArray<float3>(enemyTransforms.length, Allocator.Persistent); | |
} | |
private void OnDisable() | |
{ | |
handle.Complete(); | |
enemyTransforms.Dispose(); | |
coolTimes.Dispose(); | |
explCommands.Dispose(); | |
indices.Dispose(); | |
positions.Dispose(); | |
} | |
private void Update() | |
{ | |
handle.Complete(); | |
indices.Clear(); | |
HitEffect(); | |
handle = new UpdatePositionJob { | |
Position = positions | |
}.Schedule(enemyTransforms, handle); | |
handle = new ReduceCooltime | |
{ | |
CoolTimes = coolTimes | |
}.Schedule(coolTimes.Length, 4, handle); | |
handle = new HitcheckJob { | |
Position = positions, | |
playerPosition = player.position, | |
CoolTimes = coolTimes | |
}.ScheduleAppend(indices, enemyTransforms.length, 8, handle); | |
handle = new HiteffectJob { | |
Position = positions, | |
CoolTimes = coolTimes, | |
ExplCommands = explCommands.ToConcurrent() | |
}.ScheduleFilter(indices, 1, handle); | |
JobHandle.ScheduleBatchedJobs(); | |
} | |
void HitEffect() | |
{ | |
var emitter = new ParticleSystem.EmitParams(); | |
while (explCommands.TryDequeue(out float3 emitPos)) | |
{ | |
emitter.position = emitPos; | |
particle.Emit(emitter, 1); | |
} | |
} | |
} | |
struct UpdatePositionJob : IJobParallelForTransform | |
{ | |
[WriteOnly] public NativeArray<float3> Position; | |
public void Execute(int index, TransformAccess transform) | |
{ | |
Position[index] = transform.position; | |
} | |
} | |
struct ReduceCooltime : IJobParallelFor | |
{ | |
public NativeArray<int> CoolTimes; | |
public void Execute(int index) | |
{ | |
CoolTimes[index] = math.max(0, CoolTimes[index] - 1); | |
} | |
} | |
struct HitcheckJob : IJobParallelForFilter | |
{ | |
[ReadOnly] | |
public NativeArray<float3> Position; | |
[ReadOnly] | |
public NativeArray<int> CoolTimes; | |
[ReadOnly] | |
public float3 playerPosition; | |
public bool Execute(int index) | |
{ | |
return (CoolTimes[index] == 0) && | |
(math.distance(Position[index], playerPosition) < 1); | |
} | |
} | |
struct HiteffectJob : IJobParallelForFilter | |
{ | |
[ReadOnly] public NativeArray<float3> Position; | |
public NativeQueue<float3>.Concurrent ExplCommands; | |
public NativeArray<int> CoolTimes; | |
public bool Execute(int index) | |
{ | |
ExplCommands.Enqueue(Position[index]); | |
CoolTimes[index] = 30; | |
return true; | |
} | |
} |
Author
tsubaki
commented
Feb 12, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment