文件存在于编辑器中但在 android 导出中找不到?

File exists work in editor but not found in android export?

我实际上创建了一个脚本,其中我首先通过路径定位视频文件,并为 System.IO.File.Exists(filePath) 使用相同的路径。我设置的条件是 P1.GetComponent()。已启用 = 假;如果文件存在且 P1.GetComponent().enabled=true;如果文件不存在并给出引用 P1(还有其他)。并调用更新函数中的函数进行连续检查。它工作是因为它无法找到文件(文件或路径的位置是正确的我已经检查了很多次)并且 returns true 但其他条件不起作用。我还尝试在 运行 应用程序上进行调试,我得到了这个

 6580  6607 I Unity   :  file:///storage/emulated/0/Android/data/com.app.app/cache/folder23/video.mp4
 6580  6607 I Unity   :  
 6580  6607 I Unity   : (Filename: ./artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

之前我还创建了一个删除按钮(在编辑器和 android 两者中)并且它使用相同的路径但不知道是什么导致它在 android 中导出时不起作用。

这是代码 -

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

   if (System.IO.File.Exists(filePath)) {             
   Panel.GetComponent<Canvas> ().enabled = false;
                                        }
   else if(!System.IO.File.Exists(filePath)) {          
   Panel.GetComponent<Canvas> ().enabled = true;
                                             } 

如果我明白了,你在编辑器中的 Application.temporaryCachePath 下有一些文件,你认为它会在设备上的这个路径下?那是错误的假设。如果您想使用任何 'external' 文件(不是项目资产),您有两个选择。

  1. 在您的资产文件夹中创建 "Resources" 文件夹并将视频放在那里。不幸的是,您必须将扩展名更改为“.bytes”或 Here 中的任何其他名称,以便 Unity 可以将其读取为资源资产。然后你可以使用加载它 TextAsset bin = Resources.Load("video") as TextAsset; 请记住,您不会在设备上的 Application.dataPath + "Resources" 或任何其他路径下找到此文件夹。它正在被统一处理并打包到构建中,从那里加载文件的唯一方法是使用 Resources class.

  2. 其他方法是在您的资产文件夹中创建 StreamingAssets 文件夹。这是进入构建 'raw' 的特殊文件夹。因此,您放置在那里的所有内容都将未经修改地进入 Application.streamingAssetsPath 下的构建。但是,在 Android 上它有点复杂,因为 streamingAssetsPath 指向 .apk 文件,您需要先从 apk 中提取文件。 Here is described how to do that。我宁愿推荐你使用资源。