我有一些关于IOException的错误:共享冲突。我的仿真程序每1/60秒创建一个文本文件。这段代码是由Lua脚本编写的。
这是Lua代码。data.txt文件每1/60秒重写一次。因此,文件中的内容每1/60秒更改一次。
function dataListener:post( t )
--local fn = "data"
local fn = "time"
--local filename = fn.."_"..60*t..".txt"
local filename = fn..".txt"
local file = io.open(filename, "w+")
--file:write(string.format("%d; %d; %d\n",x,y,z))
--file:write("%d; %d; %d\n", xx, yy, zz)
--file:write(xx/100," ", yy/100," ", zz/100," ", wxx/100," ", wyy/100," ", wzz/100," \n")
file:write(60*t," \n")
file:close()
end
然后Unity3D在Update()函数中读取此文件。
这是Unity3D C#代码。
using UnityEngine;
using System.Collections;
using System;
using System.IO;
public class Textparsing : MonoBehaviour
{
public GUISkin skin;
private int currentTextNumber;// text number
string s;
// Use this for initialization
void Start()
{
currentTextNumber = 0;
s = LoadTextFile("text/time.txt");
print(s);
}
// Update is called once per frame
void Update()
{
s = LoadTextFile("text/time.txt");
print(s);
}
string LoadTextFile(string fileName)
{
string t = "";
string line = "";
StreamReader sr = new StreamReader(Application.dataPath + "/Resources/" + fileName);
if (sr == null)
{
print("Error : " + Application.dataPath + "/Resources/" + fileName);
}
else
{
line = sr.ReadLine();
while (line != null)
{
t += line;
line = sr.ReadLine();
if (line != null)
{
t += "\n";
}
}
sr.Close();
// print("Loaded " + Application.dataPath + "/Resources/db/" + fileName);
}
return t;
}
//gui
void OnGUI()
{
//read
GUI.skin = skin;
int halfW = Screen.width / 2;
int halfH = Screen.height / 2;
int x = 40;
int y = halfH + 40;
GUI.Label(new Rect(x, y, halfW * 1, halfH * 1.2f), s);
}
}
在unity3D中,发生此错误。
错误
IOException: Sharing violation on path C:\Users\Public\Documents\Unity Projects\textparsing\Assets\Resources\text\time.txt
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:320)
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share)
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
System.IO.File.OpenRead (System.String path) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:363)
System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/StreamReader.cs:167)
System.IO.StreamReader..ctor (System.String path)
(wrapper remoting-invoke-with-check) System.IO.StreamReader:.ctor (string)
Textparsing.LoadTextFile (System.String fileName) (at Assets/Textparsing.cs:36)
Textparsing.Update () (at Assets/Textparsing.cs:29)
可以读取此文件吗?或有其他替代方法吗?
您很可能在尝试从其他程序写入文本文件时读取该文本文件。这是不可能的,您需要等到一个程序完成写入,关闭文件然后Unity可以打开和读取它。
另外,在Update中写入和读取文件也不是一个好主意。正如您已经体验到的那样,这些磁盘操作太多了,这将非常慢,并且也使同步变得不可能。
解决方案:这两个程序进行通讯的目的是什么?为什么这么快?如果您每隔一段时间只有一次更新,则可以使用FileSystemWatcher类监视文本文件,并且每次更改时,都会调用监视程序的更改事件。但是可能还有许多其他更好的解决方案可以解决您的问题。也许是SQL数据库,或者使其成为Unity,套接字通信的插件,无论用例如何,都取决于它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句