반응형

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


반응형

+ Recent posts