Exemplo n.º 1
0
    public XmlDeserialization(/*string path, */ NodeNetCreator net)
    {
        string path = EditorUtility.OpenFilePanel("Open Nodenet xml", Application.dataPath, "xml");

        if (File.Exists(path))
        {
            XmlDocument doc = new XmlDocument();
            FileStream  fs  = new FileStream(path, FileMode.Open, FileAccess.Read);

            doc.Load(fs);

            //"net" node
            XmlNode netNode = doc.ChildNodes[1];

            XmlNode nodesNode = netNode.FirstChild;

            XmlNode stretchesNode = netNode.LastChild;

            int nNodes     = nodesNode.ChildNodes.Count;
            int nStretches = stretchesNode.ChildNodes.Count;

            net.DeleteAllNodes();

            List <Stretch> stretches = new List <Stretch>(nStretches);

            //Nodes
            for (int n = 0; n < nNodes; n++)
            {
                XmlNode node = nodesNode.ChildNodes[n];

                float x = float.Parse(node.Attributes["x"].Value);
                float y = float.Parse(node.Attributes["y"].Value);
                float z = float.Parse(node.Attributes["z"].Value);

                net.CreateNode(new Vector3(x, y, z));
            }

            //Stretches
            for (int s = 0; s < nStretches; s++)
            {
                XmlNode stretch = stretchesNode.ChildNodes[s];

                int anchorA = int.Parse(stretch.Attributes["a"].Value);
                int anchorB = int.Parse(stretch.Attributes["b"].Value);

                Vector3 controlARelativePos;
                Vector3 controlBRelativePos;

                //ControlA
                {
                    XmlNode controlA = stretch.ChildNodes[0];

                    float x = float.Parse(controlA.Attributes["x"].Value);
                    float y = float.Parse(controlA.Attributes["y"].Value);
                    float z = float.Parse(controlA.Attributes["z"].Value);

                    controlARelativePos = new Vector3(x, y, z);
                }
                //ControlB
                {
                    XmlNode controlB = stretch.ChildNodes[1];

                    float x = float.Parse(controlB.Attributes["x"].Value);
                    float y = float.Parse(controlB.Attributes["y"].Value);
                    float z = float.Parse(controlB.Attributes["z"].Value);

                    controlBRelativePos = new Vector3(x, y, z);
                }

                stretches.Add(new Stretch(anchorA, anchorB, controlARelativePos, controlBRelativePos));
            }

            net.stretches = stretches;

            fs.Close();
        }
    }