본문 바로가기
Unity3D

XML Parser Sample Code

by CodeDiver 2013. 10. 11.

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

<draw_correct>

<stage num ="1" filename="char_25" text="가">

 <pos id="1" x="-17.8" y="13.9"/>

 <pos id="2" x="-2.3" y="14.0"/>

 <pos id="3" x="-17.5" y="-10.0"/>

 <pos id="4" x="10.6" y="17.6"/>

 <pos id="5" x="11.0" y="-18.2"/>

 <pos id="6" x="19.2" y="1.3"/>

</stage>

<stage num ="2" filename="char_26" text="나">

 <pos id="1" x="-16.8" y="14.8"/>

 <pos id="2" x="-16.8" y="-7.3"/>

 <pos id="3" x="0.86" y="-7.3"/>

 <pos id="4" x="10.4" y="16.8"/>

 <pos id="5" x="10.4" y="-16.4"/>

 <pos id="6" x="20.4" y="1.6"/>

</stage>

</draw_correct>






using UnityEngine;

using System.Collections;

using System.Xml;

using System.Collections.Generic;


public class GHXMLParser {

private PositionData data;

public List<PositionData> loadXML() {

List<PositionData> listStage = new List<PositionData>();

TextAsset xmlData = new TextAsset();

       xmlData = (TextAsset)Resources.Load("xml/draw_correct", typeof(TextAsset));

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.LoadXml(xmlData.text);

XmlElement root = xmlDoc.DocumentElement;

XmlNodeList nodes = root.ChildNodes;

foreach (XmlNode node in nodes) {


// Stage 정보를 읽어서 리스트에 넣는다.

PositionData data = new PositionData();

data.StageNum = int.Parse(node.Attributes["num"].Value);

data.fileName = node.Attributes["filename"].Value;

data.text = node.Attributes["text"].Value;


XmlNodeList posList = node.ChildNodes;


List<Vector3> listPos = new List<Vector3>();


// 포지션 정보를 읽어서 리스트에 넣는다.

foreach (XmlNode nodePos in posList) {

string id = nodePos.Attributes["id"].Value;

string posX = nodePos.Attributes["x"].Value;

string posY = nodePos.Attributes["y"].Value;

//Debug.Log ("x: " + posX + ", y: " + posY);

listPos.Add(new Vector3(float.Parse(posX), float.Parse(posY), 0));

}

data.listPos = listPos;

listStage.Add(data);

}


Debug.Log ("listStage Count: " + listStage.Count);

return listStage;

}

}


<소스+XML 첨부>


Unity_XML_Parser_Sample.zip




'Unity3D' 카테고리의 다른 글

[Script] Raycast를 이용한 Picking  (0) 2013.10.14
[Script] Audio 재생하기  (0) 2013.10.11
Child Object의 Component 접근하기  (0) 2013.10.11
터치한 위치에 Image 표시하기  (0) 2013.10.10
Unity3D 공부 중.  (0) 2013.10.03