this post shows asimple tutorial to do some basic operations with xml file on Unity3D using C#.
How to load?
Put an XML file on Resources folder and load itwith Resources class from Unity3D
xml sample:
<achievement>
<uniqueid>0</uniqueid>
<referencename>MyAchievement</referencename>
<goal>Solve until 10seconds</goal>
<points>10</points>
</achievement>
using UnityEngine;
using System.Collections;
using System.Xml;
public class XMLEditor : MonoBehaviour
{
void Awake()
{
//Load
TextAsset textXML = (TextAsset)Resources.Load("myxml.xml",typeof(TextAsset));
XmlDocument xml = new XmlDocument();
xml.LoadXml(textXML.text);
}
}
Yeah! Using a TextAsset you can make it very simply!
How to read?
Pretty simple! Use .NET framework, XmlDocumentwill turn you live more easy.
using UnityEngine;
using System.Collections;
using System.Xml;
public class XMLEditor : MonoBehaviour
{
void Awake()
{
//Load
TextAsset textXML = (TextAsset)Resources.Load("myxml.xml",typeof(TextAsset));
XmlDocument xml = new XmlDocument();
xml.LoadXml(textXML.text);
//Read
XmlNode root = xml.FirstChild;
foreach(XmlNode node in root.ChildNodes)
{
if (node.FirstChild.NodeType == XmlNodeType.Text)
Debug.Log(node.InnerText);
}
}
}
Ok, but if there are multiplehierarchies you will must implement your own ReadMethod to do that bynavigating for all nodes.
How to save?
using UnityEngine;
using System.Collections;
using System.Xml;
public class XMLEditor : MonoBehaviour
{
void Awake()
{
//Load
TextAsset textXML = (TextAsset)Resources.Load("myxml.xml",typeof(TextAsset));
XmlDocument xml = new XmlDocument();
xml.LoadXml(textXML.text);
//Read
XmlNode root = xml.FirstChild;
foreach(XmlNode node in root.ChildNodes)
{
if (node.FirstChild.NodeType == XmlNodeType.Text)
node.InnerText ="none";
}
//Simple Save
xml.Save(AssetDatabase.GetAssetPath(textXML));
}
}
The easy way! :)
출처 : http://fernandogamedev-en.blogspot.kr/2012/09/how-to-load-read-and-save-xml-on-unity3d.html
'프로그래밍 > Unity3D' 카테고리의 다른 글
[Unity3D] 플랫폼별 각 경로들 (0) | 2016.02.22 |
---|---|
[Unity3D] 파일 입출력 기본 (0) | 2016.02.22 |
[Unity3D] 멀티터치를 이용하여 줌인 줌아웃 구현 (0) | 2016.02.22 |
[Unity3D] 마우스 휠 적용법 (2) | 2016.02.22 |
[Unity3D] 플랫폼별 의존 컴파일 방법 (0) | 2016.02.22 |