我有一个for循环,它可以获得所有" chunks"在我的场景中获取X,Y和Z坐标,然后将这些坐标及其名称写入文本文件。它适用于所有块。这个for循环位于一个名为saveLoadedChunk的函数中。每分钟调用此函数以保存块。但问题是,当玩家在10分钟前站在同一区域时,它会将相同的块和结果保存到包含所有块的文本文件中,但会多次。我不希望这种情况发生,所以编写了一小段代码,将文本文件的所有块放在一个数组中,并将它们与要保存的块进行比较。如果是这样的话,它又不会再保存这个块。但是在添加一些Debug.Log()之后它无法正常工作;函数,我发现第20行的if语句总是采用else分支。我不知道为什么,并感谢所有的帮助。
非常感谢,
代码:
function saveLoadedChunk() {
var loadedChunks : GameObject[] = FindObjectsOfType(GameObject) as GameObject[];
var fileName = "C:/Reactor Games/chunks.txt";
var fileText = System.IO.File.ReadAllText(fileName);
var lines = fileText.Split("\n"[0]);
var dontWrite = false;
var sw : System.IO.StreamWriter = new System.IO.StreamWriter(fileName, true);
for (var i = 0; i < loadedChunks.length ; i++) {
if(loadedChunks[i].name.Substring(0,5) == "Chunk" || loadedChunks[i].name.Substring(0,5) == "_TERR") {
if(loadedChunks[i].tag != "Player") {
var xco = loadedChunks[i].transform.position.x;
var yco = loadedChunks[i].transform.position.y;
var zco = loadedChunks[i].transform.position.z;
var stringToWrite = "Chunk (" + xco + ", " + yco + ", " + zco + ")";
for(var chunkName in lines) {
if(chunkName.Equals(stringToWrite)) {
Debug.LogError("dontwrite = true");
dontWrite = true;
}
else{
dontWrite = false;
}
}
if(!dontWrite){sw.WriteLine(stringToWrite);}
}
}
}
sw.Flush();
sw.Close();
}
文本文件示例(如何编写文件,不得写两次)
Chunk (100, 0, -100)
Chunk (100, 0, -50)
Chunk (100, 0, 0)
Chunk (100, 0, 50)
Chunk (100, 0, 100)
Chunk (50, 0, -100)
Chunk (50, 0, 100)
Chunk (0, 0, -100)
Chunk (0, 0, 100)
Chunk (-50, 0, -100)
Chunk (-50, 0, 100)
Chunk (-100, 0, -100)
Chunk (-100, 0, -50)
Chunk (-100, 0, 0)
Chunk (-100, 0, 50)
Chunk (-100, 0, 100)
Chunk (0, 0, -50)
Chunk (0, 0, 50)
Chunk (-50, 0, -50)
Chunk (-50, 0, 0)
Chunk (-50, 0, 50)
Chunk (50, 0, -50)
Chunk (50, 0, 0)
Chunk (50, 0, 50)
Chunk (0, 0, 0)
错误的文本文件示例: 正如您在下面的文本文件中看到的那样,每次saveLoadedChunk();都可以看到它反复写入相同的块。函数被调用。
Chunk (0, 0, -100)
Chunk (0, 0, 100)
Chunk (-50, 0, -100)
Chunk (-50, 0, 100)
Chunk (-100, 0, -100)
Chunk (-100, 0, -50)
Chunk (-100, 0, 0)
Chunk (-100, 0, 50)
Chunk (-100, 0, 100)
Chunk (0, 0, -50)
Chunk (0, 0, 50)
Chunk (-50, 0, -50)
Chunk (-50, 0, 0)
Chunk (-50, 0, 50)
Chunk (50, 0, -50)
Chunk (50, 0, 0)
Chunk (50, 0, 50)
Chunk (0, 0, 0)
Chunk (100, 0, -100)
Chunk (100, 0, -50)
Chunk (100, 0, 0)
Chunk (100, 0, 50)
Chunk (100, 0, 100)
Chunk (50, 0, -100)
Chunk (50, 0, 100)
Chunk (0, 0, -100)
Chunk (0, 0, 100)
Chunk (-50, 0, -100)
Chunk (-50, 0, 100)
Chunk (-100, 0, -100)
Chunk (-100, 0, -50)
Chunk (-100, 0, 0)
Chunk (-100, 0, 50)
Chunk (-100, 0, 100)
Chunk (0, 0, -50)
Chunk (0, 0, 50)
Chunk (-50, 0, -50)
Chunk (-50, 0, 0)
Chunk (-50, 0, 50)
Chunk (50, 0, -50)
Chunk (50, 0, 0)
Chunk (50, 0, 50)
Chunk (0, 0, 0)
Chunk (100, 0, -100)
Chunk (100, 0, -50)
Chunk (100, 0, 0)
Chunk (100, 0, 50)
Chunk (100, 0, 100)
Chunk (50, 0, -100)
Chunk (50, 0, 100)
Chunk (0, 0, -100)
Chunk (0, 0, 100)
Chunk (-50, 0, -100)
Chunk (-50, 0, 100)
Chunk (-100, 0, -100)
Chunk (-100, 0, -50)
Chunk (-100, 0, 0)
Chunk (-100, 0, 50)
Chunk (-100, 0, 100)
Chunk (0, 0, -50)
Chunk (0, 0, 50)
Chunk (-50, 0, -50)
Chunk (-50, 0, 0)
Chunk (-50, 0, 50)
Chunk (50, 0, -50)
Chunk (50, 0, 0)
Chunk (50, 0, 50)
Chunk (0, 0, 0)
Chunk (100, 0, -100)
Chunk (100, 0, -50)
Chunk (100, 0, 0)
Chunk (100, 0, 50)
Chunk (100, 0, 100)
Chunk (50, 0, -100)
Chunk (50, 0, 100)
Chunk (0, 0, -100)
Chunk (0, 0, 100)
Chunk (-50, 0, -100)
Chunk (-50, 0, 100)
Chunk (-100, 0, -100)
Chunk (-100, 0, -50)
Chunk (-100, 0, 0)
Chunk (-100, 0, 50)
Chunk (-100, 0, 100)
Chunk (0, 0, -50)
Chunk (0, 0, 50)
Chunk (-50, 0, -50)
Chunk (-50, 0, 0)
Chunk (-50, 0, 50)
Chunk (50, 0, -50)
Chunk (50, 0, 0)
Chunk (50, 0, 50)
Chunk (0, 0, 0)
Chunk (100, 0, -100)
Chunk (100, 0, -50)
Chunk (100, 0, 0)
Chunk (100, 0, 50)
Chunk (100, 0, 100)
Chunk (50, 0, -100)
Chunk (50, 0, 100)
Chunk (0, 0, -100)
Chunk (0, 0, 100)
Chunk (-50, 0, -100)
Chunk (-50, 0, 100)
Chunk (-100, 0, -100)
Chunk (-100, 0, -50)
Chunk (-100, 0, 0)
Chunk (-100, 0, 50)
Chunk (-100, 0, 100)
Chunk (0, 0, -50)
Chunk (0, 0, 50)
Chunk (-50, 0, -50)
Chunk (-50, 0, 0)
Chunk (-50, 0, 50)
Chunk (50, 0, -50)
Chunk (50, 0, 0)
Chunk (50, 0, 50)
Chunk (0, 0, 0)
更新1: 感谢罗伯特,我将代码更改为以下内容。但是由于某种原因,它仍然没有调试dontwrite ...非常感谢帮助,当它工作时我会接受答案+我对每个帮助我的答案都给予了支持。
function saveLoadedChunk() {
var loadedChunks : GameObject[] = FindObjectsOfType(GameObject) as GameObject[];
var fileName = "C:/Reactor Games/chunks.txt";
var fileText = System.IO.File.ReadAllText(fileName);
var lines = fileText.Split("\n"[0]);
var write = true;
var sw : System.IO.StreamWriter = new System.IO.StreamWriter(fileName, true);
for (var i = 0; i < loadedChunks.length ; i++) {
if(loadedChunks[i].name.Substring(0,5) == "Chunk" || loadedChunks[i].name.Substring(0,5) == "_TERR" || loadedChunks[i].name.Substring(0,5) == "_ACTI") {
if(loadedChunks[i].tag != "Player") {
var xco = loadedChunks[i].transform.position.x;
var yco = loadedChunks[i].transform.position.y;
var zco = loadedChunks[i].transform.position.z;
var stringToWrite = "Chunk (" + xco + ", " + yco + ", " + zco + ")";
write = true;
for(var chunkName in lines) {
if(chunkName.Equals(stringToWrite)) {
Debug.LogError("dontwrite = true");
write = false;
break;
}
}
if(write){sw.WriteLine(stringToWrite);}
}
}
}
sw.Flush();
sw.Close();
}
答案 0 :(得分:2)
这是因为你的for循环在找到匹配时不会转义。只要行中的LAST对象不等于stringToWrite,它就会写入。将代码更改为以下内容:
dontWrite=false;
for(var chunkName in lines) {
if(chunkName.Equals(stringToWrite)) {
Debug.LogError("dontwrite = true");
dontWrite = true;
break;
}
}