首页 > 技术文章 > unity android 读取XML

sy88 2013-12-12 15:49 原文

using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Collections;


public class Load : MonoBehaviour
{
    WWW _www;
    public string _prefabName = "XML/Body02.xml";
    FileInfo fileInfo;
    string _data;
    string _s;
    void Start()
    {
        LoadXML();
    }
    void LoadXML()
    {
        _s += "\n" + "LoadXML";
        if (Application.platform == RuntimePlatform.Android)
        {
            fileInfo = new FileInfo(Application.streamingAssetsPath + "/" + _prefabName);
            StartCoroutine("LoadWWW");
            _s += "\n" + "StartCoroutine";
        }
        else
        {
            fileInfo = new FileInfo(Application.dataPath + "/StreamingAssets" + "/" + _prefabName);
            StreamReader r = fileInfo.OpenText();
            _data = r.ReadToEnd();
            r.Close();
        }
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(_data);
        XmlNodeList nodeList = xmlDoc.SelectSingleNode("ROOT").ChildNodes;
        foreach (XmlElement xe in nodeList)
        {
            Debug.Log(xe);
        }
    }
    void OnGUI()
    {
        GUILayout.Label("" + _s);
        GUILayout.Label("" + _data);

    }
    IEnumerator LoadWWW()
    {
        _s += "\n" + "_www";
        _www = new WWW(Application.streamingAssetsPath + "/" + _prefabName);
        yield return _www;
        _data = _www.text;
        _s += "\n" + Application.streamingAssetsPath + "/" + _prefabName;
        //Instantiate(_www.assetBundle.mainAsset);
    }
}

 

 

using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Collections;


public class Load : MonoBehaviour
{
    WWW _www;
    public string _prefabNameW = "XML/Body02.xml";
    public string _prefabNameA = "XML/Body02.xml";
    public string _prefabNameB = "XML/Body02.xml";
    FileInfo fileInfo;
    string _data;
    string _s;
    void Start()
    {
        LoadXML(_prefabNameA);
    }
    void LoadXML(string _prefabName)
    {
        _s += "\n" + "LoadXML";
        if (Application.platform == RuntimePlatform.Android)
        {
            fileInfo = new FileInfo(Application.streamingAssetsPath + "/" + _prefabName);
            StartCoroutine("LoadWWW");
            _s += "\n" + "StartCoroutine";
        }
        else
        {
            fileInfo = new FileInfo(Application.dataPath + "/StreamingAssets" + "/" + _prefabName);
            StreamReader r = fileInfo.OpenText();
            _data = r.ReadToEnd();
            r.Close();
        }
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(_data);
        XmlNodeList nodeList = xmlDoc.SelectSingleNode("ROOT").ChildNodes;
        foreach (XmlElement xe in nodeList)
        {
            Debug.Log(xe);
        }
    }
    void OnGUI()
    {
        GUILayout.Label("" + _s);
        GUILayout.Label("" + _data);

    }
    IEnumerator LoadWWW()
    {
        _s += "\n" + "_www";
        _www = new WWW(Application.streamingAssetsPath + "/" + _prefabNameW);
        yield return _www;
        _data = _www.text;
        _s += "\n" + Application.streamingAssetsPath + "/" + _prefabNameW;
        //Instantiate(_www.assetBundle.mainAsset);
    }
}

 

推荐阅读