【发布时间】:2016-10-04 07:43:24
【问题描述】:
我有一些关于 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)
可以读取这个文件吗?或者有什么替代方法吗?
【问题讨论】:
标签: c# parsing unity3d lua stream