Hey I am new to the Unity and I am currently learning C# for game development and my question is Where can I use MultiDimensional Array in Unity
Not sure what this question is about exactly. You “can” use them anywhere in your code. However be aware that Unity can not serialize multidimensional arrays. So they can not be displayed in the inspector as a result. So you can only use them in code. Unity does use ordinary C# so there’s not really anything special when it comes to the language. Unity does have some quirks when it comes to UnityEngine.Object and derived types due to the managed - native duality of those objects. Though C# is still just normal C#
Note that in almost all cases you don’t really want to use multidimensional arrays as they are a bit clunky to work with and in the end they are just flattened one dimensional arrays under the hood with some additional language support and range checks which actually make them quite slow in tight loops. In most cases it’s easier and faster to just use a flattened one-dimensional array and calculate the offset yourself.
Since you didn’t mentioned any specific application, I don’t really know where to go from here…
Hello, in addition to everything said by Bunny above, I can also say that we in Unity typically use so-called jagged arrays or 1D arrays to accomplish almost every fundamental need.
Jagged arrays are set up as arrays of arrays. I recommend this only for light use (i.e. not memory intensive usage or in hot path tight loops). So you end up with array[i][j] instead of array[i, j].
And if you’re working with a uniform N-dimensional array in reality you typically need 2 or 3 dimensions at most anyway, and so because it’s uniform you know you’ll have N^D elements in total (or if non-uniform at least the number of cells is equal to volume of a parallelogram). And then you linearly arrange all of the elements in order, where you prefer some iteration order by axes.
This is an example of how to set it up for 2D (and you can very well hide this implementation behind a custom collection)
public class My2DArray<T> {
T[] _a;
Vector2Int _size;
public Vector2Int Size => _size;
public int Count => _size.x * _size.y;
public My2DArray(Vector2Int size) {
_size = size;
_a = new T[Count];
}
public My2DArray(int width, int height) : this(new Vector2Int(width, height)) {}
public T this[int index] {
get => _a[index];
set => _a[index] = value;
}
public T this[Vector2Int pos] {
get => _a[ToIndex(pos)];
set => _a[ToIndex(pos)] = value;
}
public T this[int x, int y] {
get => _a[ToIndex(new Vector2Int(x, y))];
set => _a[ToIndex(new Vector2Int(x, y))] = value;
}
public void Clear() {
Array.Fill(_a, default(T));
}
public T[] ToArray() => _a.MemberwiseClone();
public Vector2Int ToXY(int index) => ToXY(index, _size.x);
public int ToIndex(Vector2Int pos) => ToIndex(pos, _size.x);
static public Vector2Int ToXY(int index, int width)
=> new Vector2Int(index % width, index / width);
static public int ToIndex(Vector2Int pos, int width)
=> width * pos.y + pos.x;
}
Note: I wrote this from my head, apologies if something doesn’t compile.
Inside this space you can do all kinds of sampling, searching, use spans etc.
For 3D the conversions are slightly more complicated, but are still reasonable and follow the same principle.
Note2: This should be made to implement IList<T> (or at least ICollection<T>) I didn’t want to do this for brevity/clarity.