在网上查了一些方法,改改测测写出来的。可能有些地方不太合适或者有隐患,至少目前用起来没什么问题。存一下代码。
Config.cs:
using UnityEngine;
using System.Collections;
using System.Xml;
//using System.IO;
namespace MyConfig
{
public struct ConfigData
{
public int viewMode;
public int followNum;
public int planeHeight;
public int recvPort;
public string recvIP;
};
public class Config
{
public ConfigData ReadXML(string fileName)
{
ConfigData ret;
XmlDocument xmlDoc = new XmlDocument();
//string path = "G:/Test.xml";
//xmlDoc.Load(path);
//xmlDoc.Load(Application.dataPath + "/Test.xml");
//在编辑状态下输出和Asset文件夹同目录的路径,发布状态下得到和生成exe同目录的路径
string DPath = Application.dataPath;
int num = DPath.LastIndexOf("/");
DPath = DPath.Substring(0, num);
//xmlDoc.Load(DPath + "/UnityConfig.xml");
xmlDoc.Load(DPath + fileName);
XmlNode rootNode = xmlDoc.GetElementsByTagName("UnityData")[0];
//int nodeCount = rootNode.ChildNodes.Count; //根节点下节点数
//Debug.Log(nodeCount);
ret.viewMode = XmlConvert.ToInt32(rootNode.ChildNodes[0].InnerText);
ret.followNum = XmlConvert.ToInt32(rootNode.ChildNodes[1].InnerText);
ret.planeHeight = XmlConvert.ToInt32(rootNode.ChildNodes[2].InnerText);
ret.recvPort = XmlConvert.ToInt32(rootNode.ChildNodes[3].InnerText);
ret.recvIP = rootNode.ChildNodes[4].InnerText;
//Debug.Log(ret.viewMode);
//Debug.Log(ret.followNum);
//Debug.Log(ret.planeHeight);
return ret;
}
}
}
private Config config = new Config();
private ConfigData configData;
//读配置文件
configData = config.ReadXML("/UnityConfig.xml");
<?xml version="1.0" encoding="utf-8"?>
<UnityData>
<ViewMode>1</ViewMode>
<FollowNum>0</FollowNum>
<Height>0</Height>
<RecvPort>2017</RecvPort>
<RecvIP>127.0.0.1</RecvIP>
</UnityData>
以后有机会学深一点的话再回来修改。