찾기
내용으로 건너뛰기
추적
문서의 이전 판을 선택했습니다!
저장하면 이 자료로 새 판을 만듭니다.
미디어 파일
====== 게임내부의 설정 수치를 XML로 불러오기 ====== http://forum.unity3d.com/threads/xml-reading-a-xml-file-in-unity-how-to-do-it.44441/ 게임에서 사용하는 각종 수치를 하드코딩하지 않고 XML로 불러와서 적용하기 위해서 만듦. 기획자가 만든 수치를 게임에 적용시키기 위해 배워야 함. 게임 상태를 저장하거나 불러오는 것을 원하는 분들이 있다면 PlayerPrefs를 참조 해주시기 바랍니다. =====XmlDocument 저장===== using System.Xml; 필수는 아니지만 각 함수를 사용할 때 System.Xml을 쓰는건 코드 효율이 떨어지므로 using해둡니다. System.Xml.XmlDocument Document = new System.Xml.XmlDocument(); 새로운 XML 문서를 만듭니다. XmlElement myElement = Document.CreateElement("Item"); 새로운 요소를 만듭니다. Element는 요소, 성분이라는 뜻입니다. myElement.SetAttribute("Name", "아이템"); 요소에 속성을 지정합니다. 속성의 이름, 속성의 값이 들어갑니다. ItemElement.GetAttribute("Name"); 요소로부터 값을 가져옵니다. 반환된 값은 문자열 입니다. Document.AppendChild(ItemListElement); Document에 첨부 합니다. Append는 첨부 라는 뜻입니다. Document.Save(filePath); XML Document를 저장합니다. =====XmlDocument 불러오기===== XmlDocument Document = new XmlDocument(); 새로운 XML 문서를 만듭니다. Document.Load(filePath); XML Document를 불러옵니다. =====완성 코드 ( 저장, 불러오기 )===== <file cs> using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Xml; public class RecItem { public string Name; public int Level; public float Critical; } public sealed class ItemIO { public static void Write(List<RecItem> ItemList, string filePath) { XmlDocument Document = new XmlDocument(); XmlElement ItemListElement = Document.CreateElement("ItemList"); Document.AppendChild(ItemListElement); foreach(RecItem Item in ItemList) { XmlElement ItemElement = Document.CreateElement("Item"); ItemElement.SetAttribute("Name", Item.Name); ItemElement.SetAttribute("Level", Item.Level.ToString()); ItemElement.SetAttribute("Critical", Item.Critical.ToString()); ItemListElement.AppendChild(ItemElement); } Document.Save(filePath); } public static List<RecItem> Read(string filePath) { XmlDocument Document = new XmlDocument(); Document.Load(filePath); XmlElement ItemListElement = Document["ItemList"]; List<RecItem> ItemList = new List<RecItem>(); foreach(XmlElement ItemElement in ItemListElement.ChildNodes) { RecItem Item = new RecItem(); Item.Name = ItemElement.GetAttribute("Name"); Item.Level = System.Convert.ToInt32(ItemElement.GetAttribute("Level")); Item.Critical = System.Convert.ToSingle(ItemElement.GetAttribute("Critical")); ItemList.Add(Item); } return ItemList; } } </file> RecItem이라는 클래스 자료를 이용하여 저장 및 불러오기를 만들었습니다. =====저장===== "ItemList" 요소를 만들고 XML Document에 넣었습니다. List로 받은 데이터를 itemList에 갯수만큼 넣을 준비를 합니다. 요소 "Item" 하나 만듭니다. 값은 문자열로만 저장이 가능하므로 ToString()을 사용합니다. 숫자로 된 자료는 이런식으로 저장합니다. 그리고 완성이 된 아이템은 "ItemList"에 담습니다. for문이 끝나고 나면 저장 합니다. 사용의 예) <file cs> using UnityEngine; using System.Collections; using System.Collections.Generic; public class WriteTest : MonoBehaviour { void Start() { List<RecItem> itemList = new List<RecItem>(); for(int i = 0; i < 100; ++i) { RecItem item = new RecItem(); item.Name = "아이템"; item.Level = 1; item.Critical = Random.Range(0.1f, 1.0f); itemList.Add(item); } ItemIO.Write(itemList, Application.dataPath + "/Output/ItemList_Attributes.xml"); } } </file> =====불러오기===== XML형식으로 저장된 파일을 불러옵니다. 불러온 문서의 "ItemList";에 해당하는 요소를 가져옵니다. ( C#에서는 문자열을 Index화 시키기 때문에 가능합니다 ) RecItem 자료형 리스트를 만듭니다. 이제 for문을 돌면서 XML Document로부터 RecItem List로 값을 가져 옵니다. 문자열이 아닌 데이터는 System.Convert.ToInt32 , System.Convert.ToSingle 를 이용하여 변환합니다. (정수, 실수) 사용의 예) <file cs> using UnityEngine; using System.Collections; using System.Collections.Generic; public class ReadTest : MonoBehaviour { void Start() { List<RecItem> itemList = ItemIO.Read(Application.dataPath + "/Output/ItemList_Attributes.xml"); for(int i = 0; i < itemList.Count; ++i) { RecItem item = itemList[i]; Debug.Log(string.Format("Item[{0}] : ({1}, {2}, {3})", i, item.Name, item.Level, item.Critical,)); } } } </file> {{tag>xml}}
2+1?
이 필드는 비어 있도록 유지하세요:
저장
미리 보기
취소
편집 요약
참고: 이 문서를 편집하면 내용은 다음 라이선스에 따라 배포하는 데 동의하는 것으로 간주합니다:
CC Attribution-Noncommercial-Share Alike 4.0 International
문서 도구
문서 보기
이전 판
연결문서
맨 위로
다크 모드로 보기
☀️
Toggle Menu
유니티3D ( Unity3D )
너두 고쳐두 됩니다.
사이트 도구
최근 바뀜
미디어 관리자
사이트맵
사용자 도구
등록
로긴
최근 수정된 문서
sure_bet
preimplantation_genetic_diagnosis
[정상 배아 비율]
isotretinoin
[부작용]
semen_analysis
[정자 향상을 위한 방법]
ectopic_pregnancy
[약물치료]
adult_adhd
[일상생활에서의 대처 방안]
adhd
[주의력결핍- 과잉운동장애]
adhd_dx
제거됨
정신과학
adhd1
제거됨
diabetes_mellitus
[당뇨병 (Diabetes mellitus; DM)]
fructose
[과당 (Fructose)]
gout
[예방]
boric_acid
자궁내막
[자궁내막]
엽산
[Folate vs Folic acid vs 5-MethylTHF]
neural_tube_defect
[고위험군]