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


    转自:http://www.cnblogs.com/sifenkesi/archive/2012/03/12/2391330.html

    在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));

    XmlDocument xmlDoc = new XmlDocument();

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

    (2)方法二

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

    XmlDocument xmlDoc = new XmlDocument();

    xmlDoc.LoadXML(textAsset.text);

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

    (3)方法三

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

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

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

    写:

    XmlDocument xmlDoc = new XmlDocument();

    ...

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

    读:

    XmlDocument xmlDoc = new XmlDocument();

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

    PS1:

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

    PS2:

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

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

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

  • 相关阅读:
    Thinking in Java Reading Note(9.接口)
    Thinking in java Reading Note(8.多态)
    Thinking in Java Reading Note(7.复用类)
    SQL必知必会
    Thinking in Java Reading Note(5.初始化与清理)
    Thinking in Java Reading Note(2.一切都是对象)
    鸟哥的Linux私房菜笔记(1.基础)
    Thinking in Java Reading Note(1.对象导论)
    CoreJava2 Reading Note(2:I/O)
    CoreJava2 Reading Note(1:Stream)
  • 原文地址:https://www.cnblogs.com/Yellow0-0River/p/4565456.html
Copyright © 2020-2023  润新知