Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created April 25, 2018 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/776470e2067ff6e542d884853c4e55f3 to your computer and use it in GitHub Desktop.
Save tsubaki/776470e2067ff6e542d884853c4e55f3 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;
using System.Linq;
public class AnyComponent : MonoBehaviour
{
[SerializeField] Transform[] targets;
[SerializeField] Transform[] players;
void Update()
{
var targetPositions = new NativeArray<Vector2>(
targets.Select(c => (Vector2)c.position).ToArray(), Allocator.Temp);
var handles = new NativeArray<JobHandle>(2, Allocator.Temp);
var job1 = new HitCheckJob(){
playerPosition = players[0].position,
isHit = new NativeArray<int>(1, Allocator.TempJob),
source = targetPositions,
};
var job2 = new HitCheckJob(){
playerPosition = players[1].position,
isHit = new NativeArray<int>(1, Allocator.TempJob),
source = targetPositions,
};
handles[0] = job1.Schedule();
//handles[1] = job2.Schedule();
handles[1] = job2.Schedule(handles[0]);
JobHandle.CompleteAll(handles);
handles.Dispose();
job1.isHit.Dispose();
job2.isHit.Dispose();
targetPositions.Dispose();
}
struct HitCheckJob : IJob
{
public Vector2 playerPosition;
public NativeArray<Vector2> source;
[WriteOnly] public NativeArray<int> isHit;
public void Execute()
{
var length = source.Length;
for(int i=0; i<length; i++){
var distance = Vector2.Distance(playerPosition, source[i]);
if( distance < 1){
isHit[0] = 1;
return;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment