Unity3D失去焦点时,“无法读取文件”

时间:2019-11-02 13:44:12

标签: unity3d

我有一个Arduino,可通过蓝牙将传感器数据(例如GSR)发送到Unity3D。根据采样率,我每秒可得到15-45个值。为了保存这些值,我创建了一个包含Dictionary的类,该类将传感器名称映射到StreamWriter,如果接收到数据,则相应的StreamWriter会将其写入文件:

void Start()
{
    path = Application.dataPath + "/Logs/" + directoryName;
    Directory.CreateDirectory(path);
    dataLogDict = new Dictionary<string, StreamWriter>();
    if (logData) EventSystem.Current.RegisterListener<ParsedDataEventInfo>(WriteDataToFile);
}


public void WriteDataToFile(ParsedDataEventInfo parsedData)
{
    if (!dataLogDict.ContainsKey(parsedData.SensorName))
    {
        string date = DateTime.Now.ToString("dd.MM.yyyy_HH-mm-ss.fff");
        StreamWriter sw = File.AppendText(path + "/" + parsedData.SensorName + "_sensor_" + date + ".txt");
        sw.AutoFlush = true;

        dataLogDict.Add(parsedData.SensorName, sw);
    }

    var time = DateTime.Now.ToString("dd.MM.yyyy_HH-mm-ss.fff");
    var newLine = string.Format("{0},{1}", time, parsedData.SensorValue);
    dataLogDict[parsedData.SensorName].WriteLine(newLine);

} code here

一切正常,但是如果Unity3D失去焦点,即我专注于另一个应用程序,则会收到错误File couldn't be read,并且对于每个文件,此对话框都会打开

enter image description here

您知道为什么会这样吗?

0 个答案:

没有答案