Unity3D读取外部文件

本文介绍了如何在Unity3D中使用C#进行XML和JSON文件的读取。对于XML文档,Unity3D内置了System.Xml支持,无需额外导入程序集。而对于JSON,需要引入LitJson.dll来读取本地文件。注意在指定本地文件路径时需加上(file:\)前缀。
摘要由CSDN通过智能技术生成

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文档的内容
WebAPIJsonURL

C#读取Json文本

Unity3D读取json数据需要导入程序集LitJson.dll.
读取本地连接需要加上(file:\\),否则无法读取。

LitJson
读取json代码

private IEnumerator JSonPeopleFromWebAPI ()
{
   
		PeopleListImag.SetActive (true);//显示列表
		//下载json数据
		WWW ret = new WWW (APIPeopleJsonUrl);    //路径参数
		yield return ret;
		if (ret.error != null) {
   
				Debug.LogError (
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值