Game.Pathfind.NativePathfindData
Assembly: Assembly-CSharp
Namespace: Game.Pathfind
Type: struct (NativeContainer, GenerateTestsForBurstCompatibility)
Base: System.ValueType, implements System.IDisposable
Summary:
NativePathfindData is a low-level native container wrapper around an UnsafePathfindData pointer. It manages allocation and lifetime of the underlying pathfinding data used by the game's pathfinding system (edges, free id lists, owner-to-edge maps, etc.). The type exposes methods that forward to the underlying UnsafePathfindData for creating/updating/destroying edges, querying and mutating edge properties, and retrieving memory statistics. Because it stores an unsafe pointer and performs manual allocations via UnsafeUtility, it is intended for advanced usage and must be explicitly disposed to avoid native memory leaks. The struct is annotated with NativeContainer and GenerateTestsForBurstCompatibility and uses NativeDisableUnsafePtrRestriction for its pointer field.
Fields
-
internal unsafe UnsafePathfindData* m_PathfindData
Pointer to the allocated UnsafePathfindData instance. This is the backing storage for all pathfind structures. Null when the container is not created. All public operations are forwarded to this pointer (unsafe access). -
internal Allocator m_AllocatorLabel
Allocator label used when allocating the UnsafePathfindData with UnsafeUtility.Malloc. Used on Dispose to free with the correct allocator.
Properties
-
public unsafe bool IsCreated => m_PathfindData != null
True when the native container has been allocated (m_PathfindData is non-null). Use to check if the container is valid before calling other methods. -
public unsafe int Size => m_PathfindData->m_Edges.Length - m_PathfindData->m_FreeIDs.Length
Returns the current number of active edges (total edges minus free/available IDs). Computed from the underlying UnsafePathfindData arrays.
Constructors
-
public NativePathfindData(Allocator allocator)
Public constructor that allocates and initializes the underlying UnsafePathfindData using the provided allocator. Delegates to the private constructor with a default sentinel depth. -
private unsafe NativePathfindData(Allocator allocator, int disposeSentinelStackDepth)
Allocates native memory for UnsafePathfindData using UnsafeUtility.Malloc with the given allocator and alignment/size derived from UnsafeUtility. Initializes the pointed UnsafePathfindData via new UnsafePathfindData(allocator). The disposeSentinelStackDepth parameter is present in signature (likely for native-container disposal checks) but not used beyond selecting this overload.
Methods
-
private void CheckRead()
Conditional method compiled only when ENABLE_UNITY_COLLECTIONS_CHECKS is defined. Intended for read-safety checks; no-op when checks are disabled. -
private void CheckWrite()
Conditional method compiled only when ENABLE_UNITY_COLLECTIONS_CHECKS is defined. Intended for write-safety checks; no-op when checks are disabled. -
public unsafe void Dispose()
Disposes the underlying UnsafePathfindData (calls its Dispose), frees the allocated native memory via UnsafeUtility.Free using m_AllocatorLabel, and sets m_PathfindData to null. Must be called to avoid native memory leaks. -
public unsafe void Clear()
Forwards to UnsafePathfindData.Clear(), clearing stored edges and related structures but keeping the container allocated. -
public unsafe void GetMemoryStats(out uint used, out uint allocated)
Forwards to UnsafePathfindData.GetMemoryStats to obtain memory usage/allocated bytes for the underlying data. -
public unsafe EdgeID CreateEdge(PathNode startNode, PathNode middleNode, PathNode endNode, PathSpecification specification, LocationSpecification location)
Creates a new edge in the underlying data and returns its EdgeID. -
public unsafe void UpdateEdge(EdgeID edgeID, PathNode startNode, PathNode middleNode, PathNode endNode, PathSpecification specification, LocationSpecification location)
Updates an existing edge with new node positions/specification/location. -
public unsafe void DestroyEdge(EdgeID edgeID)
Removes/destroys an edge by EdgeID (for reuse of IDs). -
public unsafe void AddEdge(Entity owner, EdgeID edgeID)
Associates an owner entity with a primary edge. -
public unsafe void AddSecondaryEdge(Entity owner, EdgeID edgeID)
Associates an owner entity with a secondary edge. -
public unsafe bool GetEdge(Entity owner, out EdgeID edgeID)
Gets the primary EdgeID associated with an owner entity; returns true if found. -
public unsafe bool GetSecondaryEdge(Entity owner, out EdgeID edgeID)
Gets the secondary EdgeID associated with an owner entity; returns true if found. -
public unsafe bool RemoveEdge(Entity owner, out EdgeID edgeID)
Removes and returns the primary EdgeID associated with an owner entity; returns true if an edge was removed. -
public unsafe bool RemoveSecondaryEdge(Entity owner, out EdgeID edgeID)
Removes and returns the secondary EdgeID associated with an owner entity; returns true if an edge was removed. -
public unsafe ref float SetDensity(EdgeID edgeID)
Returns a ref to the m_Density field of the specified edge's PathSpecification so callers can modify density in-place. -
public unsafe ref PathfindCosts SetCosts(EdgeID edgeID)
Returns a ref to the PathfindCosts struct inside the edge's specification for in-place modification. -
public unsafe ref byte SetFlowOffset(EdgeID edgeID)
Returns a ref to the flow offset byte in the edge specification. -
public unsafe EdgeFlags GetFlags(EdgeID edgeID)
Returns the EdgeFlags value stored in the edge's PathSpecification. -
public unsafe void SetEdgeDirections(EdgeID edgeID, PathNode startNode, PathNode endNode, bool enableForward, bool enableBackward)
Sets allowed directions for the edge (forward/backward) based on nodes and flags. -
public unsafe UnsafePathfindData GetReadOnlyData()
Returns a copy of the underlying UnsafePathfindData value (dereferences the pointer and returns the struct by value). Useful for read-only snapshot-style access; be mindful this copies struct contents.
Usage Example
// Create with a persistent allocator (or Temp/TempJob as appropriate)
var pathfindData = new NativePathfindData(Allocator.Persistent);
try
{
// Use API to create edges, query stats, etc.
uint used, allocated;
pathfindData.GetMemoryStats(out used, out allocated);
UnityEngine.Debug.Log($"Pathfind memory used={used} allocated={allocated}");
// Example: clear all data
pathfindData.Clear();
// Check if created
if (pathfindData.IsCreated)
{
int count = pathfindData.Size;
UnityEngine.Debug.Log($"Active edges: {count}");
}
// ... create/update/destroy edges via CreateEdge/UpdateEdge/DestroyEdge
}
finally
{
// Always dispose to free native memory
pathfindData.Dispose();
}
Notes and safety considerations: - This type uses unsafe pointers and manual native allocation. Always call Dispose when finished to free memory. - The struct is marked with NativeDisableUnsafePtrRestriction for the pointer field; misuse can violate safety rules. The CheckRead/CheckWrite methods are conditional debug checks and no-ops unless ENABLE_UNITY_COLLECTIONS_CHECKS is defined. - Although annotated for Burst compatibility testing, care must be taken when using this container inside jobs; it is a raw pointer-backed container and may not include built-in AtomicSafetyHandle protections in this struct alone. Use appropriate job- and thread-safety patterns when accessing from worker threads.