unity Android打包后读取XML文件

转载:http://www.it165.net/pro/html/201408/18828.html

  • 问题:    前天在做东西的过程中发现了一个让人很纠结的问题,为什么Unity 程序在PC上测试一点都没问题但是打包发布到Android后却无法读取XML文件。

    通过查找自资料发现打包发不到安卓后的路径和PC上测试时的路径发生了变化,因此读取就出bug了。

    那么解决方法很简单:

    1,建立一个新工程

    \

    2,添加两个GUItext组件一个用于显示测试平台另一个用于显示读取到的XML数据,

    如下:

    \

    3,该贴代码了

    001. //------------------------------------------------------------------------------
    002. // <auto-generated>
    003. //     此代码由工具生成。
    004. //     运行时版本:4.0.30319.18063
    005. //
    006. //     对此文件的更改可能会导致不正确的行为,并且如果
    007. //     重新生成代码,这些更改将会丢失。
    008. // </auto-generated>
    009. //------------------------------------------------------------------------------
    010. using System;
    011. using UnityEngine;
    012. using System.IO;
    013. using System.Xml;
    014. using System.Linq;
    015. using System.Text;
    016. using System.Collections;
    017. using System.Collections.Generic;
    018.  
    019.  
    020. namespace AssemblyCSharp1
    021. {
    022.  
    023. public class AddressData1
    024. {
    025. public string timeURL;
    026. public static string all;
    027. public static string hp;       
    028. public static string speed;
    029. public static string demage;
    030.  
    031. public static string localPath;
    032. public static string id;
    033. public static string score;
    034. public static List<int> allScore;
    035.  
    036. public void AddressData ()
    037. {
    038. Debug.Log (localPath);
    039. }
    040.  
    041. public static List<int> getAllScore()
    042. {
    043. return allScore;
    044. }
    045.  
    046. /// <summary>
    047. /// 获取XML路径
    048. /// </summary>
    049. /// <returns>The XM.</returns>
    050. public static IEnumerator GetXML()
    051. {
    052. if(Application.platform==RuntimePlatform.<a href="http://www.it165.net/pro/ydad/" target="_blank" class="keylink">Android</a>)
    053. {
    054. localPath = Application.streamingAssetsPath+"/score.xml"//在Android中实例化WWW不能在路径前面加"file://"
    055. Debug.Log (localPath);
    056. }
    057. else
    058. {
    059. localPath = "file://"+UnityEngine.Application.streamingAssetsPath + "/score.xml";//在Windows中实例化WWW必须要在路径前面加"file://"
    060.  
    061. Debug.Log (localPath);
    062. }
    063. WWW www = new WWW(localPath);
    064. while (!www.isDone)
    065. {
    066. Debug.Log("Getting GetXML");
    067. yield return www;
    068. all = www.text;
    069. ParseXml(www);
    070. }
    071. }
    072.  
    073. /// <summary>
    074. ///按属性获取节点
    075. /// </summary>
    076. /// <param name="www">Www.</param>
    077. public static void ParseXml(WWW www)
    078. {
    079. if(allScore == null)
    080. {
    081. allScore =new List<int>();
    082. }
    083. XmlDocument xmlDoc = new XmlDocument();
    084. xmlDoc.LoadXml(www.text);
    085. XmlNodeList nodeList=xmlDoc.SelectSingleNode("rank").ChildNodes;
    086.  
    087. foreach(XmlElement xe in nodeList)
    088. {
    089. id = xe.GetAttribute("id");
    090. score = xe.GetAttribute("score");
    091. allScore.Add(int.Parse(score));  //将所有得分读入List
    092.  
    093. Debug.Log ("ID:"+id+" Score:"+score);
    094. }
    095. allScore.Sort();
    096. allScore.Reverse();
    097. foreach(var score in allScore )
    098. {
    099. Debug.Log (score.ToString());
    100. }
    101. }
    102.  
    103. /// <summary>
    104. /// 读取xml内容
    105. /// </summary>
    106. public static IEnumerator load()
    107. {
    108. string url = string.Empty;
    109. string path = string.Empty;
    110. string line1 = string.Empty;
    111. if(Application.platform==RuntimePlatform.Android)
    112. {
    113. url=Application.streamingAssetsPath+"/hp.xml"//在Android中实例化WWW不能在路径前面加"file://"
    114.  
    115. WWW wWA=new WWW(path);///WWW读取在各个平台上都可使用
    116. yield return wWA;
    117. line1=wWA.text;
    118. Debug.Log (line1);
    119. }
    120. else
    121. {
    122. url="file://"+Application.streamingAssetsPath+"/hp.xml";//在Windows中实例化WWW必须要在路径前面加"file://"
    123. WWW wWA=new WWW("file://"+url);
    124. yield return wWA;
    125. line1=wWA.text;
    126. Debug.Log (line1);
    127. }
    128. yield return null;
    129. }
    130.  
    131.  
    132. /// <summary>
    133. /// 加载xml文档
    134. /// </summary>
    135. /// <returns></returns>
    136. public static  XmlDocument ReadAndLoadXml()
    137. {
    138. XmlDocument doc = new XmlDocument();
    139. //Debug.Log("加载xml文档");
    140. doc.Load(localPath);
    141. return doc;
    142. }
    143.  
    144. /// <summary>
    145. /// 增加节点
    146. /// </summary>
    147. /// <returns>The node.</returns>
    148. /// <param name="score">Score.</param>
    149. public static void insertNode(int score)
    150. {
    151. int minute=int.Parse((System.DateTime.Now.Minute.ToString()));
    152. string order = System.DateTime.Now.Hour+""+System.DateTime.Now.Minute+""+System.DateTime.Now.Second;
    153. XmlDocument xmlDoc=new XmlDocument();
    154. xmlDoc.Load(Application.dataPath + "/StreamingAssets/score.xml");
    155. XmlNode root=xmlDoc.SelectSingleNode("rank");
    156.  
    157. XmlElement xel=xmlDoc.CreateElement("rank");    //建立节点
    158. xel.SetAttribute("id",order);
    159. xel.SetAttribute("score",score.ToString());
    160.  
    161. root.AppendChild(xel);
    162. xmlDoc.Save(Application.dataPath + "/StreamingAssets/score.xml");
    163. return;
    164. }
    165.  
    166. }
    167.  
    168.  
    169. }

    测试读取数据代码:

    01. using UnityEngine;
    02. using System.Collections;
    03. using AssemblyCSharp1;
    04.  
    05. public class testinsert : MonoBehaviour
    06. {
    07. public GUIText guitext;
    08. public GUIText platform;
    09. string allscores="";
    10.  
    11. void Awake()
    12. {
    13. if(Application.platform==RuntimePlatform.Android)
    14. {
    15. platform.text = "Android";
    16. }
    17. else
    18. {
    19. platform.text = "PC";
    20. }
    21. }
    22.  
    23. // Use this for initialization
    24. void Start ()
    25. {
    26.  
    27. StartCoroutine(AddressData1.GetXML());
    28. //AddressData1.insertNode(22);
    29.  
    30. }
    31.  
    32. void OnGUI()
    33. {
    34. if(GUI.Button(new Rect(100,100,40,40),"load"))
    35. {
    36. foreach(int score in AddressData1.allScore)
    37. {
    38. allscores+=score.ToString();
    39. guitext.text+="\t";
    40. }
    41. guitext.text = allscores;
    42. }
    43. }
    44.  
    45. // Update is called once per frame
    46. void Update ()
    47. {
    48.  
    49. }
    50. }

    XML文件:
    score.xml

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <rank>
    3. <rank id="5618" score="12" ></rank>
    4. <rank id="1712" score="14" ></rank>
    5. </rank>

    那下面鸡冻的时刻来了:
    PC端 运行前:

    \

    运行后:

     \

    点击load按钮:

    \

    看看控制台都输出哪些内容了:

    \

    PC上测试Ok了吧。

    接下来Android测试,本人的手机哦:

    \

    \

    到此结束了,以后程序关于数据处理的都是浮云了!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值