반응형
using UnityEngine;
using System;
using System.Xml;
using System.IO;
[ExecuteInEditMode]
[Serializable]
public static class RyXmlTools
{
public static XmlDocument loadXml(TextAsset xmlFile)
{
MemoryStream assetStream = new MemoryStream(xmlFile.bytes);
XmlReader reader = XmlReader.Create(assetStream);
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(reader);
}
catch (Exception ex)
{
Debug.Log("Error loading "+ xmlFile.name + ":\n" + ex);
}
finally
{
Debug.Log(xmlFile.name + " loaded");
}
return xmlDoc;
}
public static void writeXml(string filepath, XmlDocument xmlDoc)
{
if (File.Exists(filepath))
{
using (TextWriter sw = new StreamWriter(filepath, false, System.Text.Encoding.UTF8)) //Set encoding
{
xmlDoc.Save(sw);
}
}
}
}
사용 방법 :
읽어오기 방법
TextAsset m_XmlTextAsset = (TextAsset)Resources.Load("FileNameWhitoutFileExtention", typeof(TextAsset));
XmlDocument m_xmlDoc = RyXmlTools.loadXml(m_XmlTextAsset);
저장하기
private static string m_XmlTextAssetPath = Application.dataPath.ToString() + "/resources/FileName.xml"; RyXmlTools.writeXml(m_XmlTextAssetPath , m_xmlDoc);
아래 코드는 Ressources.Load() 함수를 이용한 로드 방법이다.
TextAsset textAsset = (TextAsset) Resources.Load("MyXMLFile");
XmlDocument xmldoc = new XmlDocument ();
xmldoc.LoadXml ( textAsset.text );
참고 사이트 : http://answers.unity3d.com/questions/59466/loading-xml-file-from-resources-file-after-build.html
반응형
'개발 Tip > Unity3D' 카테고리의 다른 글
| Unity3D NGUI에서 라벨에 한글 적용하기 (0) | 2013.03.20 |
|---|---|
| Unity3D 4.0 으로 버전업 하면서 경고 메세지 중 하나! (0) | 2013.02.27 |
| Unity3D에서 XML 읽고 쓰기 (0) | 2013.02.21 |
| Unity3D에서 바이러니 데이터를 읽어오기 (9) | 2013.02.20 |
| Unity3D 해상도 1:1 매칭을 위한 카메라 값 정하기 (0) | 2013.01.24 |