Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
}
@tsubaki
Copy link
Author

tsubaki commented Feb 12, 2019

55

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment