Retain values of a List of list of a class in editor?

Run down of what I am doing:
Using scene view in editor to modify marching cubes set.

My problem is:
the values in a List i am using for the marching cubes grid are not being retained in the scene. So when I save/close then reload the List is empty and null.

My List looks something like this…

public class RoomCubes : MonoBehaviour
{
    public List<List<List<voxel>>> vox;
}

public class voxel{
    public bool check;
	public bool mark;
}

1 Answer

1

Unfortunately lists of lists are not serialized (and therefore not saved) by Unity.
A hacky, but working solution is to define a class for each dimension of your array.

E.g.

 public class RoomCubes : MonoBehaviour
 {
[SerializeField]
List<VoxelD1> voxels;

[System.Serializable]
class VoxelD1
{
      List<VoxelD2> voxels;
}

[System.Serializable]
class VoxelD2
{
      List<Voxel> voxels;
}

[System.Serializable]
public class Voxel
{
     public bool check;
     public bool mark;
 }
 }

Then, to get the element at 1,1,1

 voxels[1].voxels[1].voxels[1]

Which is pretty ugly. You could write a method

List<List<List<voxel>>> generateListRepresentation();

to generate your previous format for ease of use.

So I changed it to array instead of a list and made a class for each dimension like you suggested. It works! Thank you. I don't know if its in my head or not but it seems slower. It's because the editor is updating? [90914-untitled.jpg|90914] [edit] I suppose it should of been obvious that a large array would slow down the editor. I made a child object that is there only to hold the array. If I happen to click the child object my computer practically freezes with a 40x40x40 array. But alas if I leave it alone editing the marching cubes is pretty quick.