Game.Pathfind.CoverageAction
Assembly: Assembly-CSharp (game runtime code)
Namespace: Game.Pathfind
Type: struct (value type)
Base: System.ValueType
Implements: System.IDisposable
Summary:
CoverageAction is a small value-type wrapper that owns a NativeReference to a CoverageActionData instance. It provides a by-ref property to access the underlying CoverageActionData stored in native memory and implements IDisposable to free that native memory. This struct is used by the pathfinding/coverage systems to allocate and manage CoverageActionData in native (unsafe) memory for performance and job compatibility.
Fields
-
public NativeReference<CoverageActionData> m_Data
Holds the native reference to the underlying CoverageActionData instance. The NativeReferenceallocates native memory (using the provided Allocator) and provides ValueAsRef() to obtain a ref to the contained value. The wrapper must be disposed to free the native memory. -
public ref CoverageActionData data => ref m_Data.ValueAsRef()
A ref-returning accessor that exposes the underlying CoverageActionData by reference. This lets callers read or write fields on the native CoverageActionData without copying.
Properties
public ref CoverageActionData data { get }
Provides direct by-ref access to the CoverageActionData stored in the NativeReference. The property is read-only (no setter) and returns a ref to the native object. Because it returns a reference, modifications operate on the native memory directly.
Notes: - Accessing this property after Dispose() has been called is invalid. - The returned ref can be used in performance-sensitive code and in contexts where avoiding copies is important (e.g., inside hot loops or jobs when allowed).
Constructors
public CoverageAction(Allocator allocator)
Allocates a new CoverageActionData in native memory using the supplied Allocator and stores the NativeReference in m_Data. The constructor calls into CoverageActionData(allocator) to initialize the inner data.
Notes: - Choose an appropriate Allocator (e.g., TempJob, Persistent) depending on lifetime. - If CoverageActionData performs its own native allocations in its constructor, those will be created as part of this operation and must be cleaned up by Dispose().
Methods
public void Dispose()
Disposes the CoverageAction: first calls Dispose() on the contained CoverageActionData (via the ref accessor) and then disposes the NativeReference itself to free the underlying native memory.
Important behavior and safety notes: - Once Dispose() is called, the native memory is freed and further access to data or m_Data is invalid and will likely crash. - CoverageAction is a struct; copying it will copy the NativeReference handle. That means multiple copies will reference the same native memory. Disposing one copy will invalidate the memory for all copies. Avoid unintended copies or ensure only one owner disposes. - If you intend to pass this into Unity Jobs or Burst-compiled contexts, ensure CoverageActionData is blittable and that the chosen Allocator and lifetime are compatible with the job system.
Usage Example
using Unity.Collections;
using Game.Pathfind;
// Allocate a CoverageAction with persistent lifetime (example)
var action = new CoverageAction(Allocator.Persistent);
try
{
ref var d = ref action.data;
// Read or modify fields on CoverageActionData:
// d.someField = someValue;
// d.Initialize(...);
}
finally
{
// Always dispose to free native memory
action.Dispose();
}
Additional recommendations: - Prefer using try/finally or a using-like pattern to guarantee Dispose() is called. - If you must share the struct, document ownership and disposal responsibility clearly to avoid double-dispose or use-after-free bugs. - Select Allocator.TempJob for short-lived allocations that will be used from jobs, Allocator.Persistent for long-lived allocations.