unity Android 打包后读取 xml 文件
原地址:http://www.cnblogs.com/wuzhang/p/wuzhang20140731.html
问题: 前天在做东西的过程中发现了一个让人很纠结的问题,为什么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文件:
score.xml
<?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测试,本人的手机哦:



到此结束了,以后程序关于数据处理的都是浮云了!
unity Android 打包后读取 xml 文件的更多相关文章
- Android 开发自己的网络收音机4——读取XML文件的电台数据
国内外的电台数据很多,起码有好几百,所以把这些数据都写到代码里面是不实际的.只能写成一个数据文件,程序启动的时候再去加载.保存这些简单数据,我们肯定会优先使用XML文件,今天讲讲如何读取XML里面的数 ...
- unity Android在streamingAssets路径下文件无法读取的的解决方法
unity Android在streamingAssets路径下文件,有时候plugin下的.jar或者.so无法直接读取: 解决方法之一,拷贝至其他路径: #if UNITY_ANDROID str ...
- android读取xml文件来实现省份,城市,区的选择
本博客如需转载.请注明出处. ------------------------------------------------------------------------------------- ...
- 读取xml文件报错:Invalid byte 2 of 2-byte UTF-8 sequence。
程序读取xml文件后,系统报“Invalid byte 2 of 2-byte UTF-8 sequence”错误,如何解决呢? 1.程序解析xml的时候,出现Invalid byte 2 of 2- ...
- 在C#中创建和读取XML文件
1.创建简单的XML文件 为了便于测试,我们首先创建控制台应用程序,项目命名为CreateXml,Program.cs代码如下: 这样会在C盘根目录下创建data2.xml文件,文件内容为 using ...
- Android color(颜色) 在XML文件和java代码中
Android color(颜色) 在XML文件和java代码中,有需要的朋友可以参考下. 1.使用Color类的常量,如: int color = Color.BLUE;//创建一个蓝色 是使用An ...
- java读取xml文件报“org.xml.sax.SAXParseException: Premature end of file” .
背景:java读取xml文件,xml文件内容只有“<?xml version="1.0" encoding="UTF-8"?>”一行 java读取该 ...
- C#中经常使用的几种读取XML文件的方法
XML文件是一种经常使用的文件格式,比如WinForm里面的app.config以及Web程序中的web.config文件,还有很多重要的场所都有它的身影.Xml是Internet环境中跨平台的,依赖 ...
- 读取XML文件内容
myeclipse中类的格式 上面中的RunMain.java为程序执行的入口,JdbcUtil.java为实体类,XmlDocumentUtil.java执行解释xml文件与获取里面的属性,程序所需 ...
随机推荐
- unique函数 (STL)
转自http://www.cnblogs.com/heyonggang/archive/2013/08/07/3243477.html 类属性算法unique的作用是从输入序列中“删除”所有相邻的重复 ...
- 【转】Monkeyrunner测试1——Monkeyrunner的使用
Monkeyrunner的使用 一.终端输入单行命令行实现 1. 开启android 模拟器 (1) 用Eclipse打开andorid的模拟器 (2) 在CMD中用andorid命令打开模拟器 定位 ...
- 如何覆盖aar的资源
1.首先理解一下aar的构造 classes.jar ----代码 res---资源文件 2.替换 查看res里面的资源文件,这个资源文件事实上都是跟安卓的资源文件夹是一样的.你只需要理解xml和里面 ...
- 重大发现Android studio 如何简单快速修改package name
好多人都发现Android studio修改包名比较麻烦,只能一级一级的修改,今天偶尔发现了一个快捷方法. 废话不多说: 1 打开项目的AndroidManifest.xml文件 2 鼠标光笔定位到你 ...
- Android Toast 总结(五种用法)
Toast大家都很熟,不多说.直接上图上代码. 具体代码如下: main.xml: <?xml version="1.0" encoding="utf-8" ...
- ###C中的extern-static-const关键词
#@date: 2014-06-14 #@author: gerui #@email: forgerui@gmail.com Contents extern的作用一般是用来声音一个外部变量和函数.一般 ...
- Xcode中添加代码块的方式
在写代码的过程中,经常会有重复的代码(比如说,cell的使用),当然了复制粘贴也不是不行,但是Xcode提供了一个很方便的东西. 1.在Xcode右下角你会看到有一个{}的东西,这里是一些常用的代码块 ...
- iOS UIView 快速修改 frame,
在iOS开发布局修改 frame 时需要繁琐的代码实现,今天偶尔看到一播客说到快速修改的 frame 的方法,自己动手写了一遍实现代码. 快速实现主要通过 添加类目的方式,对UIView 控件添加了一 ...
- 04_SSM框架整合(Spring+SpringMVC+MyBatis)
[SSM的系统架构] [整合概述] 第一步: MyBatis和Spring整合,通过Spring管理mapper接口. 使用mapper的扫描器自动扫描mapper接口在Spring中进行注册. 第二 ...
- 查找并绘制轮廓[OpenCV 笔记XX]
好久没有更新了,原谅自己放了个假最近又在赶进度,所以...更新的内容是很靠后的第八章,因为最近工作要用就先跳了,后面会更新笔记编号...加油加油! 在二值图像中寻找轮廓 void cv::findCo ...