0

I am working on saving arrays that I have pulled from a long text file. I used a foreach loop to get the arrays but am a little lost on where to go from here. I can use [serializedfield] to show the coordinates X,Y,Z in the inspector but need to figure out how to save the data from the loop. Any advice to get me in the right direction would be awesome!
Thank you ahead of time. Here is my code:

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

[Serializable]
public class MultiArrayList : MonoBehaviour {

public TextAsset datafile;
private int i;
//private float[,] coordinates;
[SerializeField] private float[] coordX;
[SerializeField] private float[] coordY;
[SerializeField] private float[] coordZ;
[SerializeField] private float[] intensity;
//private Vector3 verts;

// Use this for initialization
void Start() {

    string[] dataLines = datafile.text.Split ('\n');
    string[] lineValues;
    //print (dataLines.Length);
    i=0;

    //float[,] coordinates = new float[6853, 3]; 
    float[] coordX = new float[6853];
    float[] coordY = new float[6853]; 
    float[] coordZ = new float[6853]; 
    float[] intensity = new float[6853];
    foreach (string line in dataLines) {

        lineValues = line.Split (' ');
        float coordinateX = float.Parse (lineValues [0]);
        float coordinateY = float.Parse (lineValues [1]);
        float coordinateZ = float.Parse (lineValues [2]);
        float intens = float.Parse (lineValues [3]);

        coordX [i] = coordinateX;
        coordY [i] = coordinateY;
        coordZ [i] = coordinateZ;

        //coordinates [i, 0] = coordinateX;
        //coordinates [i, 1] = coordinateY;
        //coordinates [i, 2] = coordinateZ;

        intensity [i] = intens;

        //print (coordX [i]);

        i++; 

        //Vector3 coordinates = new Vector3 (coordinateX,coordinateY,coordinateZ);
        //print (coordinates);

    }


}

void OnGUI()
{
    Display (coordX [i]);
}

}
Sockness_Rogers
  • 1,693
  • 3
  • 12
  • 23
  • why don't you use the class field arrays but create new ones with the same name in the `start` method? – Mong Zhu Jun 28 '16 at 10:07

1 Answers1

1

Here is a suggestion. You can use XML-Serialization to save the entire class with a given dataset to a xml-file. In the example below I included 2 methods for saving and retrieving the data: I made the arrays a little smaller to be able to show also the ouput. But it should work fine with larger arrays also :) One thing is important: you have to declare the arrays as public. Otherwise they will not end up in the file.

public class MultiArrayList
{

    private int i = 0;
    //private float[,] coordinates;
    public int MyProperty { get; set; }
    public float[] coordX { get; set; }
    public float[] coordY { get; set; }
    public float[] coordZ { get; set; }
    public float[] intensity { get; set; }
    //private Vector3 verts;

    // Use this for initialization
    public void Start()
    {
        string test = "1 2 3 99\n4 5 6 99\n7 89 90 99";
        string[] dataLines = test.Split('\n');
        string[] lineValues;
        //print (dataLines.Length);
        i = 0;

        //float[,] coordinates = new float[6853, 3]; 
        coordX = new float[4];
        coordY = new float[4];
        coordZ = new float[4];
        intensity = new float[4];
        foreach (string line in dataLines)
        {

            lineValues = line.Split(' ');
            float coordinateX = float.Parse(lineValues[0]);
            float coordinateY = float.Parse(lineValues[1]);
            float coordinateZ = float.Parse(lineValues[2]);
            float intens = float.Parse(lineValues[3]);

            coordX[i] = coordinateX;
            coordY[i] = coordinateY;
            coordZ[i] = coordinateZ;


            intensity[i] = intens;

            i++;

        }

        // save the entire class with the results.
        save("test.xml");

    }


    public void save(string fileName)
    {
        using (var writer = new StreamWriter(fileName))
        {   
            // EDIT: got lost during copy paste
            var serializer = new XmlSerializer(this.GetType());

            serializer.Serialize(writer, this);

            writer.Flush();
        }
    }


    public static MultiArrayList Load(string FileName)
    {
        MultiArrayList t = new MultiArrayList();

        using (var stream = File.OpenRead(FileName))     
        {
            var serializer = new XmlSerializer(typeof(MultiArrayList));
            t = serializer.Deserialize(stream) as MultiArrayList;

        }

        return t;
    }

}

This is the output that you get:

<?xml version="1.0" encoding="utf-8"?>
<MultiArrayList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MyProperty>0</MyProperty>
  <coordX>
    <float>1</float>
    <float>4</float>
    <float>7</float>
    <float>0</float>
  </coordX>
  <coordY>
    <float>2</float>
    <float>5</float>
    <float>89</float>
    <float>0</float>
  </coordY>
  <coordZ>
    <float>3</float>
    <float>6</float>
    <float>90</float>
    <float>0</float>
  </coordZ>
  <intensity>
    <float>99</float>
    <float>99</float>
    <float>99</float>
    <float>0</float>
  </intensity>
</MultiArrayList>

When you use the load method you get an Object with all the values in your arrays. Hope this can help

EDIT: to use the StreamWriter you need to include the namespace:

using System.IO;

to use the XmlSerializer you need to include the namespace:

using System.Xml.Serialization;
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • Thank you!! I'll take a look at this! – Sockness_Rogers Jun 28 '16 at 10:11
  • Also I am unfamiliar with the stream reader/writer. What assembly reference do I need to get this to work in my script?? Thank you! – Sockness_Rogers Jun 28 '16 at 10:57
  • @jrogers12 Sorry for that. I edited my answer which namespaces you have to include. – Mong Zhu Jun 28 '16 at 11:06
  • Awesome thanks! One more quick question- in serializer.Serialize (writer, this); unity does not recognize the serializer.Serialize for some reason. Do you know why this would be? – Sockness_Rogers Jun 28 '16 at 11:23
  • what exactly is the question? It means that the `serialiser` uses the `StreamWriter` to write `this` object (the current object, basically with all its public properties) to the file – Mong Zhu Jun 28 '16 at 11:25
  • May to you need to add [XmlRoot] over the class and [XmlElement] over each property like in this [example](http://stackoverflow.com/questions/17286308/how-to-write-an-xml-file-in-c-sharp-in-unity) – Mong Zhu Jun 28 '16 at 11:30
  • When I save the script I get an error message that says error CS0103: The name `serializer' does not exist in the current context. – Sockness_Rogers Jun 28 '16 at 11:30
  • damin! I deleted on line too much. MY mistake!! very sorry. I edit my answer again. You need to create of course the variable in the save method: `var serializer = new XmlSerializer(this.GetType());` – Mong Zhu Jun 28 '16 at 11:31