1.解壓壓縮包,把文件夾拖到腳本文件夾下
Mono是第三方基金會開發的開源的東西,通過Mono基礎上開發的程序可以在各個系統下運行。開發語言是C#。
用插件解析比較高效,平台運行穩定。使用簡單。
Unity安裝路徑中可以找到Mono壓縮包
2.新建腳本XmlTest
using UnityEngine;
using System.Collections;
using Mono.Xml;
using System.Security;
public class XmlTest : MonoBehaviour {
//測試xml是否可用
// Use this for initialization
void Start () {
//加載外部xml文檔
string strXml = Resources.Load("Enemy").ToString();
//解析xml,生成SecurityParser對象,然后解析字符串為xml格式
SecurityParser parse = new SecurityParser();
//傳入要解析的字符串
parse.LoadXml(strXml);
//獲取加載xml的根節點
SecurityElement se = parse.ToXml();
//遍歷se子節點,se代表root,子節點代表table
foreach (SecurityElement element in se.Children)
{
//先判定節點是否為table
if (element.Tag.Equals("table"))
{
//輸出所有wave屬性值
Debug.Log(element.Attribute("wave").ToString());
Debug.Log(element.Attribute("level").ToString());
}
}
}
// Update is called once per frame
void Update () {
}
}
3.在Scene中新建Manager空物體,把腳本掛在上面,運行。
看控制台輸出,如果輸出為xml文件中變量的值則解析成功。