회원 로그인
|
XML 파싱 (with Unity)
XML 파싱
:유니티에서 파싱하여 사용
-UTF-8로 꼭 저장
-요소들의 소대문자 구분
-Resources 폴더에서 관리하면 효율적
:유니티에서 파싱하여 사용
-UTF-8로 꼭 저장
-요소들의 소대문자 구분
-Resources 폴더에서 관리하면 효율적
-xml 파싱 / 저장 / 로드
-Server에서 받기
-배경 배치된 것을 저장하여 재사용 가능 / 업데이트 유용
-xml 이외에 txt 파일도 파싱
아래에서 만든 맵 에디터의 정보를 xml 저장
XML에 저장해야 할 목록
1. width : 2, height : 2
2. style 정보(색상)
3. id 정보(오브젝트의 이름, x_y)
MapData01.xml
아래에서 만든 맵 에디터의 정보를 xml 저장
XML에 저장해야 할 목록
1. width : 2, height : 2
2. style 정보(색상)
3. id 정보(오브젝트의 이름, x_y)
MapData01.xml
파싱0 0 0 1 0 0 0 1 0 1 1 0
using UnityEngine; using System.Collections; using System.Collections.Generic; // xml, save, load 관련 클래스 using System.Xml; using System.Xml.Serialization; using System.Text; // save, load using System.IO; // cube 정보 public class Cube { [XmlElement("x")] public int x = 0; [XmlElement("y")] public int y = 0; [XmlElement("z")] public int z = 0; [XmlElement("style")] public int style = 0; } [XmlRoot("mapData")] public class MapData { [XmlIgnore] // list 변수는 xml 형식에 사용을 하지 않는다. List<Cube> list = null; [XmlElement("cube")] public Cube[] cubeList { get { // list 담긴 값을 배열로 넘겨준다. return list.ToArray(); } set // set 구현시 xml 파일을 로드 후 여기에 Cube 넣어준다. { // 배열로 넘겨준 데이터(Cube 배열)를 list로 담음 list = new List<Cube>(value); } } } public class XMLManager : MonoBehaviour { void Start () { // xml 불러오기 용도 // TextAsset => 텍스트 파일 관리하는 클래스 TextAsset textAsset = Resources.Load("MapData/MapData01") as TextAsset; print(textAsset.text); // load된 텍스트 파일 출력 // XmlSerializer() 형식을 관리 XmlSerializer ser = new XmlSerializer(typeof(MapData)); // Deserialize() xml -> class 형식으로 변환 MapData mapData = ser.Deserialize(new StringReader(textAsset.text)) as MapData; foreach(Cube cube in mapData.cubeList) { print("x " + cube.x); print("y " + cube.y); print("z " + cube.z); print("style " + cube.style); } } }xml 저장
// 중략 List<Cube> list = new List<Cube>(); // 게임오브젝트 Cube 정보를 담을 List 공간 // MapManager/child의 게임오브젝트(cube들)의 정보를 담음 int childCount = mapManager.transform.childCount; for(int i = 0; i < childCount; i++) { Transform childTran = mapManager.transform.GetChild(i); int x = (int)childTran.position.x; int y = (int)childTran.position.y; int z = (int)childTran.position.z; int style = (int)childTran.gameObject.GetComponent<TileInfo>()._style; // style 얻어 오기 Cube cube = new Cube(); cube.x = x; cube.y = y; cube.z = z; cube.style = style; list.Add(cube); // list cube 데이터를 임시 보관 (xml에 옮기기 위해) } // xml저장하고자하는 데이터를 mapData에 담는다. (class 생성) MapData mapData = new MapData(); mapData.cubeList = list.ToArray(); // 임시 보관한 list를 cubeList에 넣음 // 저장 형식 지정 XmlSerializer ser = new XmlSerializer(typeof(MapData)); // StreamWriter(저장할 경로) : 저장 공간 확보(데이터를 차곡차곡 확보) TextWriter textWriter = new StreamWriter(path); // xml 파일 저장 ser.Serialize(textWriter, mapData); // class -> xml 변환(저장) // 중략
트러블슈팅 XmlException: Text node cannot appear in this state. 해결
XmlException: Whitespace is required between PUBLIC id and SYSTEM id. 해결
|
- MapData01.xml (465B) (0)
댓글 0개
| 엮인글 0개
285개(5/15페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
205 | [Python] Anaconda 사용 | 푸딩뱃살 | 3412 | 2017.08.28 17:51 |
204 | [Python] url 인코딩/디코딩 | 푸딩뱃살 | 6312 | 2017.08.12 19:23 |
203 | [Python] Qt5(ui) to py | 푸딩뱃살 | 2622 | 2017.07.04 15:29 |
202 | [VisualStudio] Visual Studio Community 2015 설치 파일 (다운로드) [1+1] | 푸딩뱃살 | 4497 | 2017.06.11 20:11 |
201 | [VisualStudio] Visual Studio Community 2015 삭제 | 푸딩뱃살 | 4270 | 2017.06.11 20:06 |
200 | [Python] PyQt4 to PyQt5 | 푸딩뱃살 | 2968 | 2017.06.02 16:50 |
199 | [Python] NumPy 라이브러리 설치 | 푸딩뱃살 | 3342 | 2017.05.18 15:14 |
198 | [Python] Python Decompiler | 푸딩뱃살 | 3934 | 2017.04.28 14:28 |
197 | [Python] 암호화 ASE | 푸딩뱃살 | 3426 | 2017.01.09 00:10 |
196 | [Python] 모듈 with xbox 360 controller | 푸딩뱃살 | 2097 | 2016.09.18 22:03 |
195 | [PHP] php 메모리 부족 (PHP Fatal error) | 푸딩뱃살 | 4300 | 2016.09.10 21:07 |
194 | [C#] 조건연산자 ?: | 푸딩뱃살 | 2937 | 2016.09.06 13:46 |
193 | [C#] object 형 변환 (with Unity) [1+1] | 푸딩뱃살 | 10529 | 2016.05.04 02:20 |
192 | [C#] 현재 시간 | 푸딩뱃살 | 3029 | 2016.02.28 23:23 |
191 | [C#] 문자열 Format | 푸딩뱃살 | 2461 | 2016.01.21 12:10 |
>> | [XML] XML 파싱 (with Unity) | 푸딩뱃살 | 7032 | 2016.01.20 10:33 |
189 | [PHP] PHP 전송 종류 | 푸딩뱃살 | 2404 | 2016.01.11 10:49 |
188 | [C#] 배열 | 푸딩뱃살 | 1199 | 2016.01.05 14:38 |
187 | [C#] 참조 ref, out (with Unity) | 푸딩뱃살 | 6068 | 2015.12.08 11:22 |
186 | [PHP] CodeIgniter(코드이그나이트) 사용 | 푸딩뱃살 | 11874 | 2015.12.06 17:46 |