Unity3D读取XML文档信息 分类: Unity3D XML C# -- : 人阅读 评论(0)收藏举报Unity3DXML[csharp] view plaincopyprint?using System; using UnityEngine; using System.IO; using System.Xml; using System.Linq; using System.Text; using System.Collections.Generic; namespace Address { /// <summary> /// 地址数据 /// </summary> public class AddressData { /// <summary> ///当前城市ID /// </summary> public static string _nowProvinceId; /// <summary> /// 所有省名字 /// </summary> public static List<string> allProvinceName = new List<string>(); /// <summary> /// 所有城市id /// </summary> public List<string> allCityId = new List<string>(); ///<summary> ///所有城市名字 ///</summary> public List<string> allCityName = new List<string>(); public static string localUrl = Application.dataPath "/XMLFile1.xml"; /// <summary> /// 加载xml文档 /// </summary> /// <returns></returns> public static XmlDocument ReadAndLoadXml() { XmlDocument doc = new XmlDocument(); Debug.Log("加载xml文档"); doc.Load(localUrl); return doc; } /// <summary> /// 从本地加载xml并获取所有省的名字 /// </summary> /// <param name="url"></param> /// <returns></returns> public static List<string> GetAllProvinceName() { List<string> _allProvinceName = new List<string>(); XmlDocument xmlDoc = ReadAndLoadXml(); //所有province节点 XmlNode provinces = xmlDoc.SelectSingleNode("province"); foreach (XmlNode province in provinces) { XmlElement _province = (XmlElement)province; //所有provinceName添加到列表 allProvinceName.Add(_province.GetAttribute("name")); } Debug.Log("所有省数目"allProvinceName.Count); _allProvinceName = allProvinceName; return _allProvinceName; } /// <summary> /// 根据当前省ID返回当前省的所有城市名 /// </summary> /// <param name="nowProvinceId"></param> /// <returns></returns> public static List<string> GetAllCityNameByNowProvinceId(string nowProvinceId) { List<string> nowAllCityName = new List<string>(); XmlDocument xmlDoc = ReadAndLoadXml(); //所有province节点 XmlNode provinces = xmlDoc.SelectSingleNode("province"); foreach (XmlNode province in provinces) { XmlElement _province = (XmlElement)province; //当前城市id if (nowProvinceId == _province.GetAttribute("id")) { foreach (XmlElement city in _province.ChildNodes) { XmlElement _city = (XmlElement)city; //当前城市的所有cityName添加到列表 nowAllCityName.Add(_city.GetAttribute("name")); } } } return nowAllCityName; } /// <summary> /// 根据省的ID返回省的名字 /// </summary> /// <param name="provinceId"></param> /// <returns></returns> public static string GetProvinceName(string provinceId) { string _provinceName = ""; XmlDocument xmlDoc = ReadAndLoadXml(); //所有province节点 XmlNode provinces = xmlDoc.SelectSingleNode("province"); foreach (XmlNode province in provinces) { XmlElement _province = (XmlElement)province; if (provinceId == _province.GetAttribute("id")) { //获取实际省名 _provinceName= _province.GetAttribute("name"); } } return _provinceName; } /// <summary> /// 根据城市ID返会城市名字 /// </summary> /// <param name="cityId"></param> /// <returns></returns> public static string GetCityName(string cityId) { string cityName=""; XmlDocument xmlDoc = ReadAndLoadXml(); //所有province节点 XmlNode provinces = xmlDoc.SelectSingleNode("province"); foreach (XmlNode province in provinces) { XmlElement _province = (XmlElement)province; if (_nowProvinceId == _province.GetAttribute("id")) { foreach (XmlElement city in _province.ChildNodes) { XmlElement _city = (XmlElement)city; if (cityId == _city.GetAttribute("id")) { //获取实际城市名 cityName = _city.GetAttribute("name"); } } } } return cityName; } } }
推荐整理分享Unity3D读取XML文档信息实例(unity读取fbx文件),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:unity读取streamassets文件,unity3d 读取文件,unity3d 读取文件,unity读取txt数据,unity读取txt数据,unity3d 读取文件,unity 读取xml文件,unity3d 读取文件,内容如对您有帮助,希望把文章链接给更多的朋友!
[csharp] view plaincopyprint?using UnityEngine; using System.Collections; using System.Collections.Generic; using Address; public class FinalTest : MonoBehaviour { // Use this for initialization void Start () { List<string> allp = new List<string>(); allp = AddressData.GetAllProvinceName(); Debug.Log(AddressData.allProvinceName.Count); Debug.Log(allp.Count); List<string> allCity = new List<string>(); allCity = AddressData.GetAllCityNameByNowProvinceId(""); Debug.Log(allCity.Count); for (int i = 0; i < allCity.Count; i) { Debug.Log(allCity[i]); } string a = AddressData.GetProvinceName(""); Debug.Log(a); } // Update is called once per frame void Update() { } }
[html] view plaincopyprint?<?xml version="1.0" encoding="utf-8"?> <province> <province id ="" name="江苏"> <city id ="" name ="南京"></city> <city id ="" name ="镇江"></city> <city id ="" name ="南通"></city> </province> <province id ="" name="河南"> <city id ="" name ="郑州"></city> <city id ="" name ="开封"></city> <city id ="" name ="洛阳"></city> </province> </province>
Unity的WWW类的用法整理 Unity的WWW类的用法整理1.Unity的WWW类主要支持GET和POST两种方式。GET方式请求的内容会附在url的后面一起做为URL向服务器发送请求(请求的内容使用符号隔
[置顶] Unity Editor Extensions – Menu Items 转载请注明出处:
3D语音天气球(源码分享)——完结篇 转载请注明本文出自大苞米的博客(