Tanoda
DobotXMLSerializer.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using System.Linq;
4using System.Xml;
5using UnityEngine;
6
7public class DobotXMLSerializer : MonoBehaviour
8{
9 void Start()
10 {
11 //XmlDocument save = new XmlDocument();
12 //save.Load(@"C:\Users\UnityTeam\Documents\DobotPlayback.playback");
13 //foreach (XmlNode node in save.DocumentElement.ChildNodes)
14 //{
15 // string text = node.InnerText;
16 // Debug.Log(text);
17 // foreach (XmlElement xmlElement in node)
18 // {
19 // Debug.Log(xmlElement.InnerText);
20 // }
21 //}
22 }
23
24 public static void SaveToFile(string path, SerializableVector4[] content) => SaveToFile(path, content.ToList());
25 public static void SaveToFile(string path, List<SerializableVector4> content)
26 {
27 var doc = new XmlDocument();
28 var root = doc.CreateElement("root");
29 var dobotType = doc.CreateElement("DobotType");
30 var type0 = doc.CreateElement("item_0");
31 var rowStudioVersion = doc.CreateElement("row_StudioVersion");
32 var studioVer = doc.CreateElement("item_0");
33 studioVer.InnerText = "Ver-194";
34 type0.InnerText = "Magician";
35 rowStudioVersion.AppendChild(studioVer);
36 dobotType.AppendChild(type0);
37 root.AppendChild(dobotType);
38 root.AppendChild(rowStudioVersion);
39
40 for (var i = 0; i < content.Count; i++)
41 {
42 var v = content[i];
43 var row = doc.CreateElement("row" + i);
44 var i0 = doc.CreateElement("item_0"); //MotionStyle
45 var i1 = doc.CreateElement("item_1"); //Name
46 var i2 = doc.CreateElement("item_2"); //X
47 var i3 = doc.CreateElement("item_3"); //Y
48 var i4 = doc.CreateElement("item_4"); //Z
49 var i5 = doc.CreateElement("item_5"); //R
50 var i10 = doc.CreateElement("item_10"); //PauseTime
51 var i11 = doc.CreateElement("item_11"); //Gripper
52
53 i0.InnerText = "1";
54 if (float.IsInfinity(v.x))
55 {
56 var v3 = i > 0 ? content[i - 1] : content[i + 1];
57
58 i2.InnerText = Macro.NormalizeFraction(v3.x.ToString(), '.');
59 i3.InnerText = Macro.NormalizeFraction(v3.y.ToString(), '.');
60 i4.InnerText = Macro.NormalizeFraction(v3.z.ToString(), '.');
61 i5.InnerText = Macro.NormalizeFraction(v3.w.ToString(), '.');
62 if (float.IsPositiveInfinity(v.x))
63 {
64 i11.InnerText = "2"; //Close
65 }
66 else
67 {
68 i11.InnerText = "1"; //Open
69 }
70 }
71 else
72 {
73 i2.InnerText = Macro.NormalizeFraction(v.x.ToString(), '.');
74 i3.InnerText = Macro.NormalizeFraction(v.y.ToString(), '.');
75 i4.InnerText = Macro.NormalizeFraction(v.z.ToString(), '.');
76 i5.InnerText = Macro.NormalizeFraction(v.w.ToString(), '.');
77
78 i11.InnerText = "0"; //Disable
79 }
80 i5.InnerText = "0";
81 i10.InnerText = "0.5";
82
83 row.AppendChild(i0);
84 row.AppendChild(i1);
85 row.AppendChild(i2);
86 row.AppendChild(i3);
87 row.AppendChild(i4);
88 row.AppendChild(i5);
89 row.AppendChild(i10);
90 row.AppendChild(i11);
91
92 root.AppendChild(row);
93 }
94
95 doc.AppendChild(root);
96 doc.Save(path);
97 }
98}
static void SaveToFile(string path, List< SerializableVector4 > content)
static void SaveToFile(string path, SerializableVector4[] content)
Definition: Macro.cs:12
static string NormalizeFraction(string value)
Definition: Macro.cs:164
Since unity doesn't flag the Vector4 as serializable, we need to create our own version....
Definition: SolveIK.cs:524