您现在的位置:首页 >> 前端 >> 内容

unity安卓移动端读写Xml实例

时间:2017/12/19 10:36:25 点击:

  核心提示:翻别人的博客,翻来翻去试来试去,翻到了一些东西,撞来撞去后终于撞到对的结果之前总感觉好难啊,不想看啊,跳了。后来还是躲不过。移动端的路径什么的还是要弄懂啊,路径一直是个问题。之前试过把xml文件丢到S...

翻别人的博客,翻来翻去试来试去,翻到了一些东西,撞来撞去后终于撞到对的结果

之前总感觉好难啊,不想看啊,跳了。后来还是躲不过。移动端的路径什么的还是要弄懂啊,

路径一直是个问题。之前试过把xml文件丢到StreamingAsset文件夹里,手机终于也能读取了!那个高兴啊,后来不够三秒又不喜欢了。虽然说是电脑端,但别人就是能打开,不爽不爽。又翻。翻到一个老版论坛。好像是蛮牛的来着,可以用Resources加载TextAsset的方式读出来啊。大赞,不用路径了好6啊

大概就是从Resources中把xml用TextAsset的形式Load出来,然后把它用。。上图吧

unity安卓移动端读写Xml实例

那个帖子很老很难找。我就直接把自己打的贴了。先谢谢那位大神。

关于写Xml的,

手机端要读又要写,那就放Application.persistentDataPath里面.但是不能直接放,就算打包之前放入Application.persistentDataPath 路径中,之后会直错错错错。

所以就结合上面的,先把xml文件放到Resources文件夹中,然后第一次打开app的时候从Resources中得到xml里面的东西,再把得到的东西写入Application.persistentDataPath 路径中,之后就可以随意地用doc,Save(那个路径)去修改Xml了。

另踩到过坑,用StreamWriter 写入后,一定要记得 对StreamWriter进行关闭 sw.close() sw.Dispose() 不关闭的话,在电脑上可以看到文件是创建出来了,但是里面什么东西也没有,就是说后续什么也读不出还会报错。出现过两个错误:

XmIException:Document element did not appear.line 1 position 1

↑文件里命名没内容,就是上面说的情况

还有一个就是要把你任务栏上打开的你要写入数据的那个文件夹或者他的父路径什么的关闭

很晚啦。

补路径才是最重要的。各种路径

啊对了,源码

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using System.Xml;

using System.IO;

using System.Text;

public class TestXml : MonoBehaviour {

int tapCount = 0;

/*我的Xml很简单

dasdasfa

*/

XmlDocument doc;

///

/// UIroot。用它找它子节点下面的Collider用的

///

public GameObject uiroot;

///

/// 保存Xml的按钮

///

public GameObject saveBtn;

///

/// 修改Xml数值的按钮

///

public GameObject changeBtn;

///

/// 读取xml数据的按钮

///

public GameObject readBtn;

///

///显示读取数据的Label

///

public UILabel result;

///

///用来显示查询到的Xml结果

///

public UILabel displayInput;

///

/// 用来输入修改值的输入框

///

public UIInput inputBox;

string path;

private void Start()

{

path = Application.persistentDataPath + "/test.xml";

SetFileToPersistent();

LoadXml();

BindBtnEvent();

}

void SetFileToPersistent()

{//StreamWriter一定要记得关闭.不关闭人家不鸟你之前做什么,

FileInfo info = new FileInfo(path);

if (!info.Exists)

{

TextAsset ts = Resources.Load("TestXml/testXml") as TextAsset;

string content = ts.text;

StreamWriter sw = info.CreateText();

sw.Write(content);

sw.Close();

sw.Dispose();

result.text = "添加Xml文件陈宫";

}

else result.text = "已存在Xml文件";

}

///

/// 读取Xml

///

void LoadXml()

{

doc = new XmlDocument();

doc.Load(path);

}

///

/// 绑定事件按钮

///

void BindBtnEvent()

{

Collider[] cols = uiroot.GetComponentsInChildren();

foreach (Collider col in cols)

{

UIEventListener.Get(col.gameObject).onClick = OnClickBtn;

}

}

void OnClickBtn(GameObject go)

{

tapCount++;

if (IsBtn(go, "read"))

ReadXml();

if (IsBtn(go, "change"))

ChangeXmlData();

if (IsBtn(go, "reset"))

inputBox.value = "";

if (IsBtn(go, "save"))

SaveXml();

}

///

/// 是某个按钮的名字?

///

///

///

///

bool IsBtn(GameObject target, string name)

{

if (target.name.Equals(name))

return true;

else return false;

}

///

/// 读取Xml到结果面板上行

///

void ReadXml()

{

XmlNode node = GetNode();

string str= node.InnerText;

result.text = str+" "+tapCount.ToString();

}

///

/// 控制输入框的激活或失效

///

void ControlInputBtn()

{

if (inputBox.enabled)

inputBox.enabled = false;

else inputBox.enabled = true;

}

///

/// 修改Xml数据

///

void ChangeXmlData()

{

string value = inputBox.value;

if ( value.Equals(""))

return;

XmlNode node = GetNode();

node.InnerText = value;

Debug.Log(doc.OuterXml);

}

///

/// 保存xml

///

void SaveXml()

{

doc.Save(path);

Debug.Log("对保存有反应");

}

///

/// 获取那个唯一的节点

///

///

XmlNode GetNode()

{

return doc.SelectSingleNode("root/child");

}

}

代码有点挫,求轻喷。

Tags:UN NI IT TY 
作者:网络 来源:ou_nvhai的博