2016-06-29 9 views
0

XML 파일에서 텍스트를 읽는 코드가 있는데, 파일이 assetbundles에 추가되었습니다. 파일 하나당 번들이있는 것으로 보입니다.Unity가 assetbundle에 XML을 통합하는 대신 직접 읽어들입니다.

번들을 사용하지 말고 XML 내용을 직접 읽는 것이 좋습니다. 지금 번들을 사용하는 코드는 다음과 같습니다.

foreach(KeyValuePair<string,int> kvp in xmlFileDic) 
{ 
string name = kvp.Key; 
string path = xmlBasePath + name + ".assetbundle"; 
int version = kvp.Value; 
WWW www = WWW.LoadFromCacheOrDownload(path,kvp.Value); 
yield return www;   
AssetBundleRequest ab = www.assetBundle.LoadAsync(name,typeof(TextAsset)); 
yield return ab;      
xmlLoadDic.Add(name,(TextAsset)ab.asset); 
www.assetBundle.Unload(false); 

... 
and reading data 
... 

string gamerules = FileManagement.xmlLoadDic["GameMasterData"].text; 
//string gamerules = www.text; 
//Debug.Log("master path:"+masterPath+"game rules:"+gamerules); 
System.IO.TextReader reader = new System.IO.StringReader(gamerules); 
SmallXmlParser parser = new SmallXmlParser(); 
MasterDataHandler handler = new MasterDataHandler(); 
parser.Parse(reader,handler); 

mPhaseList = handler.phaseList; 

어떻게하면 쉽게 할 수 있습니까?

답변

0

다음과 같이 할 수 있습니다. 그러면 Resources 폴더에서 XML 파일이로드되고 LinQ를 사용하여 읽고 읽을 수있는 XElement로로드됩니다. 다음은 시작하는 데 도움이되는 몇 가지 샘플 코드입니다.

 public class XMLReader : MonoBehaviour 
     { 
      XElement someXML; 
      void Awake() 
      { 
       TextAsset textAsset = (TextAsset)Resources.Load("Name_OF_XML_File"); 
       TextReader txtReader = new StringReader(textAsset.text); 
       someXML= XElement.Load(txtReader); 
       string someData = (from data in someXML.Descendants("node") select data.Element("Element_Name").Value).ToString(); 
      } 
     } 
관련 문제