将数组从文本文件序列化到检查器(带有c#的Unity 5)

时间:2016-06-28 09:17:00

标签: c# arrays serialization

我正在努力保存从长文本文件中提取的数组。我使用了一个foreach循环来获取数组但是从这里开始有点迷失。我可以使用[serializedfield]在检查器中显示坐标X,Y,Z,但需要弄清楚如何从循环中保存数据。任何让我走向正确方向的建议都会很棒!
提前谢谢你。这是我的代码:

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]);
}

}

1 个答案:

答案 0 :(得分:1)

这是一个建议。您可以使用XML-Serialization将具有给定数据集的整个类保存到xml文件中。在下面的示例中,我包含了两种保存和检索数据的方法: 我使阵列变得更小,以便能够显示输出。但它应该适用于更大的数组也:) 有一点很重要:你必须将数组声明为public。否则他们不会在文件中结束。

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;
    }

}

这是你得到的输出:

<?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>

使用load方法时,您将获得一个包含数组中所有值的Object。 希望这可以帮助

编辑: 使用您需要的StreamWriter包含命名空间:

using System.IO;

使用您需要包含名称空间的XmlSerializer

using System.Xml.Serialization;