还是在这里做个笔记,不然容易记不住……
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场景中、将代码挂载就可以用了