Ich habe einige Fehler über IOException: Sharing Verletzung. Mein Simulationsprogramm erstellt Textdatei pro 1/60s. Dieser Code wird von Lua-Skript geschrieben.Beim Lesen der Textdatei, habe ich einen Fehler über die Freigabe der Verletzung in Unity3D
Dies ist Lua-Code. Die Datei "data.txt" wird neu pro 1/60s geschrieben. So, Der Inhalt in der Datei wird pro 1/60s geändert.
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
Und Unity3D lesen diese Datei in Update() -Funktion.
Dies ist Unity3D C# -Code.
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);
}
}
In Unity3D, Dieser Fehler tritt auf.
Fehler
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)
Es ist möglich, diese Datei zu lesen? oder Gibt es eine alternative Methode?