Call Grid functions from Unity Jobs
Use GridData and GridUtility to call the coordinate conversion functions of a Grid component from inside a Burst-compiled job.
The Grid and GridLayout components are managed types, so you can't access them from a job. GridData is a blittable copy of the grid's layout, cell size, cell gap, and cell swizzle. GridUtility is a static, Burst-compatible class that uses a GridData to perform the same coordinate conversions as the Grid component.
Use GridData and GridUtility in a job
Follow these steps:
On the main thread, create a
GridDatafrom aGridorGridLayoutcomponent.var gridData = new GridData(myGrid);Pass the
GridDatainto your job by value. To convert between cell space and world space, also pass theTransformHandlefrom the Grid's tilemap.[BurstCompile] struct MyGridJob : IJob { public GridData gridData; public TransformHandle transformHandle; public NativeArray<float3> worldPositions; public void Execute() { var cellPosition = new int3(2, 3, 0); GridUtility.CellToWorld(in gridData, ref transformHandle, in cellPosition, out var world); worldPositions[0] = world; } }Schedule the job as normal.
Inside an entity tile job delegate, the TilemapDataStruct parameter exposes a transformHandle property you can pass to GridUtility. For more information, refer to Create a custom entity tile.
Note
A GridData is a snapshot. If you change the cell size, cell gap, cell layout, or cell swizzle of the Grid component on the main thread, create a new GridData before scheduling the next job.
GridUtility methods
Each GridUtility method matches a function on the Grid or GridLayout component. For the parameters and return values of each method, refer to the equivalent Grid or GridLayout function in the following table.
| GridUtility method | Grid component equivalent |
|---|---|
CellToLocal, CellToLocalInterpolated |
GridLayout.CellToLocal, CellToLocalInterpolated |
LocalToCell, LocalToCellInterpolated |
GridLayout.LocalToCell, LocalToCellInterpolated |
LocalToWorld, WorldToLocal |
GridLayout.LocalToWorld, WorldToLocal |
CellToWorld, WorldToCell |
GridLayout.CellToWorld, WorldToCell |
GetBoundsLocal |
GridLayout.GetBoundsLocal |
GetCellCenterLocal |
Grid.GetCellCenterLocal |
GetLayoutCellCenter |
GridLayout.GetLayoutCellCenter |
CellSwizzle, InverseCellSwizzle |
Grid.Swizzle, Grid.InverseSwizzle |