c# - 如何通过 File.ReadAllLines 使用资源文本文件?

标签 c# unity-game-engine 2d-games

我有一个文件,其中包含一些用于加载预制件的文件名。我正在使用 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/

相关文章:

c# - 用于 OnSelect 和 OnDeselect 目的的委托(delegate)

facebook - Firebase 实时数据库下载使用率高

javascript - 通过 getElementById().innerHTML 编辑 div 没有任何变化

Java 游戏只按一次按钮

ios - 开始游戏时停止背景音乐

c# - 如何使用 ResourceExposureAttribute 和 ResourceConsumptionAttribute?

c# - 按当前显示的顺序获取选定的 DataGridViewRows

c# - 在分配给事件期间是否会复制委托(delegate)?

c# - 如何使用具有静态属性的静态类返回扩展抽象类的具体类的新实例

c# - 团结 : how to fade away scene for few seconds