文章转自:http://www.unity3d8.com/content/c-%E8%AF%BB%E5%8F%96%E6%96%87%E4%BB%B6-%E6%93%8D%E4%BD%9C
Unity3d 用C#操作文件
文件读写 创建
1 首先在工程面板所在的文件夹中(即Assets/文件夹下)新建一个FileOperation.txt文件,在文件中写入一些内容
2 将下面代码脚本附加到一个场景中的一个GameObject中。
using UnityEngine;
using System.Collections;
using System.IO;
public class FileOperation : MonoBehaviour
{
// Use this for initialization
StreamWriter sw;
void Start () {
string fileAddress = Application.dataPath + "/FileOperation.txt";
FileInfo fInfo0 = new FileInfo(fileAddress);
string s = "";
if (fInfo0.Exists)
{
StreamReader r = new StreamReader(fileAddress);
s = r.ReadToEnd();
Debug.Log("s=" + s);
}
FileInfo fInfo1 = new FileInfo(Application.dataPath + "/FileOperation1.txt");
if (!fInfo1.Exists)
{
sw = fInfo1.CreateText();
}
else
{
sw = fInfo1.AppendText();
}
sw.Write(s);
sw.Close();
}
// Update is called once per frame
void Update () {
}
}