Here, we use XML to implement save and read functions.
First, a reference to the header file:
Using System.Xml;
Save to XML file:
void Save () {string filePath = Application.datapath + @ "/my.xml"; XmlDocument xmldoc = new XmlDocument (); XmlElement root = xmldoc.createelement ("root"); for (int i = 0; i < options. Count; i++) {XmlElement child = xmldoc.createelement ("Child"); XmlElement x = xmldoc.createelement ("type"); X.innertext = Options[i]. ToString (); Child. AppendChild (x); int count = 1; for (int q = 0; q < strings[q]. Length; q++) {XmlElement q1 = xmldoc.createelement ("string_" + count); Q1. InnerText = Strings[i][q]; Child. AppendChild (Q1); count++; } for (int q = 0; q < ints[q]. Length; q++) {XmlElement q1 = xmldoc.createelement ("Int_" + count); Q1. InnerText = Ints[i][q]. ToString (); Child. AppendChild (Q1); count++; } for (int q = 0; q < floats[q]. Length; q++) {XmlElement q1 = xmldoc.createelement ("Float_" + count); Q1. InnerText = Floats[i][q]. ToString (); Child. AppendChild (Q1); count++; } for (int q = 0; q < vector3s[q]. Length; q++) {XmlElement q1 = xmldoc.createelement ("vector3_" + count); Q1. InnerText = Vector3s[i][q]. ToString (); Child. AppendChild (Q1); count++; } root. AppendChild (child); } xmldoc.appendchild (root); Xmldoc.save (FilePath); Assetdatabase.refresh (); Debug.Log ("ok!!"); }
To read an XML file:
void Load (String xmlpath) {XmlDocument xmldoc = new XmlDocument (); Xmldoc.load (Xmlpath); XmlNodeList Childs = Xmldoc.selectsinglenode ("root"). childnodes;//all child for (int i = 0; i < Childs. Count; i++) {XmlNodeList nodes = Childs[i]. childnodes;//all nodes of a single child string a = Nodes[0]. InnerText; Switch (a) {case "commanddestory": Commands. ADD (New Commanddestory (nodes[1). InnerText)); Break Case "Commanddialogue": int int1 = Int. Parse (Nodes[6]. InnerText); int int2 = Int. Parse (Nodes[7]. InnerText); int int3 = Int. Parse (Nodes[8]. InnerText); Commands. ADD (New Commanddialogue (nodes[1). InnerText, Int1, Int2, Int3)); Break Case "Commanddialoguemodeenter": Commands. ADD (New Commanddialoguemodeenter ()); Break Case "Commanddialoguemodeexit": Commands. ADD (New Commanddialoguemodeexit ()); Break Case "Commandgenerate": Vector3 v1 = StringToVector3 (nodes[16]. InnerText); Vector3 v2 = StringToVector3 (nodes[17]. InnerText); Commands. ADD (New Commandgenerate (nodes[1). INNERTEXT,V1,V2)); Break Case "Commandmove": Vector3 v = StringToVector3 (nodes[16]. InnerText); Commands. ADD (New Commandmove (nodes[1). InnerText, V, float. Parse (nodes[11]. (InnerText))); Break Case "Commandmusic": Commands. ADD (New Commandmusic (nodes[1). InnerText)); Break Case "Commandplayanim": Commands. ADD (New Commandplayanim (nodes[1). InnerText, Int. Parse (Nodes[6]. (InnerText))); Break Case"Commandrotate": Vector3 v3 = StringToVector3 (nodes[16]. InnerText); Commands. ADD (New Commandrotate (nodes[1). InnerText, v3, float. Parse (nodes[11]. (InnerText))); Break Case "commandwait": Commands. ADD (new commandwait (float). Parse (nodes[11]. (InnerText))); Break Default:break; }}} Vector3 StringToVector3 (string s) {s = s.substring (1,s.length-2); String[] A = S.split (', '); Vector3 B = new Vector3 (float. Parse (a[0]), float. Parse (a[1]), float. Parse (a[2])); return b; }
The contents of a command node in the XML file:
As you can see, the node names are labeled so that it's easy to get the data we want when it's read.
In fact, do the editor tool is a very good way to speed up the efficiency, because life, the program is mainly responsible for the program, planning is mainly to get the table and so on, so that the program can be part of the work to the planning to get, planning just use these tools in the editor, you can achieve the same function, is a win-ha.
[Unity Combat] Drama Dialogue (iii)