统一.txt文件到cstring

时间:2017-04-21 03:21:12

标签: c# unity3d

在Unity中,我需要在.txt文件中显示UI Text对象,就像书本一样。它需要像书一样工作,所以我只有两个单独的UI文本对象并排处理,就像页面一样,然后是每个按钮的箭头,用于下一页和上一页。我需要解析一个.txt文件(我正在尝试使用C-Strings,所以我会在.txt文件中填充一个字符串数组,不确定这是否是最佳路由?)每300个字符,因为这是最大量这将适合缩小为看起来像真实书籍的小Text对象。

所以我需要将一个巨大的.txt文件解析成300个字符的组,例如10,000个字符。然后,每个300个字符的组将在它自己的数组中,就像一个页面一样。我已经能够在UI Text对象上显示.txt文件,但解析是一个大问题。当用户点击下一个按钮时,在标题页之后,它将显示page1和page2,然后依此类推。任何人都可以帮助或提出建议我在这个开发过程中一切都会有所帮助。

1 个答案:

答案 0 :(得分:0)

取自此处(http://www.vbforums.com/showthread.php?777441-Split-String-into-multiple-strings-with-a-character-limit-w-out-splitting-words

C#的翻译版本。我确定它可以被优化,但应该做到这一点。

public static IList<string> ChunkText(string text, int characterLimit)
{
    List<string> parts = new List<string>();

    while (text.Length > 0)
    {
        if (text.Length <= characterLimit)
        {
            parts.Add(text);
            text = String.Empty;
        }
        else
        {
            int length = characterLimit;
            while (char.IsLetter(text[length])
                    && char.IsLetter(text[length - 1]))
            {
                length--;
            }

            parts.Add(text.Substring(0, length).Trim());
            text = text.Substring(length);
        }
    }

    return parts;
}


static void Main()
{
    string text = "The quick brown fox jumped over the lazy dog.";
    IList<string> parts = ChunkText(text, 10);
    foreach (string part in parts)
    {
        Console.WriteLine(part);
    }

    Console.ReadLine();
}