當時遇到的第一個問題是,想要有個讀取xml的方法,寫在了Ienumerator里面的。所以需要等待文本讀寫完畢,獲得文本的數據,才能執行下一步的代碼。
解決辦法:在方法執行到最后的時候,增加回調函數。
還有一個問題是:當時遇到xml.load(text);這句話老是報錯。
解決辦法xml.load 改成了 xml.loadxml,問題解決,具體代碼如下:
public static IEnumerator ReadVersionXML(System.Action onComplete){UnityWebRequest www = UnityWebRequest.Get(Application.streamingAssetsPath + "/version.xml");yield return www.SendWebRequest();string nowtext = www.downloadHandler.text;if (nowtext.Length > 0){//去除utf-8格式的bom,這樣才能讀取xml內容StringReader stringReader = new StringReader(nowtext);stringReader.Read(); // skip BOMstring result = stringReader.ReadToEnd();stringReader.Close();XmlDocument xml = new XmlDocument();xml.LoadXml(result); //加載xml文件XmlNodeList nodeList = xml.SelectSingleNode("Version").ChildNodes;foreach (XmlElement xe in nodeList){ //遍歷所以子節點if (xe.GetAttribute("id") == "1"){string version = xe.GetAttribute("name");//賦值游戲版本號GameData.gameVesion = version;}}onComplete?.Invoke();}else {GameData.gameVesion = Application.version;}}