Unity3D文件I / O StreamWriter文本文件

时间:2014-07-03 20:50:16

标签: unity3d .net unityscript

当我的脚本运行start()函数,该函数调用调用saveLoadedChunk()函数的writeChunks()函数时,我在文本文件中看到的只有Chunk(0,0,0)

需要发生的是,在文本文件中,每个块都用他的坐标写入(但只有一次)。我编写了以下脚本,但它只写(如我之前所说)Chunk(0,0,0)

是否有人可以调整此代码或给我一些帮助,以便脚本将所有块写入文件中,但只有一次。

非常感谢帮助。

脚本的一部分

function saveLoadedChunk() {
    var loadedChunks : GameObject[] = FindObjectsOfType(GameObject) as GameObject[];
    var fileName = "C:/Reactor Games/chunks.txt";
    for (var i = 0; i < loadedChunks.length ; i++) {
            if(loadedChunks[i].name.Substring(0,5) == "Chunk") {
                if(loadedChunks[i].tag == "Player") {
                }else{
                    if(System.IO.File.Exists(fileName)) {
                        Debug.Log("EXIST");
                        var xco1 = loadedChunks[i].transform.position.x;
                        var yco1 = loadedChunks[i].transform.position.y;
                        var zco1 = loadedChunks[i].transform.position.z;
                        writeChunks(fileName, xco1, yco1, zco1);
                    }else{
                        Debug.Log("NOTEXIST");
                        var sr = System.IO.File.CreateText(fileName);
                        var xco2 = loadedChunks[i].transform.position.x;
                        var yco2 = loadedChunks[i].transform.position.y;
                        var zco2 = loadedChunks[i].transform.position.z;
                        writeChunks(fileName, xco2, yco2, zco2);
                    }   
            }
        }
    }

}

function writeChunks(fileName : String, xco : int, yco : int, zco : int) {
    var stringToWrite = "Chunk (" + xco + ", " + yco + ", " + zco + ")";
    Debug.Log(stringToWrite);
    var sw : System.IO.StreamWriter = new System.IO.StreamWriter(fileName);
    sw.WriteLine(stringToWrite);
    sw.Flush();
    sw.Close();
}

1 个答案:

答案 0 :(得分:1)

在else分支中,输出文件被创建并保持打开状态,因此第一次调用writeChunks无法写入,因为它无法打开文件:

}else{
  Debug.Log("NOTEXIST");
  var sr = System.IO.File.CreateText(fileName); // This line opens the file and 
                                                // is unnecessary because the
                                                // StreamWriter in writeChunks will 
                                                // create the file if it does not 
                                                // already exist. Remove this line.
                                                // This entire else branch actually
                                                // is redundant and may be removed.
  var xco2 = loadedChunks[i].transform.position.x;
  var yco2 = loadedChunks[i].transform.position.y;
  var zco2 = loadedChunks[i].transform.position.z;
  writeChunks(fileName, xco2, yco2, zco2);
}

这只会影响第一次写入,但是在writeChunks中,每次都会覆盖文件的内容而不是附加到文件的内容。 StreamWriter类可选地接受第二个布尔参数,该参数允许您打开文件并附加到它:

var sw : System.IO.StreamWriter = new System.IO.StreamWriter(fileName, true);

您可能还希望考虑重构代码,这样您就不会经常打开和关闭文件,类似于以下内容:

function saveLoadedChunk() {
    var loadedChunks : GameObject[] = FindObjectsOfType(GameObject) as GameObject[];
    var fileName = "C:/Reactor Games/chunks.txt";
    var sw : System.IO.StreamWriter = new System.IO.StreamWriter(fileName);
    for (var i = 0; i < loadedChunks.length ; i++) {
        if(loadedChunks[i].name.Substring(0,5) == "Chunk") {
            if(loadedChunks[i].tag != "Player") {
                var xco1 = loadedChunks[i].transform.position.x;
                var yco1 = loadedChunks[i].transform.position.y;
                var zco1 = loadedChunks[i].transform.position.z;
                writeChunks(sw, xco1, yco1, zco1);
            }
        }
    }
    sw.Flush();
    sw.Close();
}

function writeChunks(sw : System.IO.StreamWriter, xco : int, yco : int, zco : int) {
    var stringToWrite = "Chunk (" + xco + ", " + yco + ", " + zco + ")";
    Debug.Log(stringToWrite);
    sw.WriteLine(stringToWrite);
}