還是在這裏做個筆記,不然容易記不住……
1.首先先在Asses下創建StreamingAssets文件夾、然後將配置文件、也就是要讀取的xml文件放到該文件夾下
2.然後就可以開始寫代碼啦(我是爲了方便調用、在這裏順便寫了個單例)
using UnityEngine;
using System.Xml;
public class Xml : MonoBehaviour
{
public static Xml instance;
public string ServerIP;
// Start is called before the first frame update
void Start()
{
LoadXml();
}
void Awake()
{
instance = this;
}
private void LoadXml(){
XmlDocument xml = new XmlDocument();
//---------------config.xml是文件的名字
xml.Load(Application.dataPath + "/StreamingAssets/config.xml");
//---------------這裏WebSocketConfig是一個標籤
XmlNodeList xmlNodeList = xml.SelectSingleNode("WebSocketConfig").ChildNodes;
foreach (XmlElement xl1 in xmlNodeList)
{
ServerIP = xmlNodeList.Item(0).InnerText;
}
}
// Update is called once per frame
void Update()
{
}
}
這個是我的xml文件示例,對應着需要用到的xml進行套用就行
3.最後在Unity場景中、將代碼掛載就可以用了