Best method to save a multi-dimensional array as a file?


bool[,,] data = new bool[4,4,4];

I’m having trouble coming up with a way (or -the- way) of saving something such as the above 3D array to a file. A crude way would be reading and writing from a .txt file where a line of text represents two dimensions;


And the different lines represent the final dimension;


If that makes any sense… but I feel like there MUST be an easier way to write/read this kind of data to a file. Like turning it into raw byte data and saving it? Is that possible? I’m not asking for an exact code/method (unless you feel generous), but simply some guidance towards articles where I can learn for myself. Could anyone help point me in the right path towards figuring out how to do the following effectively?

a) Read/Write file data for a 3D array of boolean’s

b) Read/Write file data for a 3D array of Color32’s

(C# language preferred)

The general method of making a multi-dimensional array “flat” should be obvious:

bool[] flatBoolArray = new bool[myMultiArray.GetLength(0) * myMultiArray.GetLength(1) * myMultiArray.GetLength(2) * myMultiArray.GetLength(3)];

int i = 0;
for(int a = 0; a < myMultiArray.GetLength(0); a ++) {
     for(int b = 0; b < myMultiArray.GetLength(1); b ++) {
          for(int c = 0; c < myMultiArray.GetLength(2); c ++) {
               for(int d = 0; d < myMultiArray.GetLength(3); d ++) {
                    flatBoolArray *= myMultiArray[a,b,c,d];*

i ++;
Can also be done with a flat list, of course.
About actually writing it to file: You could use a BinaryFormatter for that to serialize your data and save it in a file.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public static class SaveLoad {

  • public static Game loadedGame = new Game();*

  • public static void Save(Game saveGame) {*

  •  BinaryFormatter bf = new BinaryFormatter();*
  •  FileStream file = File.Create ("C:/Savegames/" + saveGame.savegameName + ".sav"); //you can call it anything you want*
  •  bf.Serialize(file, saveGame);*
  •  file.Close();*
  •  Debug.Log("Saved Game: " + saveGame.savegameName);*
  • }*

Now, the Save function takes a Game variable, and this is simpl a custom class that is marked as [System.Serializable] and has variables for the saved game name (string), as well as any other kind of data that you want to save (no types particular to Unity are allowed, such as Transforms, Vector3s, GameObjects, however. You need workarounds for that, for example a Vector3 can be represented as three floats instead.
Anyway, that is basically all there is to it. In your case, the Game class would have a bool[] variable that holds your flattened md array, which can be serialized without a hitch. Deseializing on game loading is a little bit trickier but if you need help with that I can give you the code as well.
P.S.: a Game class can also hold any kind of your custom classes or lists & arrays thereof, bas long as these are marked as [System.Serializable] as well and for them, the same limitations apply (no Unity types… If they have these, the relevant fields need to be marked as [System.NonSerialized) and would need workaround, too)
P.P.S: The advantage of serializing like this is that the resulting file is basically encrypted and can’t be read without using the deserializing function in your game. It’s also MUCH faster than writing strings to a txt file :slight_smile: