forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindNearest.cs
More file actions
61 lines (52 loc) · 2.06 KB
/
Copy pathFindNearest.cs
File metadata and controls
61 lines (52 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;
namespace Step3
{
public class FindNearest : MonoBehaviour
{
public NativeArray<float3> TargetPositions;
public NativeArray<float3> SeekerPositions;
public NativeArray<float3> NearestTargetPositions;
public void Awake()
{
Spawner spawner = Object.FindObjectOfType<Spawner>();
TargetPositions = new NativeArray<float3>(spawner.NumTargets, Allocator.Persistent);
SeekerPositions = new NativeArray<float3>(spawner.NumSeekers, Allocator.Persistent);
NearestTargetPositions = new NativeArray<float3>(spawner.NumSeekers, Allocator.Persistent);
}
public void OnDestroy()
{
TargetPositions.Dispose();
SeekerPositions.Dispose();
NearestTargetPositions.Dispose();
}
public void Update()
{
for (int i = 0; i < TargetPositions.Length; i++)
{
TargetPositions[i] = Spawner.TargetTransforms[i].localPosition;
}
for (int i = 0; i < SeekerPositions.Length; i++)
{
SeekerPositions[i] = Spawner.SeekerTransforms[i].localPosition;
}
FindNearestJob findJob = new FindNearestJob
{
TargetPositions = TargetPositions,
SeekerPositions = SeekerPositions,
NearestTargetPositions = NearestTargetPositions,
};
// Execute will be called once for every element of the SeekerPositions array,
// with every index from 0 up to (but not including) the length of the array.
// The Execute calls will be split into batches of 64.
JobHandle findHandle = findJob.Schedule(SeekerPositions.Length, 64);
findHandle.Complete();
for (int i = 0; i < SeekerPositions.Length; i++)
{
Debug.DrawLine(SeekerPositions[i], NearestTargetPositions[i]);
}
}
}
}