- Home /
How do you work with multi-dimentional arrays
How do you work with multi-dimentional arrays? Any best practices you can share?
I ask this question because it is rather common dilemma among unity programmers and there is plenty of confusion around this topic. The whole issue starts when one day you write code like this...
[SerializeField] int[] arrayA;
[SerializeField] int[,] arrayB;
[SerializeField] int[][] arrayC;
...and you notice that arrayA
will serialize fine, but arrayB
and arrayC
wont (wont show up in Inspector). So how you deal with that?
Tragicomical anecdote: UE4 has this problem as well.
TArray<TArray<int32>>
wont serialize nor show up in Details panel. Made me chuckle, won't lie : )
I'm not certain how far this will be able to take you in terms of multi-dimensional arrays, but this Github ArrayDrawer project might be a viable starting point for experimenting.
Comparing a few different resources, it looks like that uses a similar (if not the same) baseline implementation for arrays that Unity does by default. It would be interesting to see if parts of it can be expanded on (like, as an alternative, divergent array support) to provide support for multi-dimensional arrays.
The part I'm least sure about, however, if it's it something that *can* reasonably be expanded on as-is, since I don't know enough about how data is read from SerializedProperties.
Answer by andrew-lukasik · Oct 04, 2021 at 07:18 PM
My approach is to keep your data as basic and serializable 1-dimentional array, but create some kind of facade object that provides n-dimensional indexer accessors ( [ x , y , ... ]) so the the index conversion issue disappears almost completely.
Implementation example: https://gist.github.com/andrew-raphael-lukasik/b1cba7b10407c7540e33f1c0a9930746
Answer by zORg_alex · Oct 09, 2021 at 10:13 AM
I've thrown this together when ran into this problem. You almost can use it as an array and convert to it. Not so invisible as I hoped though. You can implicitly cast ArrayElement and T[] back and forth unless you want to cast array to ArrayElements[], then you're out of luck it seems, only an extension method can do that, no casting to array of type is available in C#.
//Example of use
int[][] zzz = new int[][] { new int[]{ 1, 2, 3 }, new int[]{ 4, 5, 6 } };
ArrayElement<int[]> xxx = zzz;
ArrayElement<int>[] ccc = zzz.ToArrayElemets();
Here's the implementation:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace MultidimensionalArrayUtils {
[Serializable]
public class ArrayElement<T> : IEnumerable, IEnumerable<T> {
[SerializeField]
private T[] _array;
public T[] Array { get => _array; }
public T this[int i] => _array[i];
public IEnumerator GetEnumerator() => _array.GetEnumerator();
IEnumerator<T> IEnumerable<T>.GetEnumerator() {
foreach (var item in _array) {
yield return item;
}
}
public ArrayElement(T[] array) {
_array = array;
}
public ArrayElement(IEnumerable<T> collection) {
_array = collection.ToArray();
}
public static implicit operator T[](ArrayElement<T> arr) => arr == null ? new T[0] : arr.Array;
public static implicit operator ArrayElement<T>(T[] arr) => new ArrayElement<T>(arr == null ? new T[0] : arr);
}
public static class ArrayElementExtensions {
public static ArrayElement<T>[] ToArrayElemets<T>(this IEnumerable<IEnumerable<T>> collectionOfCollections) =>
collectionOfCollections.Select(t => (ArrayElement<T>)t.ToArray()).ToArray();
public static ArrayElement<T>[] ToArrayElemets<T>(this IEnumerable<T[]> collectionOfCollections) =>
collectionOfCollections.Select(t => (ArrayElement<T>)t).ToArray();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613043827im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make custom 2 dimensional enum array class 1 Answer
Best way to match up 3 bits of data 2 Answers
Serialize custom multidimensional array from Inspector 3 Answers
Unity - Serializing a single property or UnityEngine.Object reference 0 Answers
When attempting to serialize and save a jagged array it gives me an error message 1 Answer