【问题标题】:unity3d c# typing to file fastunity3d c# 快速输入文件
【发布时间】:2017-11-10 18:19:07
【问题描述】:

我想将我输入的内容保存到一个文本文件中。下面的代码有效,但是当我输入太快时,文件没有时间包含所有输入的字符。 有人对如何解决这个问题提出建议吗?

using System.IO; 

string inp;

void Update () 
{
    if
    (
        Input.anyKeyDown
    )
    {

        inp=inp+(string)Input.inputString;

        string path="txt/txttst001.txt";

            File.WriteAllText
            (
                path, 
                inp
            );
    }
}

【问题讨论】:

  • 对文件的写入是同步的,因此不会因为写入而丢失数据,更可能的是 Update 函数调用速度不够快,如果 Update 绑定到 VSync 那么它将更新 60每秒次,所以它不会捕捉到快速按键。
  • 好文章!谢谢

标签: c# unity3d system.io.file


【解决方案1】:

尝试使用 StreamWriter:

using System.IO; 

string inp;

void Update () 

{
    if
    (
        Input.anyKeyDown
    )
    {

    inp=inp+(string)Input.inputString;

    string path="txt/txttst001.txt";

using (System.IO.StreamWriter file = new System.IO.StreamWriter(path,true)){
        file.Write(inp);
    }
}
}

有关 StreamWriter 的更多信息,您可以查看here

【讨论】:

  • 我也试过 StreamWriter,同样的问题发生了。文本文件写入太慢,文本文件中的字符没有实时更新。我想我需要一些缓冲区,但不知道该怎么做。有人吗?
【解决方案2】:

尝试在 Time Manager 中将 Fixed Timestep 更改为较低的值,并使用 FixedUpdate 而不是 Update。

https://docs.unity3d.com/Manual/class-TimeManager.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 2014-02-18
    • 2017-09-20
    相关资源
    最近更新 更多