android unity 文件读写_unity Android(安卓)打包后读取 xml 文件
//------------------------------------------------------------------------------此代码由工具生成。//运行时版本:4.0.30319.18063//
//对此文件的更改可能会导致不正确的行为,并且如果//重新生成代码,这些更改将会丢失。------------------------------------------------------------------------------
usingSystem;usingUnityEngine;usingSystem.IO;usingSystem.Xml;usingSystem.Linq;usingSystem.Text;usingSystem.Collections;usingSystem.Collections.Generic;namespaceAssemblyCSharp1
{public classAddressData1
{public stringtimeURL;public static stringall;public static stringhp;public static stringspeed;public static stringdemage;public static stringlocalPath;public static stringid;public static stringscore;public static ListallScore;public voidAddressData ()
{
Debug.Log (localPath);
}public static ListgetAllScore()
{returnallScore;
}///
///获取XML路径///
/// The XM.
public staticIEnumerator 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= newWWW(localPath);while (!www.isDone)
{
Debug.Log("Getting GetXML");yield returnwww;
all=www.text;
ParseXml(www);
}
}///
///按属性获取节点///
/// Www.
public static voidParseXml(WWW www)
{if(allScore == null)
{
allScore=new List();
}
XmlDocument xmlDoc= newXmlDocument();
xmlDoc.LoadXml(www.text);
XmlNodeList nodeList=xmlDoc.SelectSingleNode("rank").ChildNodes;foreach(XmlElement xe innodeList)
{
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 inallScore )
{
Debug.Log (score.ToString());
}
}///
///读取xml内容///
public staticIEnumerator 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 returnwWA;
line1=wWA.text;
Debug.Log (line1);
}else{
url="file://"+Application.streamingAssetsPath+"/hp.xml";//在Windows中实例化WWW必须要在路径前面加"file://"
WWW wWA=new WWW("file://"+url);yield returnwWA;
line1=wWA.text;
Debug.Log (line1);
}yield return null;
}///
///加载xml文档///
///
public staticXmlDocument ReadAndLoadXml()
{
XmlDocument doc= newXmlDocument();//Debug.Log("加载xml文档");
doc.Load(localPath);returndoc;
}///
///增加节点///
/// The node.
/// Score.
public static void insertNode(intscore)
{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=newXmlDocument();
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;
}
}
}
更多相关文章
- Pycharm安装PyQt5的详细教程
- 关于android 4.4以上版本从相册选取图片加载不了图片的问题
- Android(安卓)(shape,gradient)使用总结
- Android通过shape.xml制作渐变背景
- Android(安卓)sd卡读取数据库实例代码
- android--------根据文件路径使用File类获取文件相关信息
- Android(安卓)多媒体系统
- android:launchMode
- anr日志