u3d读取xml文件和u3d 读取txt外部文件
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
public class u3dxml : MonoBehaviour
{
private string m_filename = "2.txt";
private string m_path =null ;
// Use this for initialization
void Start ()
{
XmlDocument xmlDoc = new XmlDocument();
string xmlPath = Application.dataPath + "\1.xml";
//FileStream fs = new FileStream(xmlPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
xmlDoc.Load(xmlPath);
//xmlDoc.Load(fs);//可以Load文件的路径也可以Load文件的流
XmlNodeList componentList = xmlDoc.SelectSingleNode("ttx").ChildNodes;//获取car节点的所有子节点
foreach (XmlNode componentXN in componentList)//遍历所有子节点,得到component结点
{
XmlElement componentXE = (XmlElement)componentXN;//将子节点类型转换为XmlElement类型
XmlNodeList nls = componentXE.ChildNodes;//继续获取component子节点的所有子节点
Debug.Log("data:" + nls.Item(0).InnerText);
// dictionaryOfActionName.Add(int.Parse(nls.Item(0).InnerText), nls.Item(1).InnerText);//取值
}
readTXT();
}
// Update is called once per frame
void Update ()
{
}
///////////////////////////////////////////////////////////////////////////// 1.txt
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
void readTXT() { StreamReader m_reader = null; m_reader = File.OpenText(Application.dataPath + "\" + "config.txt"); string s_line; int Count = 0; while ((s_line = m_reader.ReadLine()) != null) { // Debug.Log(s_line); int numt; string[] words = s_line.Split(':'); numt = int.Parse(words[1]); if (Count == 0) { m_WheelCenter = numt; Debug.Log("Wheelcenter:" + m_WheelCenter); } if (Count == 1) { m_WheelLeft = numt; Debug.Log("wheelleft:" + m_WheelLeft); } if (Count == 2) { m_WheelRight = numt; Debug.Log("wheelright:" + m_WheelRight); } if (Count == 3) { m_YouMenMin = numt; Debug.Log("YouMenMin:" + m_YouMenMin); } if (Count == 4) { m_YouMenMax = numt; Debug.Log("YouMenMax:" + m_YouMenMax); } Count++; } m_YouMenFanWei = m_YouMenMax - m_YouMenMin; m_ShaCheFanWei = m_ShaCheMax - m_YouMenMin; m_reader.Close(); m_reader.Dispose(); }
更新
txt文件读和写
void ReadConfig(string _name) { StreamReader m_reader = null; m_reader = File.OpenText(Application.dataPath + "\" + _name); string s_line; int Count = 0; while ((s_line = m_reader.ReadLine()) != null) { if (Count == 0) { int numt; string[] words = s_line.Split(' '); int v = int.Parse(words[0]); Debug.Log("Debug Mode:" + v); } if (Count == 1) { int numt; string[] words = s_line.Split(' '); int v = int.Parse(words[0]); Debug.Log("Load Config CameraID:" + v); } ++Count; } m_reader.Close(); m_reader.Dispose(); } void WriteTxtMaxScore(string _name) { StreamWriter writeStream = new StreamWriter(Application.dataPath +"\"+ _name); string histroyMaxscore = "HistoryMaxScore:"+ m_HistoryMaxScore; string todayScore = "TotadyMaxScore:" + m_TodayMaxScore; writeStream.WriteLine(histroyMaxscore); writeStream.WriteLine(todayScore); //关闭流 writeStream.Close(); //销毁流 writeStream.Dispose(); }