【问题标题】:How to read and display all of the data inside a textfile如何读取和显示文本文件中的所有数据
【发布时间】:2019-04-13 05:04:26
【问题描述】:

目前我正在开发一款文字益智游戏,但我无法进行无尽模式。所以我现在要做的是我有一个包含数千个单词的文本文件(字典),我想一个一个地显示每个数据

例如:我有一个按钮,每次点击它都会显示第一个单词,如果我再次点击它,第二个单词会显示类似

到目前为止,我尝试的是通过执行类似这样的操作来显示文本文件中的第一个单词

TextAsset myTextAsset = Resources.Load("dictionary") as TextAsset;
string myString = myTextAsset.text;

Debug.Log(myString)

有人可以帮我弄清楚怎么做吗?

【问题讨论】:

  • 那么当你运行你的代码时会发生什么?
  • @MickyD 它只能在我的文本文件中显示第一个单词
  • 要显示单词,首先应该将整个文本分成单词。

标签: c# unity3d game-development


【解决方案1】:

我知道了。

public static List<string> textArray;

public Text textComp;

public int[] rowsToReadFrom;

public string FileName;

private TextAsset myTextAsset;
private void Start()
{
    myTextAsset = Resources.Load("dictionary") as TextAsset;
    string myString = myTextAsset.text;
}

public void readTextFile()
{
    textArray = myTextAsset.text.Split('\n').ToList();
    for(int i = 0; i < rowsToReadFrom.Length; i++)
    {
        if(rowsToReadFrom[0] < 0 || rowsToReadFrom.Length == 0)
        {
            textComp.text = myTextAsset.text;
        }
        else
        {
            textComp.text += textArray[rowsToReadFrom[i]] + "\n";
        }
    }

}

现在我唯一的问题是每次点击都会增加,但我想我可以处理它。

【讨论】:

  • 您可能应该在 readTextFile 的开头清除一次文本字段,以便以空文本开头:textComp.text = "";
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-22
  • 2011-11-07
  • 2010-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-17
相关资源
最近更新 更多