Skip to content

NativeArray Extensions

Andrzej Kebab edited this page Feb 4, 2024 · 3 revisions

UtilityLibary.Unity.Runtime

Get At Index

GetAtFlatIndex

Retrieves the element at the specified 2D index in a flattened NativeArray.

NativeArray<int> array = new NativeArray<int>(new int[] { 1, 2, 3, 4 }, Allocator.Temp);
int width = 2;
int x = 1;
int y = 0;

int result = array.GetAtFlatIndex(width, x, y);
// Output: 2

Parameters:

  • array: The one-dimensional NativeArray to retrieve the element from.
  • width: The width of the original two-dimensional array.
  • x: The x-coordinate in the original two-dimensional array.
  • y: The y-coordinate in the original two-dimensional array.

Returns:

  • T: The element at the specified 2D index in the flattened array.

GetAtFlatIndex

Retrieves the element at the specified 3D index in a flattened NativeArray.

NativeArray<int> array = new NativeArray<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }, Allocator.Temp);
int width = 2;
int height = 2;
int x = 1;
int y = 0;
int z = 1;

int result = array.GetAtFlatIndex(width, height, x, y, z);
// Output: 6

Parameters:

  • array: The one-dimensional NativeArray to retrieve the element from.
  • width: The width of the original three-dimensional array.
  • height: The height of the original three-dimensional array.
  • x: The x-coordinate in the original three-dimensional array.
  • y: The y-coordinate in the original three-dimensional array.
  • z: The z-coordinate in the original three-dimensional array.

Returns:

  • T: The element at the specified 3D index in the flattened array.

GetAtFlatIndex

Retrieves the element at the specified 3D index in a flattened NativeArray. This method assumes that the original three-dimensional array was a cube, with equal width, height, and depth.

NativeArray<int> array = new NativeArray<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }, Allocator.Temp);
int size = 2;
int x = 1;
int y = 0;
int z = 1;

int result = array.GetAtFlatIndex(size, x, y, z);
// Output: 6

Parameters:

  • array: The one-dimensional NativeArray to retrieve the element from.
  • size: The size (width, height, and depth) of the original three-dimensional array.
  • x: The x-coordinate in the original three-dimensional array.
  • y: The y-coordinate in the original three-dimensional array.
  • z: The z-coordinate in the original three-dimensional array.

Returns:

  • T: The element at the specified 3D index in the flattened array.

Set At Index

SetAtFlatIndex

Sets the value at the specified 2D index in a flattened NativeArray.

NativeArray<int> array = new NativeArray<int>(new int[] { 1, 2, 3, 4 }, Allocator.Temp);
int width = 2;
int x = 1;
int y = 0;
int value = 8;

array.SetAtFlatIndex(width, x, y, value);
// Output: { 1, 8, 3, 4 }

Parameters:

  • array: The one-dimensional NativeArray to set the value in.
  • width: The width of the original two-dimensional array.
  • x: The x-coordinate in the original two-dimensional array.
  • y: The y-coordinate in the original two-dimensional array.
  • value: The value to set at the specified index.

SetAtFlatIndex

Sets the value at the specified 3D index in a flattened NativeArray.

NativeArray<int> array = new NativeArray<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }, Allocator.Temp);
int width = 2;
int height = 2;
int x = 1;
int y = 0;
int z = 1;
int value = 8;

array.SetAtFlatIndex(width, height, x, y, z, value);
// Output: { 1, 2, 3, 4, 5, 8, 7, 8 }

Parameters:

  • array: The one-dimensional NativeArray to set the value in.
  • width: The width of the original three-dimensional array.
  • height: The height of the original three-dimensional array.
  • x: The x-coordinate in the original three-dimensional array.
  • y: The y-coordinate in the original three-dimensional array.
  • z: The z-coordinate in the original three-dimensional array.
  • value: The value to set at the specified index.

SetAtFlatIndex

Sets the value at the specified 3D index in a flattened NativeArray. This method assumes that the original three-dimensional array was a cube, with equal width, height, and depth.

NativeArray<int> array = new NativeArray<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }, Allocator.Temp);
int size = 2;
int x = 1;
int y = 0;
int z = 1;
int value = 8;

array.SetAtFlatIndex(size, x, y, z, value);
// Output: { 1, 2, 3, 4, 5, 8, 7, 8 }

Parameters:

  • array: The one-dimensional NativeArray to set the value in.
  • size: The size (width, height, and depth) of the original three-dimensional array.
  • x: The x-coordinate in the original three-dimensional array.
  • y: The y-coordinate in the original three-dimensional array.
  • z: The z-coordinate in the original three-dimensional array.
  • value: The value to set at the specified index.

Clone this wiki locally