在Unity中如何检查文件是否存在?

Dea*_*g66 -1 android file file-handling unity-game-engine file-exists

我想在已知的路径中找到该文件,如果它在android中预先存在,则用它触发事件(启用/禁用画布).这是我用过的一个例子 -

public void FileChk(){

    string filePath = "file://" + Application.temporaryCachePath + "/" + "folder23" + "/" + fileName;

    if (!fileName.Exists)
    {
        //event
    }   
    else
    {       
        //event     
    }
}
Run Code Online (Sandbox Code Playgroud)

我在这里做错了什么以及如何在文件存在时触发此事件.

Ian*_* H. 5

您可以使用System.IO命名空间.

public void FileChk()
{
    string filePath = "file://" + Application.temporaryCachePath + "/" + "folder23" + "/" + fileName;

    if (System.IO.File.Exists(filePath))
    {
        // The file exists -> run event
    }
    else
    {
        // The file does not exist -> run event
    }
}
Run Code Online (Sandbox Code Playgroud)

该方法bool System.IO.File.Exists(string fileName)返回一个值,指示文件是否存在.