如何在涉及System.IO.File成员函数的UNITY 3D项目中修复JavaScript错误?

时间:2014-07-03 19:08:34

标签: unity3d .net unityscript

我在这里有一个存储块的坐标的函数。看起来很好,但控制台给了我这个错误:

BCE0019: 'WriteLine' is not a member of 'System.IO.StreamReader'

如果你有任何想法如何解决这个问题,我非常感谢。

function writeChunks(fileName : String, xco : int, yco : int, zco : int) {
    var sr = System.IO.File.OpenText(fileName);
    sr.WriteLine("Chunk ("
                    + xco + ", " 
                    + yco + ", " 
                    + zco + ")");
    sr.Close();
}

更新1 这段代码:

 function writeChunks(fileName : String, xco : int, yco : int, zco : int) {
    var sr = System.IO.File.OpenText(fileName);
    System.IO.File.WriteAllText("C:/youfilename.txt", "Chunk ("
                + xco + ", " 
                + yco + ", " 
                + zco + ")");
    sr.Close();
    }

给我这个错误:

    DirectoryNotFoundException: Could not find a part of the path "C:\Reactor Games\chunks.txt".
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.IO/FileStream.cs:292)
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.StreamWriter..ctor (System.String path, Boolean append, System.Text.Encoding encoding, Int32 bufferSize) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.IO/StreamWriter.cs:124)
System.IO.StreamWriter..ctor (System.String path, Boolean append)
(wrapper remoting-invoke-with-check) System.IO.StreamWriter:.ctor (string,bool)
System.IO.File.CreateText (System.String path) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.IO/File.cs:159)
infiniteTerrain+$saveLoadedChunk$5+$.MoveNext () (at Assets/Script/infiniteTerrain.js:61)

更新2 这段代码:

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

给我这个错误:

BCE0005: Unknown identifier: 'StreamWriter'.

1 个答案:

答案 0 :(得分:0)

如果您可以为方便起见添加C#类,那么您只需调用它即可。但似乎你也可以在JavaScript中使用它:

还要确保您的目录存在:

System.IO.File.WriteAllText(filepathIncludingFileName, "Chunk ("
                + xco + ", " 
                + yco + ", " 
                + zco + ")");

我还检查过其他地方有些人提到你可以在JavaScript部分使用StreamWriter类:

var sw : StreamWriter = new StreamWriter(filepathIncludingFileName);
sw.WriteLine("Line to write");
sw.WriteLine("Another Line");
sw.Flush();
sw.Close();

很遗憾,我无法在Unity3D脚本API文档中找到此信息。你应该谨慎使用它。