Serialize arrays from text file to inspector (Unity 5 with c#) -


i working on saving arrays have pulled long text file. used foreach loop arrays little lost on go here. can use [serializedfield] show coordinates x,y,z in inspector need figure out how save data loop. advice me in right direction awesome!
thank ahead of time. here 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 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 suggestion. can use xml-serialization save entire class given dataset xml-file. in example below included 2 methods saving , retrieving data: made arrays little smaller able show ouput. should work fine larger arrays :) 1 thing important: have declare arrays public. otherwise not end in file.

public class multiarraylist {      private int = 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 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);         = 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 entire class 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) multiarraylist;          }          return t;     }  } 

this output 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 use load method object values in arrays. hope can help

edit: use streamwriter need include namespace:

using system.io; 

to use xmlserializer need include namespace:

using system.xml.serialization; 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -