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.
2、File.Delete:
1)函数形式
public static void Delete(string path);
2)参数解释
pathThe name of the file to be deleted.
3)简单描述
Deletes the specified file.
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";
// Use this for initialization
void Start () {
bool isExist = File.Exists(fileText1path);
print(@"Assets\FileText1.txt IsExist :" + isExist);
File.Delete(fileText1path);
isExist = File.Exists(fileText1path);
print(@"Assets\FileText1.txt IsExist :" + isExist);
}
}
6、脚本编译正确后,回到Unity界面,在场景中新建一个“GameObject”,以便于震动效果观察,然后把脚本“FileTest ”赋给“GameObject”,具体如下图

7、运行场景,即可看到“Assets”路径下指定文件已被删除,并且控制台Console上的打印也与预期一致,具体如下图


8、到此,《Unity 经典教程 之 快速学会File删除指定文件》讲解结束,谢谢