Programming/Game Engine

[Unity] Unity 에서 xml 사용

JMob 2017. 9. 2. 14:41
728x90
반응형


Unity에서 XML로 데이더를 저장하고 읽는 방법


※ Android에서 xml을 사용하기 위해서는 filePath를 "Application.persistentDataPath" 이후에 해야합니다.


public class XmlFile : MonoBehaviour {



    public static void Write_List()

    {

        string filePath;


#if UNITY_ANDROID

        filePath = Application.persistentDataPath + "/Person.xml";

#endif


#if UNITY_STANDALONE_WIN

        filePath = Application.dataPath + "/Person.xml";

#endif

        XmlDocument Document = new XmlDocument();

        XmlDeclaration xmlDeclaration = Document.CreateXmlDeclaration("1.0", "UTF-8", null);

        Document.AppendChild(xmlDeclaration);


        XmlElement ItemListElement = Document.CreateElement("List");

        Document.AppendChild(ItemListElement);


            

        for (int i = 0 ; i < 10; i++ )

        {

            XmlElement ItemElement = (XmlElement)ItemListElement.AppendChild(Document.CreateElement("Person"));

            ItemElement.SetAttribute("Name", "Person Name " + i.ToString() );

            ItemElement.SetAttribute("Age", (i*10).ToString() );

        }

        // time 저장

       


        XmlElement ItemElement2 = (XmlElement)ItemListElement.AppendChild(Document.CreateElement("time"));

        ItemElement2.SetAttribute("minute", "10");

        ItemElement2.SetAttribute("second", "30");


        Document.Save(filePath);

    }


    public static void Read_List()

    {

        string filePath;

#if UNITY_ANDROID

        filePath = Application.persistentDataPath + "/Person.xml";

#endif


#if UNITY_STANDALONE_WIN

        filePath = Application.dataPath + "/Person.xml";

#endif

        XmlDocument Document = new XmlDocument();

        Document.Load(filePath);

        XmlElement ItemListElement = Document["List"];


        

        foreach (XmlElement ItemElement in ItemListElement.ChildNodes)

        {

            if (ItemElement.Name.Equals("Person"))

            {

                string name = ItemElement.GetAttribute("Name");

                string age = ItemElement.GetAttribute("Age");

                Debug.Log("Name : " + name + ", Age :" + age);

            }

            else if (ItemElement.Name.Equals("time"))

            {

                int minute, second = 0;

                minute = System.Convert.ToInt32(ItemElement.GetAttribute("minute"));

                second = System.Convert.ToInt32(ItemElement.GetAttribute("second"));

                Debug.Log("minute : " + minute + ", second :" + second);

            }

        }



    }



Wirte_List()를 수행한 결과


<?xml version="1.0" encoding="UTF-8"?>

<List>

  <Person Name="Person Name 0" Age="0" />

  <Person Name="Person Name 1" Age="10" />

  <Person Name="Person Name 2" Age="20" />

  <Person Name="Person Name 3" Age="30" />

  <Person Name="Person Name 4" Age="40" />

  <Person Name="Person Name 5" Age="50" />

  <Person Name="Person Name 6" Age="60" />

  <Person Name="Person Name 7" Age="70" />

  <Person Name="Person Name 8" Age="80" />

  <Person Name="Person Name 9" Age="90" />

  <time minute="10" second="30" />

</List> 



728x90
반응형