我有一个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
,并且对于每个文件,此对话框都会打开
您知道为什么会这样吗?