using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using System.Text; using UnityEngine.UIElements; //保存的文档格式,第一行,共有n种数据类型 //第二行到第n+1行,每行记录数据类型的名称和数量以及数据种类的数量 //后面依次记录每种数据内含各种属性介绍和每种数据内部数据的各种属性详细数值 //例 //2(m) //树 3(n) 2 //草 2(q) 3 m+1(添加新的数据种类需要在第一行加一并在(m+1)++行进行添加) 并在(m+1+n+1+q+1)++处开始添加对应的数据库 //高度 树干粗度 //4 6 //3 5 //2 2 m+1+n+1(添加某个数据类型在 (m+1+n+1)++行添加数据 ) //高度 光合率 绿度 //0.2 2 4 //0.1 2 4 m+1+n+1+q+1 public class TextReadOrWrite : MonoBehaviour { // Start is called before the first frame update public float time; public int n; public string path = "C:\\Users\\PC\\Desktop\\三维仿真组件\\新建文本文档.txt"; public int line = 0; void Start() { time = 0; n = 1; } //计时器 public bool Time_Clock(float interval){ time += Time.deltaTime; if (time > interval * n) { n++; return true; } else { return false; } } public void WriteNew(string msg) { string UnityPath = path; //将文档中所有行数据存入数组中 string[] rowText = File.ReadAllLines(UnityPath); //创建一个新的长度加一的新数组 string[] rowTextNew = new string[rowText.Length + 1]; for (int n = 0; n < rowText.Length; n++) { rowTextNew[n] = rowText[n]; } rowTextNew[rowTextNew.Length - 1] = msg; //删除原本文本文档里的内容 DeleteTxt(path); //重新撰写一遍文档 for (int j = 0; j < rowTextNew.Length; j++) { Write(path, rowTextNew[j]); } } //C:\\Users\\zsjl\\wendang\\MyTxt.txt 目前按此路径存储 //统一按照文件路径的形式写入内容 public void Write(string position, string Details) { string UnityPath = position; StreamWriter sw = File.AppendText(UnityPath); sw.WriteLine(Details); sw.Close(); } //查找某一个数据再第几行的功能 public int Find(string msg,string position) { string UnityPath = position; //将文档中所有行数据存入数组中 string[] rowText = File.ReadAllLines(UnityPath); //遍历数组查找对应的数据 int m = 0; for (int n = 0; n < rowText.Length;n++) { if (msg.IndexOf(rowText[n]) != -1) { m= n; } } return m; } //读取文档里某一行数据的功能 public string Read(string position, int line) { string UnityPath = position; //将文档中所有行数据存入数组中 string[] rowText = File.ReadAllLines(UnityPath); //获取文档中某一行数据并且按空格分割,存入新数组 if (rowText.Length > line) { return rowText[line]; } else { return null; } } //删除整个文档的功能 public void ClearTxt(string position) { File.Delete(position); } //删除文档内容 public void DeleteTxt(string position) { string path = position; File.WriteAllText(path, string.Empty); } //删除某一行数据的功能 public void Delete(string position, int line) { string UnityPath = position; //将文档中所有行数据存入数组中 string[] rowText = File.ReadAllLines(UnityPath); if (rowText.Length > 1) { //创建一个新的长度减一的新数组 string[] rowTextNew = new string[rowText.Length - 1]; //给新的数组赋值 for (int j = 0; j < rowTextNew.Length; j++) { if (j < line - 1) { rowTextNew[j] = rowText[j]; } else { rowTextNew[j] = rowText[j + 1]; } } //删除原本文本文档里的内容 DeleteTxt(position); //重新撰写一遍文档 for (int j = 0; j < rowTextNew.Length; j++) { Write(position, rowTextNew[j]); } } else { //删除原本文本文档里的内容 DeleteTxt(position); } } //改写某一行数据的功能 public void Rewrite(string position,int line,string Details) { string UnityPath = position; //将文档中所有行数据存入数组中 string[] rowText = File.ReadAllLines(UnityPath); //修改数组对应行数的数据 rowText[line - 1] = Details; //删除原本文本文档里的内容 DeleteTxt(position); //重新撰写一遍文档 for (int j = 0; j < rowText.Length; j++) { Write(position, rowText[j]); } } //添加某一行数据的功能 public void AddNewLine(string position,int line, string Details) { string UnityPath = position; //将文档中所有行数据存入数组中 string[] rowText = File.ReadAllLines(UnityPath); //创建一个新的长度加一的新数组 string[] rowTextNew = new string[rowText.Length +1]; //给新的数组赋值 for (int j = 0; j < rowTextNew.Length; j++) { if (j < line - 1) { rowTextNew[j] = rowText[j]; } if (j == line - 1) { rowTextNew[j] = Details; } if(j>line-1) { rowTextNew[j] = rowText[j -1]; } } //删除原本文本文档里的内容 DeleteTxt(position); //重新撰写一遍文档 for (int j = 0; j < rowTextNew.Length; j++) { Write(position, rowTextNew[j]); } } //从某个string字符处切割string值并输出最后一个值 string text_cut(string text, char low) { string[] b = text.Split(low); string text_cut = b[b.Length - 1]; return text_cut; } string text_cut(string text, string low) { string[] b = text.Split(low); string text_cut = b[b.Length - 1]; return text_cut; } //从某个string字符处切割string值并输出第一个值 string text_cut_up(string text, char low) { string[] b = text.Split(low); string text_cut = b[0]; return text_cut; } string text_cut_up(string text, string low) { string[] b = text.Split(low); string text_cut = b[0]; return text_cut; } //切割某个字符到某个字符输出中间的值 string text_cut_all(string text, string front, string back) { return text_cut_up(text_cut(text, front), back); } }