简体   繁体   English

将数组从文本文件序列化到检查器(使用C#的Unity 5)

[英]Serialize arrays from text file to inspector (Unity 5 with c#)

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. 我使用了一个foreach循环来获取数组,但是对于从这里去哪里却有些迷失。 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. 我可以使用[serializedfield]在检查器中显示坐标X,Y,Z,但需要弄清楚如何从循环中保存数据。 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]);
}

}

Here is a suggestion. 这是一个建议。 You can use XML-Serialization to save the entire class with a given dataset to a xml-file. 您可以使用XML-Serialization将具有给定数据集的整个类保存到xml文件中。 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. 当使用load方法时,您将获得一个包含数组中所有值的Object。 Hope this can help 希望这可以帮助

EDIT: to use the StreamWriter you need to include the namespace: 编辑:要使用StreamWriter您需要包括名称空间:

using System.IO;

to use the XmlSerializer you need to include the namespace: 要使用XmlSerializer您需要包括名称空间:

using System.Xml.Serialization;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM