Unity 在构建项目时将场景和资源合并到生成的播放器中的二进制文件中。但是,您可以将文件放入目标计算机上的普通文件系统中,使其可通过路径名访问。例如,如果在 iOS 设备上部署了电影文件,原始电影文件必须位于文件系统中的某个位置,以便使用 PlayMovie 函数进行播放。如果要在播放器安装中直接分发 AssetBundles 而不是按需下载,也可以将其包含在此文件夹中。
Unity 会将放置在 Unity 项目中名为 StreamingAssets(区分大小写)的文件夹中的所有文件逐字复制到目标计算机上的特定文件夹中。要检索文件夹,请使用 Application.streamingAssetsPath 属性。在任何情况下,最好使用 Application.streamingAssetsPath 来获取 StreamingAssets 文件夹的位置,因为它总是指向运行应用程序的平台上的正确位置。
Application.streamingAssetsPath 返回的位置因平台而异:
Application.dataPath + "/StreamingAssets"。Application.dataPath + "/Resources/Data/StreamingAssets"。Application.dataPath + "/Raw"。"jar:file://" + Application.dataPath + "!/assets"。Application.streamingAssetsPath 返回一个 HTTP 网址,指向 Web 服务器上的 StreamingAssets/ 路径。例如,当应用程序在本地开发服务器上运行时,会返回 http://localhost:8000/unity_webgl_build/StreamingAssets/。在 Android 和 Web 平台上,无法通过文件系统 API 和 streamingAssets 路径直接访问流媒体资源文件,因为这些平台会返回网址。改用 UnityWebRequest 类来访问内容。
注意:
streamingAssets 路径为只读。不要在运行时修改新文件或将新文件写入 streamingAssets 目录。