Created
April 25, 2018 15:36
-
-
Save tsubaki/af86f8891780dd4c692e04b7b1352165 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 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 playerPositions = new NativeArray<Vector2>( | |
players.Select(c => (Vector2)c.position).ToArray(), Allocator.Temp); | |
var job = new HitCheckJob(){ | |
playersPositions = playerPositions, | |
source = targetPositions, | |
isHit = new NativeArray<int>(players.Length, Allocator.TempJob), | |
}; | |
var handle = job.Schedule(players.Length, 2); | |
handle.Complete(); | |
Debug.Log( job.isHit[0] ); | |
job.isHit.Dispose(); | |
targetPositions.Dispose(); | |
playerPositions.Dispose(); | |
} | |
struct HitCheckJob : IJobParallelFor | |
{ | |
[ReadOnly] public NativeArray<Vector2> playersPositions; | |
// public NativeArray<Vector2> source; | |
[ReadOnly] public NativeArray<Vector2> source; // index外にアクセスするものはReadOnlyを設定 | |
[WriteOnly] public NativeArray<int> isHit; | |
public void Execute(int index) | |
{ | |
var length = source.Length; | |
var playerPosition = playersPositions[index]; | |
for(int i=0; i<length; i++){ | |
var distance = Vector2.Distance(playerPosition, source[i]); | |
if( distance < 1){ | |
isHit[index] = 1; | |
return; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment