public sealed class StreamableGrid : ScriptableObject
Each Streamable Grid represents a collection of Stremable Cells, and more importantly,
the
Streamable Asset Chunks associated with those Cells.
It stores vital information about the Cells, such as their dimensions and whether
each Cell
has Asset Chunks associated with it (and if they do, how many Asset Chunks are used
by it),
and vital information about those Chunks, such as the shared name of all Asset Chunks
in the
group and their level of detail.
You can think of a Streamable Grid as a blueprint for building
World's at runtime (Worlds combines multiple Streamable Grid's
and actual scene positional space to form a physical game world).
To create a Streamable Grid asset, right click the folder where you'd like to create
it and choose
"Create -> Deep Space Labs -> SAM -> Streamable Grid".
All of the public methods that take in or return a Cell
are 1 based. This means the first
Cell on the Streamable Grid has a row of 1, column of 1, and (for 3D worlds) a layer
of 1.
If you see PreInitSafe in the name of one of the methods or properties below, it means
that the
method or property can be called before the Streamable Grid has been initialized (at
runtime or
in editor code). Methods or properties without PreInitSafe should only be called after
the
Streamable Grid has been intialized (which only happens when the game is run).
| Name | Type | Description |
|---|---|---|
| Axes_PreInitSafe | Axes |
Gets the Axes used by the Streamable Grid |
| CellEqualityComparer | EqualityComparer<Cell> |
Gets an Cell equality comparer matching the type of Streamable Grid (2D or 3D).
|
| ColumnWidthsNative | NativeArray<double> |
Gets the column widths of the Streamable Grid as a native array. The widths are only configured once the Streamable Grid has been initialized. The lifetime of this array is handled by the Streamable Grid, so you should never dispose it yourself. You should also treat it as a read only array and not reassign values to any of its indexes, otherwise you will screw things up royally since SAM uses these arrays internally. Finally, note that the length of the array will be 1 if all columns are of equal size (in an attempt to save space). |
| ColumnsAsLong | long |
Gets the number of columns on the Streamable Grid as a long. |
| Columns_PreInitSafe | int |
Gets the number of columns on the Streamable Grid. |
| EnabledCells | long |
Gets the total number of enabled cells on this Streamable Grid. |
| FlattenCellIndex | Func<Cell, long> |
A function that can be retrieved and used to flatten a Streamable Grid Cell Index.
|
| FlattenCellIndexForSparseEnabledArray | Func<Cell, int> |
A function that can be retrieved and used to flatten the Cell Index of an Enabled
Cell on the Streamable Grid, where the input Cell is
one based.
|
| FlattenCellIndex_ZeroBased | Func<Cell, long> |
Works just like
FlattenCellIndex,
except the input Streamable Grid Cell should be zero based.
|
| FlattenEnabledCellIndexForSparseEnabledArray_ZeroBased | Func<Cell, int> |
Works just like
FlattenCellIndexForSparseEnabledArray,
except the input Streamable Grid Cell should be zero based.
|
| GetColumnWidthFunction | Func<int, double> |
Gets a function that can be used to get the width of a particular column (the input) on this Streamable Grid. Configured after the Streamable Grid is initialized. |
| GetLayerHeightFunction | Func<int, double> |
Gets a function that can be used to get the height of a particular layer (the input) on this Streamable Grid. Configured after the Streamable Grid is initialized. |
| GetRowLengthFunction | Func<int, double> |
Gets a function that can be used to get the length of a particular row (the input) on this Streamable Grid. Configured after the Streamable Grid is initialized. |
| GridDimensions | Vector3Double |
Gets the dimensions of the Streamable Grid expressed as a Vector3Double. This can be useful in math operations. The components of the vector are automatically set correctly according to the Axes of the Grid. |
| Height | double |
Gets the height of the Streamable Grid, which is the distance between the beginning of the first layer and end of the last layer. |
| Indices | Cell |
Gets the number of Rows, Columns, and Layers expressed as a Cell structure. |
| IsCellDisabled | Func<Cell, bool> |
Gets (post initialization only)/Sets (PreInitSafe) a function that can be used to
determine if a particular cell on the Streamable Grid is disabled.
|
| IsGrid3D | bool |
Gets a value indicating whether the Streamable Grid is 3D (Axes Type set to Three
Dimensional).
|
| IsInitialized | bool |
Gets a value indicating if the Streamable Grid is initialized. It should go without saying, but this property can be accessed before the Grid has been intialized (in which case it will return false). |
| LayerHeightsNative | NativeArray<double> |
Gets the layer heights of the Streamable Grid as a native array. The heights are only configured once the Streamable Grid has been initialized. The lifetime of this array is handled by the Streamable Grid, so you should never dispose it yourself. You should also treat it as a read only array and not reassign values to any of its indexes, otherwise you will screw things up royally since SAM uses these arrays internally. Finally, note that the length of the array will be 1 if all layers are of equal size (in an attempt to save space), or if the Streamable Grid is 2D. |
| Layers | int |
Gets the number of layers on the Streamable Grid. Will be 1 if the Axes is not Three Dimensional. |
| LayersAsLong | long |
Gets the number of layers on the Streamable Grid as a long. |
| Length | double |
Gets the length of the Streamable Grid, which is the distance between the beginning of the first row and end of the last row. |
| LevelsOfDetail_PreInitSafe | int |
Gets the number of LOD Groups on the Streamable Grid (effectively the levels of detail
the Grid makes use of).
|
| RowLengthsNative | NativeArray<double> |
Gets the row lengths of the Streamable Grid as a native array. The lengths are only configured once the Streamable Grid has been initialized. The lifetime of this array is handled by the Streamable Grid, so you should never dispose it yourself. You should also treat it as a read only array and not reassign values to any of its indexes, otherwise you will screw things up royally since SAM uses these arrays internally. Finally, note that the length of the array will be 1 if all rows are of equal size (in an attempt to save space). |
| RowsAsLong | long |
Gets the number of rows on the Streamable Grid as a long. |
| Rows_PreInitSafe | int |
Gets the number of rows on the Streamable Grid. |
| ToOneBasedCell | Func<Cell, Cell> |
Gets a function that can be used to convert a cell in zero based format to a cell in one based format. This method is especially useful for processing large amounts of cells, as it will disregard the layer value if the Streamable Grid is not 3D (will always be 1 on the returned Cell). |
| ToZeroBasedCell | Func<Cell, Cell> |
Gets a function that can be used to convert a cell in one based format to a cell in zero based format. This method is especially useful for processing large amounts of cells, as it will disregard the layer value if the Streamable Grid is not 3D (will always be 0 on the returned Cell). |
| Width | double |
Gets the width of the Streamable Grid, which is the distance between the beginning of the first column and end of the last column. |
public static long CalculateTotalCells(int rows, int columns, int layers)
Calculates the total number of cells given rows, columns, and layers values. The input values are casted to long before the calculation is performed, allowing the total number of cells to be as large as 18,446,744,073,709,551,615. This method does not detect overflow, if overflow occurs the returned value will likely be negative and invalid. If using this method with input values retrieved from a Streamable Grid, however, you do not need to worry about overflow, as the Streamable Grid can only contain rows/columns/layers values that do not overflow.
| Name | Type | Description |
|---|---|---|
| rows | int |
The number of rows to use in the calculation. |
| columns | int |
The number of columns to use in the calculation. |
| layers | int |
The number of layers to use in the calculation. |
long
The total number of cells.
public static long CalculateTotalCellsWithOverflowCheck(int rows, int columns, int layers)
Calculates the total number of cells given rows, columns, and layers values. The input values are casted to long before the calculation is performed, allowing the total number of cells to be as large as 18,446,744,073,709,551,615. This method will detect and throw an overflow exception if the total number of cells is greater than this value!
| Name | Type | Description |
|---|---|---|
| rows | int |
The number of rows to use in the calculation. |
| columns | int |
The number of columns to use in the calculation. |
| layers | int |
The number of layers to use in the calculation. |
long
The total number of cells.
| Type | Condition |
|---|---|
| OverflowException | Thrown if the calculated value is greater than 18,446,744,073,709,551,615 |
public Cell ConvertCellOnEndlessGridToCellOnStreamableGrid(Cell cellOnEndlessGrid)
Converts an Endless Grid Cell to its equivalent Streamable Grid Cell. Each Endless
Grid Cell is just a projection of a Streamable Grid Cell, and thus has the same dimensions
and chunk associations.
Note that the passed in cell should be 1 based. That is, the first cell in the grid
has a row of 1, column of 1,
and layer of 1. The returned Streamable Grid Cell will also be 1 based.
Use this method over the PreInitSafe version if you know the Streamable Grid is initialized,
as it will be slightly faster.
| Name | Type | Description |
|---|---|---|
| cellOnEndlessGrid | Cell |
The Endless Grid Cell to be converted. |
Cell
The equivalent Streamable Grid Cell for the Endless Grid Cell specified.
public Cell ConvertCellOnEndlessGridToCellOnStreamableGrid_PreInitSafe(Cell cellOnEndlessGrid)
Converts a cell on the Endless Grid to a cell on the Streamable Grid. You can use this method before the Streamable Grid has been initialized.
| Name | Type | Description |
|---|---|---|
| cellOnEndlessGrid | Cell |
The one based cell on the Endless Grid you wish to convert |
Cell
The converted one based cell on the Streamable Grid.
public int ConvertEndlessGridColumnToStreamableGridColumn_PreInitSafe(int endlessGridColumn)
Converts an Endless Grid column to a column on the Streamable Grid. You can use this method before the Streamable Grid has been initialized.
| Name | Type | Description |
|---|---|---|
| endlessGridColumn | int |
The Endless Grid column to convert. |
int
The column on the Streamable Grid.
public int ConvertEndlessGridLayerToStreamableGridLayer_PreInitSafe(int endlessGridLayer)
Converts an Endless Grid layer to a layer on the Streamable Grid. You can use this method before the Streamable Grid has been initialized.
| Name | Type | Description |
|---|---|---|
| endlessGridLayer | int |
The Endless Grid layer to convert. |
int
The layer on the Streamable Grid.
public int ConvertEndlessGridRowToStreamableGridRow_PreInitSafe(int endlessGridRow)
Converts an Endless Grid row to a row on the Streamable Grid. You can use this method before the Streamable Grid has been initialized.
| Name | Type | Description |
|---|---|---|
| endlessGridRow | int |
The Endless Grid row to convert. |
int
The row on the Streamable Grid.
public void DeRegister()
Deregisters with the World and if no more users are registered, de-initializes the
World (disposes of Native Containers used by the Streamable Grid at runtime).
The World cannot be reinitialized because the arrays used to create the native arrays
are cleared at runtime (to save on memory).
So, once user count becomes 0, the World should never be registered with another user.
Note, in practice it should never be necessary for you to call this method manually;
it is done automatically by SAM! The only time you
might need to do so is if you are using the Streamable Grid for some sort of custom
purpose. Every De-Register call must have been proceeded
by a Register call previously!
public EnabledStreamableGridEnumerable EnabledStreamableGridCells()
Gets an enumerable collection of the enabled cells on this Streamable Grid. This can
be used in a foreach statement to iterate over
only enabled cells in the grid in a garbage free manner, and automatically takes into
account whether the grid is 2D or 3D. This is very useful in situations where
you need to perform some operation that uses a Cell struct, for each enabled cell
on the Streamable Grid. If you have a very large world with a ton of cells where
most are disabled, it can make more sense to use this rather than iterating over every
cell on the Streamable Grid via
StreamableGridCells_PreInitSafe.
Also note that in some cases, it may be beneficial to iterate over the flattened index
of each enabled cell rather than the index in
Cell form, which you can do using
the FlattenedIndexesOfEnabledCells method.
Finally, take heed that this method can only be used after the Streamable Grid has
been initialized. If you need to a method that will work
before the Grid is intialized, for example outside of Play Mode in the editor, use
EnabledStreamableGridCells_PreInitSafe instead.
EnabledStreamableGridEnumerable
The enumerable collection of enabled Streamable Grid Cells.
Example:
class IterateOverEnabledStreamableGridCellsExample
{
StreamableGrid streamableGrid;
public void Iterate()
{
foreach(Cell enabledCell in streamableGrid.EnabledStreamableGridCells())
DoSomething(enabledCell);
}
}
public EnabledCellsEnumerable_PreInitSafe EnabledStreamableGridCells_PreInitSafe()
Same as EnabledStreamableGridCells, except this method can be used even if the Streamable Grid has not be initialized, for example in the editor outside of Play Mode.
EnabledCellsEnumerable_PreInitSafe
The enumerable collection of enabled Streamable Grid Cells.
Example:
class IterateOverEnabledStreamableGridCellsExample
{
StreamableGrid streamableGrid;
public void Iterate()
{
foreach(Cell enabledCell in streamableGrid.EnabledStreamableGridCells_PreInitSafe())
DoSomething(enabledCell);
}
}
public FlattenedIndexesOfEnabledCellsEnumerable FlattenedIndexesOfEnabledCells()
Gets an enumerable collection of the flattened indexes of enabled cells on this Streamable
Grid. This can be used in a foreach statement to iterate over
only the enabled cells in the grid in a garbage free manner, and automatically takes
into account whether the grid is 2D or 3D.
This is very useful in situations where you need to perform some operation that uses
the Flattened Index of each enabled cell on the Streamable Grid.
If you have a very large world with a ton of cells where
most are disabled, it can make more sense to use this rather than iterating over every
cell and checking whether each one is enabled or disabled.
For each enabled cell, the flattened index is equivalent to the value returned by
the
FlattenCellIndex function for that cell (not the
FlattenEnabledCellIndex function!).
The number of items in the collection will always be equal to EnabledCells. If you need
to iterate over this collection repeatedly over different frames, it may make sense
to store the values in an array, using EnabledCells as the
length of the array.
FlattenedEnabledStreamableGridIndexesEnumerable
The enumerable collection of the flattened indexes of Streamable Grid Cells that are
enabled.
Example:
class IterateOverFlattenedIndexesOfEnabledCellsExample
{
StreamableGrid streamableGrid;
public void Iterate()
{
Data[] enabledCellData = new Data[streamableGrid.EnabledCells];
int i = 0;
foreach(int flattenedIndex in streamableGrid.FlattenedIndexesOfEnabledCells))
enabledCellData[i++] = GetCellData(flattenedIndex);
}
}
public CellDimensions GetCellDimensionsOfStreamableGridCell(Cell streamableGridCell)
Get the cell dimensions of cell on the Streamable Grid. The indexes of this cell must
fall within the range
of rows, columns, and/or layers of your Streamable Grid.
For instance, in a 4 rows x 4 columns Streamable Grid, a row or column index of
0 or 5 would not be valid (but 1, 2, 3, or 4 would be).
| Name | Type | Description |
|---|---|---|
| streamableGridCell | Cell |
The cell on the Streamable Grid whose dimensions should be retrieved. |
CellDimensions
The cell dimensions of the cell.
public CellDimensions GetCellDimensionsOfStreamableGridCell_PreInitSafe(Cell streamableGridCell)
Gets the dimensions of a specific cell on the Streamable Grid. Safe for use before Streamable Grid has been initialized (for example, in the editor).
| Name | Type | Description |
|---|---|---|
| streamableGridCell | Cell |
The cell on the Streamable Grid to get the dimensions for |
CellDimensions
The cell's dimensions
public long GetCountOfEnabledCells_PreInitSafe()
Gets a count of the total Enabled Cells on this Streamable Grid (as set in the Unity Editor). Safe to use before or after the Streamable Grid has been initialized, although if you know for sure that the Grid has been initialized, using the EnabledCells property will be marginally faster.
long
The number of Enabled Cells. If you have set a custom IsCellDisabled function, note
that this value will not be accurate, as
there is no way to count the number of enabled cells in that case.
public double GetHeightOfEndlessGridLayer(int endlessGridLayer)
Get the height of an Endless Grid layer. Each input Endless Grid layer is translated
to its equivalent Streamable Grid layer, and then the height of that Streamable Grid
layer is returned.
This is a runtime method and should not be used in the editor.
| Name | Type | Description |
|---|---|---|
| endlessGridLayer | double |
The layer who's height should be retrieved. |
double
The height of the input endlessGridLayer.
public double GetHeightOfStreamableGridLayer(int streamableGridLayer)
Get the height of a Streamable Grid layer. The input streamableGridLayer should be
0 index based. For instance, to get the
height of the very first layer
in the grid, you would pass in 0, not 1. To get the second layer, you'd pass in 1,
not 2, and so on.
This is a runtime method and should not be used in the editor.
| Name | Type | Description |
|---|---|---|
| streamableGridLayer | double |
The layer who's height should be retrieved. |
double
The height of the input streamableGridLayer.
public GridLODDetails GetLODDetails_PreInitSafe(int LOD)
Returns the grid LOD details for the given LOD. All LOD Group specific data can be retrieved from the GridLODDetails object.
| Name | Type | Description |
|---|---|---|
| LOD | int |
The LOD. |
GridLODDetails
The grid LOD details for the LOD.
| Type | Condition |
|---|---|
| NullReferenceException | thrown if the LOD is less than 1 or greater than the number of levels of detail on this Streamable Grid. |
public int GetChunksOfStreamableGridCell_PreInitSafe(int LOD, Cell streamableGridCell)
Gets the number of chunks the cellOnStreamableGrid has in the LOD group specified by the LOD index.
| Name | Type | Description |
|---|---|---|
| LOD | index |
The index of the LOD as shown in the inspector. |
| streamableGridCell | Cell |
The cell on the Streamable Grid (one based). |
int
The number of chunks.
public double GetLengthOfEndlessGridRow(int endlessGridRow)
Get the length of an Endless Grid row. Each input Endless Grid row is translated
to its equivalent Streamable Grid row, and then the length of that Streamable Grid
row is returned.
This is a runtime method and should not be used in the editor.
| Name | Type | Description |
|---|---|---|
| endlessGridRow | double |
The row who's length should be retrieved. |
double
The length of the input endlessGridRow.
public double GetLengthOfStreamableGridRow(int streamableGridRow)
Get the length of a Streamable Grid row. The input streamableGridRow should be 0 index
based.
For instance, to get the length of the very first row
in the grid, you would pass in 0, not 1. To get the second row, you'd pass in 1, not
2, and so on.
This is a runtime method and should not be used in the editor.
| Name | Type | Description |
|---|---|---|
| streamableGridRow | double |
The row who's length should be retrieved. |
double
The length of the input streamableGridRow.
public CellDimensions GetVectorDimensionsOfStreamableGridCell_PreInitSafe(Cell streamableGridCell)
Gets the dimensions of a specific cell on the Streamable Grid. Safe for use before
Streamable Grid has
been initialized (for example, in the editor).
This method is similar to
GetCellDimensionsOfStreamableGridCell_PreInitSafe, however the dimensions are placed
on the correct axes according
to the Streamable Grid's Axes. For example, if the Axes is 2D XZ or 3D, the row length
of the cell is placed
in the z component of the Vector3Double and the layer height is placed in the y component.
If the Axes is 2D XY, the row length is placed in the y component instead, and the
layer height is placed in the
z component.
| Name | Type | Description |
|---|---|---|
| streamableGridCell | Cell |
The cell on the Streamable Grid to get the dimensions for |
Vector3Double
The cell's dimensions as a Vector3Double. This method automatically takes into account
the Axes and places
the cell's dimensions on the correct axes.
public double GetWidthOfEndlessGridColumn(int endlessGridColumn)
Get the width of an Endless Grid column. Each input Endless Grid column is translated
to its equivalent Streamable Grid column, and then the width of that Streamable Grid
column is returned.
This is a runtime method and should not be used in the editor.
| Name | Type | Description |
|---|---|---|
| endlessGridColumn | double |
The column who's width should be retrieved. |
double
The width of the input endlessGridColumn.
public double GetWidthOfStreamableGridColumn(int streamableGridColumn)
Get the width of a Streamable Grid column. The input streamableGridColumn should be
0 index based.
For instance, to get the width of the very first column
in the grid, you would pass in 0, not 1. To get the second column, you'd pass in 1,
not 2, and so on.
This is a runtime method and should not be used in the editor.
| Name | Type | Description |
|---|---|---|
| streamableGridColumn | double |
The column who's width should be retrieved. |
double
The width of the input streamableGridColumn.
public bool IsCellDisabled_PreInitSafe(Cell streamableGridCell)
Can be used to determine if a particular Streamable Cell on this grid is disabled. Uses the states of the cells as set in the inspector of the Streamable Grid. At runtime it is better to use the IsCellDisabled function, as it will be faster.
| Name | Type | Description |
|---|---|---|
| streamableGridCell | Cell |
The Streamable Grid Cell you wish to know the state of. |
bool
True if the Cell is disabled, false otherwise.
public void Register()
Registers with the Streamable Grid. This initializes the Grid if no other users have
already registered with it, and must be
used if you wish to access members that are only available after the Grid has been
initialized, such as the native arrays.
Note that SAM automatically calls this method when the Streamable Grid is used with
any World (even if it's just a prototype), so the
chances of you needing to call it yourself are slim. The only time that might not
be true is if you are using the Streamable Grid
for some custom purpose, and it is not being used with any Worlds in the scene.
For each call to Register, you must call DeRegister! This method should also not be
called in the editor outside of Play Mode!
public void SetGlobalExtraData_PreInitSafe(string key, string data)
Sets the global extra data for a given key.
This will replace any existing data using the key for this Streamable Grid, or create
a new entry if there is no existing entry using the key.
Generally speaking, you should call this method before SAM has been initialized in
order to ensure that anything making use of the
Extra Data will have the correct value, as most things making use of it will retrieve
the extra data just once (when SAM is
initialized) and cache the value for performance reasons.
If you want to set/change the extra data for a specific LOD Group, you should use
the GridLODDetail class's
SetExtraData_PreInitSafe method instead.
| Name | Type | Description |
|---|---|---|
| key | string |
The key used to set the extra data. |
| data | string |
The data to set. |
public StreamableGridEnumerable StreamableGridCells_PreInitSafe()
Gets an enumerable collection of the cells on this Streamable Grid. This can be used
in a foreach statement to iterate over
every cell in the grid in a garbage free manner, and automatically takes into account
whether the grid is 2D or 3D.
This is very useful in situations where
you need to perform some operation that uses a Cell struct, for each cell on the Streamable
Grid. Note that the enumerable does not take into account
disabled cell locations, which may or may not be a problem.
If you have a very large world with many disabled cells, you should consider using
the
EnabledStreamableGridCells_PreInitSafe method instead.
StreamableGridEnumerable
The enumerable collection of Streamable Grid Cells
Example:
class IterateOverStreamableGridCellsExample
{
StreamableGrid streamableGrid;
public void Iterate()
{
foreach(Cell cell in streamableGrid.StreamableGridCells_PreInitSafe())
DoSomething(cell);
}
}
public bool TryGetGlobalExtraData_PreInitSafe(string key, out string data)
Attemps to get data stored in the Streamable Grids Global Extra Data. Use this if you know for certain the data you want is stored in the Global Extra Data. If you are not sure if it's stored here, or you know it is but there also may be data with the same key stored in an LOD's Extra Data, use the GridLODDetails TryGetExtraData_PreInitSafe method istead, as that will return the data from the LOD if it exist, or data from the Global Extra Data if it does not exist on the LOD.
| Name | Type | Description |
|---|---|---|
| key | string |
The key to use to access the data. |
| data | string |
Will be set to the data if the method returns true or null if the method returns false. |
bool
True if the data was successfully retrieved, false otherwise.