1. 程式人生 > >unity Android 打包後讀取 xml 檔案

unity Android 打包後讀取 xml 檔案

學習學習!原帖地址:http://www.cnblogs.com/wuzhang/p/wuzhang20140731.html?utm_source=tuicool&utm_medium=referral

問題: 前天在做東西的過程中發現了一個讓人很糾結的問題,為什麼Unity 程式在PC上測試一點都沒問題但是打包釋出到Android後卻無法讀取XML檔案。

通過查詢自資料發現打包發不到安卓後的路徑和PC上測試時的路徑發生了變化,因此讀取就出bug了。

那麼解決方法很簡單:

1,建立一個新工程

2,新增兩個GUItext元件一個用於顯示測試平臺另一個用於顯示讀取到的XML資料,

如下:

3,該貼程式碼了

//------------------------------------------------------------------------------// <auto-generated>//	 此程式碼由工具生成。//	 執行時版本:4.0.30319.18063//
//	 對此檔案的更改可能會導致不正確的行為,並且如果//	 重新生成程式碼,這些更改將會丟失。// </auto-generated>//------------------------------------------------------------------------------
using System;
using UnityEngine; using System.IO; using System.Xml; using System.Linq; using System.Text; using System.Collections; using System.Collections.Generic; namespace AssemblyCSharp1 { public class AddressData1 { public string timeURL; public static string all; public static string hp; public static
string speed; public static string demage; public static string localPath; public static string id; public static string score; public static List<int> allScore; public void AddressData () { Debug.Log (localPath); } public static List<int> getAllScore() { return allScore; } /// <summary> /// 獲取XML路徑/// </summary> /// <returns>The XM.</returns> public static IEnumerator GetXML() { if(Application.platform==RuntimePlatform.Android) { localPath = Application.streamingAssetsPath+"/score.xml"; //在Android中例項化WWW不能在路徑前面加"file://" Debug.Log (localPath); } else { localPath = "file://"+UnityEngine.Application.streamingAssetsPath + "/score.xml";//在Windows中例項化WWW必須要在路徑前面加"file://" Debug.Log (localPath); } WWW www = new WWW(localPath); while (!www.isDone) { Debug.Log("Getting GetXML"); yield return www; all = www.text; ParseXml(www); } } /// <summary> ///按屬性獲取節點/// </summary> /// <param name="www">Www.</param> public static void ParseXml(WWW www) { if(allScore == null) { allScore =new List<int>(); } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(www.text); XmlNodeList nodeList=xmlDoc.SelectSingleNode("rank").ChildNodes; foreach(XmlElement xe in nodeList) { id = xe.GetAttribute("id"); score = xe.GetAttribute("score"); allScore.Add(int.Parse(score)); //將所有得分讀入List Debug.Log ("ID:"+id+" Score:"+score); } allScore.Sort(); allScore.Reverse(); foreach(var score in allScore ) { Debug.Log (score.ToString()); } } /// <summary> /// 讀取xml內容/// </summary> public static IEnumerator load() { string url = string.Empty; string path = string.Empty; string line1 = string.Empty; if(Application.platform==RuntimePlatform.Android) { url=Application.streamingAssetsPath+"/hp.xml"; //在Android中例項化WWW不能在路徑前面加"file://" WWW wWA=new WWW(path);///WWW讀取在各個平臺上都可使用 yield return wWA; line1=wWA.text; Debug.Log (line1); } else { url="file://"+Application.streamingAssetsPath+"/hp.xml";//在Windows中例項化WWW必須要在路徑前面加"file://" WWW wWA=new WWW("file://"+url); yield return wWA; line1=wWA.text; Debug.Log (line1); } yield return null; } /// <summary> /// 載入xml文件/// </summary> /// <returns></returns> public static XmlDocument ReadAndLoadXml() { XmlDocument doc = new XmlDocument(); //Debug.Log("載入xml文件"); doc.Load(localPath); return doc; } /// <summary> /// 增加節點/// </summary> /// <returns>The node.</returns> /// <param name="score">Score.</param> public static void insertNode(int score) { int minute=int.Parse((System.DateTime.Now.Minute.ToString())); string order = System.DateTime.Now.Hour+""+System.DateTime.Now.Minute+""+System.DateTime.Now.Second; XmlDocument xmlDoc=new XmlDocument(); xmlDoc.Load(Application.dataPath + "/StreamingAssets/score.xml"); XmlNode root=xmlDoc.SelectSingleNode("rank"); XmlElement xel=xmlDoc.CreateElement("rank"); //建立節點 xel.SetAttribute("id",order); xel.SetAttribute("score",score.ToString()); root.AppendChild(xel); xmlDoc.Save(Application.dataPath + "/StreamingAssets/score.xml"); return; } } }

測試讀取資料程式碼:

using UnityEngine;
using System.Collections;
using AssemblyCSharp1;

public class testinsert : MonoBehaviour 
{
public GUIText guitext;
public GUIText platform;
string allscores="";

void Awake()
{
if(Application.platform==RuntimePlatform.Android)
{
platform.text = "Android";
}
else
{
platform.text = "PC";
}
}

// Use this for initialization
void Start () 
{

StartCoroutine(AddressData1.GetXML());
//AddressData1.insertNode(22);

}

void OnGUI()
{
if(GUI.Button(new Rect(100,100,40,40),"load"))
{
foreach(int score in AddressData1.allScore)
{
allscores+=score.ToString();
guitext.text+="\t";
}
guitext.text = allscores;
}
}

// Update is called once per frame
void Update () 
{

}
}
<?xml version="1.0" encoding="UTF-8" ?>
<rank>
    <rank id="5618" score="12" ></rank>
    <rank id="1712" score="14" ></rank>
</rank>

那下面雞凍的時刻來了: PC端 執行前:

執行後:

點選load按鈕:

看看控制檯都輸出哪些內容了:

PC上測試Ok了吧。

接下來Android測試,本人的手機哦:

到此結束了,以後程式關於資料處理的都是浮雲了!