欢迎来到Introzo百科
Introzo百科
当前位置:网站首页 > 技术 > unity Android 打包后读取 xml 文件

unity Android 打包后读取 xml 文件

日期:2023-09-28 16:38

-->

原地址:http://www.introzo.com/wuzhang/p/wuzhang20140731.html

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

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

那么解决方法很简单:

1,建立一个新工程

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

如下:

3,该贴代码了

//------------------------------------------------------------------------------
//
// 此代码由工具生成。
// 运行时版本:4.0.30319.18063
//
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
//

//------------------------------------------------------------------------------
using System;
using UnityEngine;
using www.introzo.com;
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 allScore; public void AddressData ()
{
Debug.Log (localPath);
} public static List getAllScore()
{
return allScore;
} ///
/// 获取XML路径
///

/// The XM.
public static IEnumerator GetXML()
{
if(Application.platform==RuntimePlatform.Android)
{
localPath = www.introzo.comingAssetsPath+"/score.xml"; //在Android中实例化WWW不能在路径前面加"file://"
Debug.Log (localPath);
}
else
{
localPath = "file://"+www.introzo.comingAssetsPath + "/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);
}
} ///
///按属性获取节点
///

/// Www.
public static void ParseXml(WWW www)
{
if(allScore == null)
{
allScore =new List();
}
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(www.text);
XmlNodeList nodeList=www.introzo.comSingleNode("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());
}
} ///
/// 读取xml内容
///

public static IEnumerator load()
{
string url = string.Empty;
string path = string.Empty;
string line1 = string.Empty;
if(Application.platform==RuntimePlatform.Android)
{
url=www.introzo.comingAssetsPath+"/hp.xml"; //在Android中实例化WWW不能在路径前面加"file://" WWW wWA=new WWW(path);///WWW读取在各个平台上都可使用
yield return wWA;
line1=wWA.text;
Debug.Log (line1);
}
else
{
url="file://"+www.introzo.comingAssetsPath+"/hp.xml";//在Windows中实例化WWW必须要在路径前面加"file://"
WWW wWA=new WWW("file://"+url);
yield return wWA;
line1=wWA.text;
Debug.Log (line1);
}
yield return null;
} ///
/// 加载xml文档
///

///
public static XmlDocument ReadAndLoadXml()
{
XmlDocument doc = new XmlDocument();
//Debug.Log("加载xml文档");
doc.Load(localPath);
return doc;
} ///
/// 增加节点
///

/// The node.
/// Score.
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=www.introzo.comSingleNode("rank"); XmlElement xel=xmlDoc.CreateElement("rank"); //建立节点
xel.SetAttribute("id",order);
xel.SetAttribute("score",score.ToString()); root.AppendChild(xel);
www.introzo.com(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





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

运行后:

点击load按钮:

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

PC上测试Ok了吧。

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

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

-->