C#读取文本文件停止工作-使用Unity [英] C# Reading of text file stopped working - with Unity

查看:105
本文介绍了C#读取文本文件停止工作-使用Unity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,该程序可以读取目录中任意数量的.txt文件,并将它们组合在一个列表中,然后一次向用户显示此列表.用户此时做出决定并存储反应时间.最近,文本导入脚本无缘无故停止了工作.我已经几个月没有更改任何文本导入代码了.

我已经检查了读取文件中的代码,根本什么也没发现.

这是Unity计划的一部分,仅供参考.

TextImport

using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;

public class TextImport : MonoBehaviour {

    public static List<string> TextLines;
    bool FileRead = false;

    // Use this for initialization
    void Awake() {
        TextLines = new List<string>();
        string path;
        if(Application.platform == RuntimePlatform.WindowsEditor)
        {
            path = Application.dataPath + "/Resources/";
        }
        else if (Application.platform == RuntimePlatform.WindowsPlayer)
        {
            path = Application.dataPath;
        }
        else if (Application.platform == RuntimePlatform.Android)
        {
            FileRead = true;
            path = "/StroopTest/";
            TextAsset Allfiles = Resources.Load("Android/AllText") as TextAsset;
            string[] AllLinesAndroid = Allfiles.text.Split("\n"[0]);
            foreach (string Line in AllLinesAndroid)
            {
                TextLines.Add(Line);
                break;
            }
        }
        else
        {
            path = Application.dataPath;
        }
        if (!FileRead)
        {
            DirectoryInfo info = new DirectoryInfo(path);
            FileInfo[] fileInfo = info.GetFiles("*.txt");//I've changed the "*.txt" to nothing to read all files instead of just .txt files
            foreach (FileInfo file in fileInfo)
            {
                //I created another List here to store the file names that were read, however none were.
                string[] AllLines = File.ReadAllLines(file.FullName);
                foreach (string line in AllLines)
                {
                    TextLines.Add(line);
                }
            }
        }
    }
}

解决方案

我建议使用Application.persistentDataPath.

Application.dataPath是只读目录,您可以在其中找到打包的游戏相关资产.

Application.persistentDataPath是一个可写目录,它返回一个路径(不同平台不同),您可以在其中在运行时添加内容(例如:编写文件,下载文件等)

我希望能帮上忙.

I have a program that reads in any amount of .txt files in a directory and assembles them in a list, this list is then displayed to the user one at a time. The user makes a decision at this point and the reaction time is stored. Recently the text import script stopped working for no reason. I haven't changed any of the text import code in months.

I've checked the code that reads in the files and nothing was being recognised at all.

This is part of Unity program just FYI.

TextImport

using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;

public class TextImport : MonoBehaviour {

    public static List<string> TextLines;
    bool FileRead = false;

    // Use this for initialization
    void Awake() {
        TextLines = new List<string>();
        string path;
        if(Application.platform == RuntimePlatform.WindowsEditor)
        {
            path = Application.dataPath + "/Resources/";
        }
        else if (Application.platform == RuntimePlatform.WindowsPlayer)
        {
            path = Application.dataPath;
        }
        else if (Application.platform == RuntimePlatform.Android)
        {
            FileRead = true;
            path = "/StroopTest/";
            TextAsset Allfiles = Resources.Load("Android/AllText") as TextAsset;
            string[] AllLinesAndroid = Allfiles.text.Split("\n"[0]);
            foreach (string Line in AllLinesAndroid)
            {
                TextLines.Add(Line);
                break;
            }
        }
        else
        {
            path = Application.dataPath;
        }
        if (!FileRead)
        {
            DirectoryInfo info = new DirectoryInfo(path);
            FileInfo[] fileInfo = info.GetFiles("*.txt");//I've changed the "*.txt" to nothing to read all files instead of just .txt files
            foreach (FileInfo file in fileInfo)
            {
                //I created another List here to store the file names that were read, however none were.
                string[] AllLines = File.ReadAllLines(file.FullName);
                foreach (string line in AllLines)
                {
                    TextLines.Add(line);
                }
            }
        }
    }
}

解决方案

I suggest using Application.persistentDataPath.

Application.dataPath is a read-only directory, where (if packaged to do so) you can find game-related assets there.

Application.persistentDataPath, however, is a writeable directory and returns a path (different for different platforms) where you can add stuff at runtime (ex: writing a file, downloading a file, etc.)

I hope that helps.

这篇关于C#读取文本文件停止工作-使用Unity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆