【发布时间】:2021-10-09 01:43:35
【问题描述】:
我目前将列表存储在 .csv 文件中,但是,当我构建 .apk 时,就像这些文件根本不存在一样。我如何设法将这些文件保存在构建中以便一切正常?
这是我目前针对这种情况的代码的 sn-p
private static string TODCSVPath = "/Scripts/TOD/TOD_Questions.csv";
public void GenerateTODS() //called in Awake()
{
string[] allLines = File.ReadAllLines(Application.dataPath + TODCSVPath);
foreach (string uncas in allLines)
{
questions.Add(uncas);
}
}
在编辑器中一切正常,但我真的很难理解我需要修复什么。我搜索了一下,有人说使用string[] allLines = File.ReadAllLines(Application.persistentDataPath + TODCSVPath); 而不是string[] allLines = File.ReadAllLines(Application.dataPath + TODCSVPath); 但这会立即在控制台中引发错误。
还看到了一些关于创建TextAsset 然后从中读取文件的内容,但我根本无法做到。
注意:不知道这是否有帮助,但我所有的 .csv 文件都只有一列!
任何帮助或指导将不胜感激。谢谢!
【问题讨论】:
-
建议使用 Application.persistentDataPath 是一个不错的选择。确保路径确实存在(如果需要,创建文件夹)。 persistentDataPath 不会在您的项目文件夹中,通过打印调试检查它的位置
-
只有资源文件夹中的文件是构建的一部分。 docs.unity3d.com/ScriptReference/Resources.Load.html