創建、寫入、讀取本地.txt(.ini)
public Text readText; private string path; private string path2; private string content; private string[] contents; // Use this for initialization void Start () { path = Application.dataPath + "\\test.txt";//創建.ini文件直接將后綴由.txt改為.ini即可 path2 = Application.dataPath + "\\test1.txt"; content = "窗前明月光|疑是地上霜|舉頭望明月|低頭思故鄉";//內容改為自己需要的即可,比如網址之類的 contents = content.Split('|'); } // Update is called once per frame void Update () { } //寫入數據 public void WriteTxt() { if(!File.Exists(path)) { //文本不存在創建文本 FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate); StreamWriter sw = new StreamWriter(fileStream, Encoding.UTF8); foreach (var item in contents) { sw.WriteLine(item);//WriteLine每次最后結尾也會帶一個回車符號,如果只有一行內容建議使用Write,比如項目中只需要寫入網址,那么就只需要一行就行 } sw.Close(); fileStream.Close(); } else { } } //讀取數據 public void ReadTxt() { if(File.Exists(path)) { StreamReader sr = new StreamReader(path); readText.text = sr.ReadToEnd(); sr.Close(); } } //刪除文本 public void DeleTxt() { FileInfo fileInfo = new FileInfo(path); fileInfo.Delete(); } //拷貝文本 public void CopyTxt() { if(File.Exists(path)&&File.Exists(path2)) { string content; StreamReader sr = new StreamReader(path); content= sr.ReadToEnd(); StreamWriter sw = new StreamWriter(path2); sw.Write(content); sr.Close(); sw.Close(); } } }