Unity 经典教程 之 快速学会File文件读操作
1、File:
Provides static methods for file operations.
This is an alternative for System.IO.File class. Note: this class is only available when targeting Universal Windows Platform.
1、打开Unity,新建一个空工程,然后Unity界面如下图

2、在工程中新建一个脚本,脚本可以命名为“FileTest”,具体如下图

3、选中“FileTest”脚本,双击脚本或者右键“Open C# Project”,具体如下图

4、在打开的“FileTest”脚本上进行代码编辑,首先设置文件的读取路径,接着通过不同的三种方法读取文件中的内容,具体代码及代码说明如下图

5、脚本具体代码如下:
using System.IO;
using UnityEngine;
public class FileTest : MonoBehaviour {
private string fileText1path = @"Assets\FileText1.txt";
private string fileText2path = @"Assets\FileText2.txt";
private string fileText3path = @"Assets\FileText3.png";
// Use this for initialization
void Start () {
string fileText1Content = File.ReadAllText(fileText1path);
print("fileText1Content :\n"+fileText1Content);
string[] fileText2Content = File.ReadAllLines(fileText2path);
print("fileText2Content :\n");
foreach (string str in fileText2Content) {
print(str);
}
byte[] fileText3Content = File.ReadAllBytes(fileText3path);
print("fileText3Content :\n");
foreach (byte str in fileText3Content)
{
print(str);
}
}
}
6、脚本编译正确后,回到Unity界面,在场景中新建一个“GameObject”,以便于震动效果观察,然后把脚本“FileTest ”赋给“GameObject”,具体如下图

7、运行场景,即可看到“Assets”路径下多了刚才的写入保存的文件,并比对内容是为写入内容,具体如下图

8、到此,《Unity 经典教程 之 快速学会File文件读操作》讲解结束,谢谢