在 Unity 中重新加载场景后 StreamReader 无法正常工作
StreamReader not working correctly after reloading scene in Unity
我有一个流 reader 我用它来将数据解析到程序中。当我第一次从主菜单打开场景时,流 reader 在解析数据时工作正常(由于代码很长,我在代码片段中省略了解析)。第一个 运行 上屏幕的 text.text 输出是 G。如果我返回主菜单,然后返回到此场景,但是 text.text 输出的第一行是我的代码显示 text.text = line 的文本文档,我试图找出原因。 sr.Close() 不是关闭流 reader 的正确方法吗?或者文件是否仍然以某种方式打开?
void Start () {
text.text = "A";
trimAndPlaceDataSets = GetComponent<TrimAndPlaceDataSets> ();
text.text = "B";
string AthenaData = Application.streamingAssetsPath + "/Athena.txt";
text.text = "C";
PopulateAthenaData(AthenaData);
}
void PopulateAthenaData(string s){
text.text = "D";
using (StreamReader sr = new StreamReader(s)) {
text.text = "E";
string line = sr.ReadLine();
text.text = line;
while (line != null) {
//Do Stuf
sr.ReadLine();
}
sr.Close();
text.text = "G";
}
}
这就是我加载场景的方式
public void LoadPressed() {
scenesDropdown.gameObject.SetActive(false);
loadButton.SetActive(false);
SceneManager.LoadScene(scenesArray[scenesDropdown.value]);
}
以正确的方式为您使用 do close stream,这是因为 StreamReader class 已经实现了 IDisposable 接口,这允许使用 using 语句,该语句在范围结束后自动调用 SreamReader 中的 Dispose 方法和该方法清除资源并关闭打开的文件,如果需要,最后一步称为垃圾收集器
using (StreamReader sr = new StreamReader(s))
{
//do stuff with stream reader
//sr.Close();
}
您的问题是关于 "Garbage Collection"(访问此 Url:“https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/”)
简单的答案是 C# 可以在您使用它们之后释放(处置)很多东西,但是 "files" 之类的东西不是其中之一。你必须在使用后释放它们,如果你不这样做,你的文件将被打开并且另一个进程无法访问它。您需要在工作完成后处理 "StreamReader"。 "using" 这样做(您不需要为此编写自定义处理器)。
using (StreamReader sr = new StreamReader("Your file path"))
{
//do your work
}
之后文件会自动释放
我有一个流 reader 我用它来将数据解析到程序中。当我第一次从主菜单打开场景时,流 reader 在解析数据时工作正常(由于代码很长,我在代码片段中省略了解析)。第一个 运行 上屏幕的 text.text 输出是 G。如果我返回主菜单,然后返回到此场景,但是 text.text 输出的第一行是我的代码显示 text.text = line 的文本文档,我试图找出原因。 sr.Close() 不是关闭流 reader 的正确方法吗?或者文件是否仍然以某种方式打开?
void Start () {
text.text = "A";
trimAndPlaceDataSets = GetComponent<TrimAndPlaceDataSets> ();
text.text = "B";
string AthenaData = Application.streamingAssetsPath + "/Athena.txt";
text.text = "C";
PopulateAthenaData(AthenaData);
}
void PopulateAthenaData(string s){
text.text = "D";
using (StreamReader sr = new StreamReader(s)) {
text.text = "E";
string line = sr.ReadLine();
text.text = line;
while (line != null) {
//Do Stuf
sr.ReadLine();
}
sr.Close();
text.text = "G";
}
}
这就是我加载场景的方式
public void LoadPressed() {
scenesDropdown.gameObject.SetActive(false);
loadButton.SetActive(false);
SceneManager.LoadScene(scenesArray[scenesDropdown.value]);
}
以正确的方式为您使用 do close stream,这是因为 StreamReader class 已经实现了 IDisposable 接口,这允许使用 using 语句,该语句在范围结束后自动调用 SreamReader 中的 Dispose 方法和该方法清除资源并关闭打开的文件,如果需要,最后一步称为垃圾收集器
using (StreamReader sr = new StreamReader(s))
{
//do stuff with stream reader
//sr.Close();
}
您的问题是关于 "Garbage Collection"(访问此 Url:“https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/”) 简单的答案是 C# 可以在您使用它们之后释放(处置)很多东西,但是 "files" 之类的东西不是其中之一。你必须在使用后释放它们,如果你不这样做,你的文件将被打开并且另一个进程无法访问它。您需要在工作完成后处理 "StreamReader"。 "using" 这样做(您不需要为此编写自定义处理器)。
using (StreamReader sr = new StreamReader("Your file path"))
{
//do your work
}
之后文件会自动释放