Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Raycast発行(非同期)
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Experimental.AI;
public class RaycastCheck : MonoBehaviour
{
[SerializeField] Transform start, target;
private NavMeshQuery query;
private JobHandle handle;
private NativeArray<NavMeshHit> hits;
private NavMeshLocation location;
private void OnEnable()
{
query = new NavMeshQuery(NavMeshWorld.GetDefaultWorld(), Allocator.Persistent);
hits = new NativeArray<NavMeshHit>(1, Allocator.Persistent);
location = query.MapLocation(start.position, Vector3.up * 10, 0);
}
private void OnDisable()
{
handle.Complete();
hits.Dispose();
query.Dispose();
}
private void Update()
{
handle.Complete();
handle = new RayCastCheckJob()
{
query = query,
location = location,
hits = hits,
startPos = start.position,
targetPos = target.position
}.Schedule(handle);
handle = new LogJob()
{
hits = hits
}.Schedule(handle);
JobHandle.ScheduleBatchedJobs();
}
struct RayCastCheckJob : IJob
{
[ReadOnly]
public NavMeshQuery query;
[WriteOnly]
public NativeArray<NavMeshHit> hits;
public NavMeshLocation location;
public Vector3 startPos, targetPos;
public void Execute()
{
location = query.MoveLocation(location, startPos);
query.Raycast(out NavMeshHit hitcheck, location, targetPos);
hits[0] = hitcheck;
}
}
struct LogJob : IJob
{
[ReadOnly]
public NativeArray<NavMeshHit> hits;
public void Execute()
{
Debug.Log(hits[0].hit);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.