【问题标题】:How do I fix a JavaScript error in a UNITY 3D project involving a System.IO.File member function?如何修复涉及 System.IO.File 成员函数的 UNITY 3D 项目中的 JavaScript 错误?
【发布时间】:2014-08-25 00:58:28
【问题描述】:

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

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'.

【问题讨论】:

  • 还没有,我不知道它可能是什么:(
  • 对你的问题投了赞成票.. 让更多人看到它:-)
  • 将测试并尝试修复问题,但直到 8:00 之后才能使用 Unity 3d 库。投票虽然有用
  • @user3722399 DirectoryNotFoundException 表示您需要该目录。创建一个名为 Reactor Games 的目录。
  • 请注意,UnityScript 与 Javascript 不同。 wiki.unity3d.com/index.php?title=UnityScript_versus_JavaScript

标签: unity3d .net unityscript


【解决方案1】:

如果您可以为方便添加一个 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 文档中找不到此信息。您应该谨慎使用它。

【讨论】:

  • DirectoryNotFoundException: 找不到路径“C:\Reactor Games\chunks.txt”的一部分。
  • 它给了我一个不适合此评论的错误,我会在我的原始帖子中发布它
猜你喜欢
  • 1970-01-01
  • 2019-06-10
  • 1970-01-01
  • 1970-01-01
  • 2019-09-12
  • 2013-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多