加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

U3D不同平台载入XML文件的方法——IOS MAC Android

发布时间:2020-12-16 05:06:42 所属栏目:百科 来源:网络整理
导读:在PC上和IOS上读取XML文件的方式略有差别,经测试,IOS上不支持如下方法载入XML文件: XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("Assets/Resources/text.xml"); IOS上载入XML的正确方法有2种: (1)方法一 TextAsset textAsset = (TextAsset)R

在PC上和IOS上读取XML文件的方式略有差别,经测试,IOS上不支持如下方法载入XML文件:

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load("Assets/Resources/text.xml");

IOS上载入XML的正确方法有2种:

(1)方法一

TextAsset textAsset = (TextAsset)Resources.Load(filename,typeof(TextAsset));

xmlDoc.Load(new StringReader(textAsset.text));

(2)方法二

xmlDoc.LoadXML(textAsset.text);

上述2种方法分别使用了XmlDocument的Load()方法和LoadXML()方法,传入的参数有些差别,不过都需要通过Resources.Load()方法先将文件载入到一个TextAsset中,然后传给xmlDoc的载入方法。

(3)方法三

需要在IPad上进行持久化操作的文件,比如游戏的本地存档等数据,是不能存放在Resources目录下的,因为IPad上没法对其进行写操作。

那么对于IPad上读写XML,应该怎样进行操作呢?方法如下所述:

将需要序列化的文件存放在Application.persistentDataPath目录下,该目录是一个平台相关的路径。

写:

...

xmlDoc.Save(Application.persistentDataPath+"//abc.xml");

读:

xmlDoc.Load(Application.persistentDataPath+"//abc.xml");

PS1:还有另外一种实现本地持久化操作的方法,使用PlayerPrefs类,此类是U3D提供的专门用来进行玩家偏好设置的类,不过偶暂时未使用此类,是否方便尚未测试。

PS2:

对于Android平台:使用上述方法(3),即和IOS平台相同的操作即可。

对于Mac平台:使用上述方法(1)/(2)。

对于Windows平台:使用上述方法(1)/(2)。


Unity中的Path对应各平台中的Path

IOS:
Application.dataPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data
Application.streamingAssetsPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data/Raw
Application.persistentDataPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents
Application.temporaryCachePath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Library/Caches

Android:
Application.dataPath : /data/app/xxx.xxx.xxx.apk
Application.streamingAssetsPath : jar:file:///data/app/xxx.xxx.xxx.apk/!/assets
Application.persistentDataPath : /data/data/xxx.xxx.xxx/files
Application.temporaryCachePath : /data/data/xxx.xxx.xxx/cache


Windows:
Application.dataPath : /Assets
Application.streamingAssetsPath : /Assets/StreamingAssets
Application.persistentDataPath : C:/Users/xxxx/AppData/LocalLow/CompanyName/ProductName
Application.temporaryCachePath : C:/Users/xxxx/AppData/Local/Temp/CompanyName/ProductName


Mac:
Application.dataPath : /Assets
Application.streamingAssetsPath : /Assets/StreamingAssets
Application.persistentDataPath : /Users/xxxx/Library/Caches/CompanyName/Product Name
Application.temporaryCachePath : /var/folders/57/6b4_9w8113x2fsmzx_yhrhvh0000gn/T/CompanyName/Product Name


Windows Web Player: Application.dataPath : file:///D:/MyGame/WebPlayer (即导包后保存的文件夹,html文件所在文件夹) Application.streamingAssetsPath : Application.persistentDataPath : Application.temporaryCachePath :

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读