차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판이전 판다음 판 | 이전 판 | ||
unity:xml [2015/05/23 16:28] – V_L | unity:xml [2018/02/22 03:04] (현재) – 바깥 편집 127.0.0.1 | ||
---|---|---|---|
줄 1: | 줄 1: | ||
+ | {{tag> | ||
====== 게임내부의 설정 수치를 XML로 불러오기 | ====== 게임내부의 설정 수치를 XML로 불러오기 | ||
- | |||
http:// | http:// | ||
+ | |||
+ | |||
+ | SQLite는 프로버전만 가능하므로 XML을 사용한다. | ||
게임에서 사용하는 각종 수치를 하드코딩하지 않고 XML로 불러와서 적용하기 위해서 만듦. | 게임에서 사용하는 각종 수치를 하드코딩하지 않고 XML로 불러와서 적용하기 위해서 만듦. | ||
- | 기획자가 만든 수치를 게임에 적용시키기 위해 배워야 | + | 기획자가 만든 수치를 게임에 적용시키기 위해 배워야 |
- | 게임 상태를 저장하거나 불러오는 것을 원하는 분들이 있다면 PlayerPrefs를 참조 해주시기 바랍니다. | + | 게임 상태를 저장하거나 불러오는 것을 원하는 분들이 있다면 PlayerPrefs를 참조 해주시기 바란다. |
+ | |||
+ | 유니티로 맵데이터나 기본 케릭터 정보를 저장할때 XML을 사용한다. | ||
+ | |||
+ | 본 자료는 인터넷에 있는 자료를 토대로 정리되었다. | ||
줄 14: | 줄 21: | ||
필수는 아니지만 각 함수를 사용할 때 System.Xml을 쓰는건 코드 효율이 떨어지므로 using해둡니다. | 필수는 아니지만 각 함수를 사용할 때 System.Xml을 쓰는건 코드 효율이 떨어지므로 using해둡니다. | ||
- | System.Xml.XmlDocument Document = new System.Xml.XmlDocument(); | + | System.Xml.XmlDocument Document = new System.Xml.XmlDocument(); |
새로운 XML 문서를 만듭니다. | 새로운 XML 문서를 만듭니다. | ||
XmlElement myElement = Document.CreateElement(" | XmlElement myElement = Document.CreateElement(" | ||
- | 새로운 요소를 만듭니다. Element는 요소, 성분이라는 뜻입니다. | + | 새로운 요소를 만듭니다. Element는 요소, 성분이라는 뜻이다. |
- | myElement.SetAttribute(" | + | myElement.SetAttribute(" |
- | 요소에 속성을 지정합니다. 속성의 이름, 속성의 값이 들어갑니다. | + | 요소에 속성을 지정한다. 속성의 이름, 속성의 값이 들어간다. |
- | ItemElement.GetAttribute(" | + | ItemElement.GetAttribute(" |
- | 요소로부터 값을 가져옵니다. 반환된 값은 문자열 | + | 요소로부터 값을 가져온다. 반환된 값은 문자열 |
Document.AppendChild(ItemListElement); | Document.AppendChild(ItemListElement); | ||
- | Document에 첨부 | + | Document에 첨부 |
Document.Save(filePath); | Document.Save(filePath); | ||
- | XML Document를 저장합니다. | + | XML Document를 저장한다. |
=====XmlDocument 불러오기===== | =====XmlDocument 불러오기===== | ||
- | XmlDocument Document = new XmlDocument(); | + | XmlDocument Document = new XmlDocument(); |
새로운 XML 문서를 만듭니다. | 새로운 XML 문서를 만듭니다. | ||
Document.Load(filePath); | Document.Load(filePath); | ||
- | XML Document를 불러옵니다. | + | XML Document를 불러온다. |
줄 71: | 줄 76: | ||
foreach(RecItem Item in ItemList) | foreach(RecItem Item in ItemList) | ||
{ | { | ||
- | XmlElement ItemElement = Document.CreateElement(" | + | XmlElement ItemElement = Document.CreateElement(" |
- | ItemElement.SetAttribute(" | + | ItemElement.SetAttribute(" |
- | ItemElement.SetAttribute(" | + | ItemElement.SetAttribute(" |
- | ItemElement.SetAttribute(" | + | ItemElement.SetAttribute(" |
ItemListElement.AppendChild(ItemElement); | ItemListElement.AppendChild(ItemElement); | ||
} | } | ||
줄 82: | 줄 87: | ||
public static List< | public static List< | ||
{ | { | ||
- | XmlDocument Document = new XmlDocument(); | + | XmlDocument Document = new XmlDocument(); |
- | Document.Load(filePath); | + | Document.Load(filePath); |
- | XmlElement ItemListElement = Document[" | + | XmlElement ItemListElement = Document[" |
List< | List< | ||
foreach(XmlElement ItemElement in ItemListElement.ChildNodes) | foreach(XmlElement ItemElement in ItemListElement.ChildNodes) | ||
- | { | + | { |
RecItem Item = new RecItem(); | RecItem Item = new RecItem(); | ||
- | Item.Name = ItemElement.GetAttribute(" | + | Item.Name = ItemElement.GetAttribute(" |
Item.Level = System.Convert.ToInt32(ItemElement.GetAttribute(" | Item.Level = System.Convert.ToInt32(ItemElement.GetAttribute(" | ||
Item.Critical = System.Convert.ToSingle(ItemElement.GetAttribute(" | Item.Critical = System.Convert.ToSingle(ItemElement.GetAttribute(" | ||
줄 101: | 줄 106: | ||
</ | </ | ||
- | RecItem이라는 클래스 자료를 이용하여 저장 및 불러오기를 만들었습니다. | + | RecItem이라는 클래스 자료를 이용하여 저장 및 불러오기를 만들었다. |
=====저장===== | =====저장===== | ||
- | " | + | " |
- | List로 받은 데이터를 itemList에 갯수만큼 넣을 준비를 | + | List로 받은 데이터를 itemList에 갯수만큼 넣을 준비를 |
요소 " | 요소 " | ||
- | 값은 문자열로만 저장이 가능하므로 ToString()을 사용합니다. 숫자로 된 자료는 이런식으로 저장합니다. | + | 값은 문자열로만 저장이 가능하므로 ToString()을 사용한다. 숫자로 된 자료는 이런식으로 저장한다. |
- | 그리고 완성이 된 아이템은 " | + | 그리고 완성이 된 아이템은 " |
- | for문이 끝나고 나면 저장 | + | for문이 끝나고 나면 저장 |
사용의 예) | 사용의 예) | ||
줄 118: | 줄 123: | ||
public class WriteTest : MonoBehaviour | public class WriteTest : MonoBehaviour | ||
- | { | + | { |
void Start() | void Start() | ||
{ | { | ||
줄 138: | 줄 143: | ||
=====불러오기===== | =====불러오기===== | ||
- | XML형식으로 저장된 파일을 불러옵니다. | + | XML형식으로 저장된 파일을 불러온다. |
- | 불러온 문서의 " | + | 불러온 문서의 " |
RecItem 자료형 리스트를 만듭니다. | RecItem 자료형 리스트를 만듭니다. | ||
- | 이제 for문을 돌면서 XML Document로부터 RecItem List로 값을 가져 | + | 이제 for문을 돌면서 XML Document로부터 RecItem List로 값을 가져 |
- | 문자열이 아닌 데이터는 System.Convert.ToInt32 , System.Convert.ToSingle 를 이용하여 변환합니다. (정수, 실수) | + | 문자열이 아닌 데이터는 System.Convert.ToInt32 , System.Convert.ToSingle 를 이용하여 변환한다. (정수, 실수) |
사용의 예) | 사용의 예) | ||
줄 158: | 줄 163: | ||
{ | { | ||
RecItem item = itemList[i]; | RecItem item = itemList[i]; | ||
- | Debug.Log(string.Format(" | + | Debug.Log(string.Format(" |
i, item.Name, item.Level, item.Critical, | i, item.Name, item.Level, item.Critical, | ||
} | } | ||
줄 170: | 줄 175: | ||
- | |||
- | {{tag> |