我有一个文件,其中包含一些用于加载预制件的文件名。我正在使用 File.ReadAllLines(hardPathToFile) 加载文件,但这在构建时不起作用。您需要使用“资源”文件夹。所以我尝试使用 Resource.Load(fileName)
但是,这不会产生列表或数组。然后我尝试了 File.ReadAllLines(Resource.Load(fileName, typeOf(TextAsset)) as TextAsset))
但这不起作用,因为加载的资源不是字符串文件路径。
完整代码如下:
void getList(string filePath)
{
prefabObjects.Clear();
//Read all the lines from the file. File loaded from resources
print((Resources.Load(filePath, typeof(TextAsset)) as TextAsset).text);
var prefabs = File.ReadAllLines((Resources.Load(filePath, typeof(TextAsset)) as TextAsset).text);
print(prefabs);
foreach (var prefab in prefabs)
{
print(prefab);
GameObject newPrefab = Resources.Load(prefab, typeof(GameObject)) as GameObject;
prefabObjects.Add(newPrefab);
}
}
我怎样才能让它发挥作用?
感谢所有帮助,谢谢:)
最佳答案
如果您以前使用过文件的硬路径,我猜您做了类似 /path/to/file.txt
的操作。与Resources.Load()您没有指定文件结尾 (file.txt
-> file
)。
这是我在项目中读取文本文件内容的操作:
var api = Resources.Load("ConnectionString") as TextAsset;
var apiKey = api.text;
关于c# - 如何通过 File.ReadAllLines 使用资源文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41893171/