Unity3D读取外部文件xml、json
C#读取XML文档
Unity3D读取XML文档不需要另外导入程序集(dll),直接引用System.Xml就行。
C#读取XML文档的代码需要根据xml文档结构做出修改。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;
using System.Xml;
using System.IO;
using UnityEngine.UI;
using System;
using UnityEngine.SceneManagement;
public class UnityWebApiJson_zhu : MonoBehaviour
{
void Start ()
{
//IO判断xml文件是否存在
if (File.Exists (Application.streamingAssetsPath + "/WebAPIJsonURL.xml"))
{
XmlDocument xml = new XmlDocument ();
xml.Load (Application.streamingAssetsPath + "/WebAPIJsonURL.xml");
//根节点<Xml>
XmlNodeList xmlnodelist = xml.SelectSingleNode ("Xml").ChildNodes;
foreach (XmlElement elem in xmlnodelist)
{
//字节点<WeapiPeople>
if (elem.Name == "WebapiPeople")
{
//再下一级节点,第二个<Jsonurl>
APIPeopleJsonUrl = elem.ChildNodes [1].InnerText.ToString ();
Debug.Log ("APIPeopleJsonUrl" + APIPeopleJsonUrl);
}
}
}
}
}
下面xml文档的内容
C#读取Json文本
Unity3D读取json数据需要导入程序集LitJson.dll.
读取本地连接需要加上(file:\\),否则无法读取。
读取json代码
private IEnumerator JSonPeopleFromWebAPI ()
{
PeopleListImag.SetActive (true);//显示列表
//下载json数据
WWW ret = new WWW (APIPeopleJsonUrl); //路径参数
yield return ret;
if (ret.error != null) {
Debug.LogError (