- Home /
Question by
crazyKnight · May 18, 2012 at 06:16 AM ·
assetxmlresourcesbuilds
Load xml from resource folder after build(web player)
Greetings,
i am trying to store my character coordinates to a xml file, i am able to do it in the editor but when i build the project i am not able to load the xml heres my code to read and write to xml
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
public class XmlEditor : MonoBehaviour
{
public static void WriteToXml(string elementName,Vector3 pos)
{
string X = pos.x.ToString();
string Y = pos.y.ToString();
string Z = pos.z.ToString();
string filepath = Application.dataPath + @"/Resources/Data.xml";
XmlDocument xmlDoc = new XmlDocument();
if(File.Exists (filepath))
{
xmlDoc.Load(filepath);
XmlElement elmRoot = xmlDoc.DocumentElement;
//elmRoot.RemoveAll(); // clear all inside the transforms node.
XmlNodeList transformList = xmlDoc.GetElementsByTagName(elementName);
for(int i = 0; i < transformList.Count ; i++)
{
transformList[i].ParentNode.RemoveChild(transformList[i]);
}
XmlElement elmNew = xmlDoc.CreateElement(elementName); // create the rotation node.
XmlElement rotation_X = xmlDoc.CreateElement("x"); // create the x node.
rotation_X.InnerText = X; // apply to the node text the values of the variable.
XmlElement rotation_Y = xmlDoc.CreateElement("y"); // create the y node.
rotation_Y.InnerText = Y; // apply to the node text the values of the variable.
XmlElement rotation_Z = xmlDoc.CreateElement("z"); // create the z node.
rotation_Z.InnerText = Z; // apply to the node text the values of the variable.
elmNew.AppendChild(rotation_X); // make the rotation node the parent.
elmNew.AppendChild(rotation_Y); // make the rotation node the parent.
elmNew.AppendChild(rotation_Z); // make the rotation node the parent.
elmRoot.AppendChild(elmNew); // make the transform node the parent.
xmlDoc.Save(filepath); // save file.
}
}
public static Vector3 LoadFromXml(string elementName)
{
float X = 0;
float Y = 0;
float Z = 0;
string filepath = Application.dataPath + @"/Resources/Data.xml";
XmlDocument xmlDoc = new XmlDocument();
if(File.Exists (filepath))
{
xmlDoc.Load(filepath);
XmlNodeList transformList = xmlDoc.GetElementsByTagName(elementName);
foreach (XmlNode transformInfo in transformList)
{
XmlNodeList transformcontent = transformInfo.ChildNodes;
foreach (XmlNode transformItens in transformcontent)
{
if(transformItens.Name == "x")
{
X = float.Parse(transformItens.InnerText); // convert the strings to float and apply to the X variable.
}
if(transformItens.Name == "y")
{
Y = float.Parse(transformItens.InnerText); // convert the strings to float and apply to the Y variable.
}
if(transformItens.Name == "z")
{
Z = float.Parse(transformItens.InnerText); // convert the strings to float and apply to the Z variable.
}
}
}
}
return new Vector3(X,Y,Z);;
}
}
can anyone help me out...
Thanks
Comment