【问题标题】:Save dictionary to text file (C#)将字典保存到文本文件 (C#)
【发布时间】:2016-09-18 11:37:11
【问题描述】:

我想将字典保存到 Unity3D 中的文本文件(使用 C#)。字典包含 GameObjects 作为键,对应于 Vector2 的列表。

这是我的代码:

public void SaveLevel(string LevelName) 
{
    using (StreamWriter file = new StreamWriter (LevelName+".txt"))
    {
        foreach (GameObject entity in levelStruct.Keys)
        { 
            file.WriteLine (entity.ToString () + ": " + levelStruct[entity].ToString());        
        }
    }
}

生成示例文件:

wall (UnityEngine.GameObject): System.Collections.Generic.List`1[UnityEngine.Vector2]
rat (UnityEngine.GameObject): System.Collections.Generic.List`1[UnityEngine.Vector2]
dwarf (UnityEngine.GameObject): System.Collections.Generic.List`1[UnityEngine.Vector2]
floor (UnityEngine.GameObject): System.Collections.Generic.List`1[UnityEngine.Vector2]

我的问题是文件需要包含 Vector2 列表中的实际项目,而不是列表本身:

System.Collections.Generic.List1[UnityEngine.Vector2]

我希望上面的行看起来像这样:

System.Collections.Generic.List1[(0,2),(3,1),(4,3)]

我将如何做到这一点?

【问题讨论】:

  • 您最好和最简单的调用是将您的字典转换为 JSON,大多数 json 解析器都会这样做,然后将 json 保存为文本。您不仅可以保存,还可以以可识别的格式获得它。

标签: c# list dictionary unity3d


【解决方案1】:

您可以创建特定类型的变量并获取所需的属性

   public void SaveLevel(string LevelName) 
    {
    string res;
        using (StreamWriter file = new StreamWriter (LevelName+".txt"))
        {
            foreach (GameObject entity in levelStruct.Keys)
            { 
                foreach(Vector2 v in levelStruct[entity])){
                      res = " "+"("+v.x+"."+v.y+")";
             }
          file.WriteLine (entity.ToString () + ": " + res);

            }
        }
    }

【讨论】:

  • 等等,这不会只写一个列表项吗?
【解决方案2】:

我之前没有使用过 Unity,如果我有误解,敬请原谅。如果 Vector2 是您创建的自定义类,那么您可以覆盖默认的 ToString() 方法,以便它输出您需要的任何字符串。或者,您需要访问您试图从字典中的值中写出的任何属性。例如(假设 Foo 是您的财产)。

file.WriteLine (entity.ToString() + ": " + levelStruct[entity].Foo.ToString());

【讨论】:

  • 感谢您的回答。不过,我还没有代表投票。如果你熟悉的话,Vector2 就像 python 中的元组,它包含在 api 中,当你想在 2D 环境中表示坐标时会派上用场。
【解决方案3】:

我认为你应该使用序列化和反序列化来保存和恢复你的对象,因为当你使用 ToString() 函数时,大多数时候只返回对象的名称。我建议你谷歌它,但有一些资源:

http://www.codeproject.com/Articles/483055/XML-Serialization-and-Deserialization-Part

https://msdn.microsoft.com/en-us/library/58a18dwa(v=vs.110).aspx

【讨论】:

  • 感谢您的提示!
【解决方案4】:

您正在编写对象。当您在对象上调用 ToString() 时,您将获得对象类型的名称。相反,您需要做以下两件事之一:

#1:如果您真的想按照您的代码建议使用 CSV 进行编写:

  Object.Property.ToString();

对于 GameObject,您可能需要使用反射来完成此操作

 using System.Reflection;
 foreach(PropertyInfo p in typeof(UnityEngine.GameObject).GHetProperties()){
     Console.WriteLine(p.Name+","+p.GetType().ToString());
 }
 //repeat as necessary with fields and methods and ... as needed using System.Reflection

第二项是向量列表——同样的。为此,您将使用

 foreach(UnityEngine.Vector2 vect in levelStruct[entity])){
    //write ToString();
  }

#2:了解如何将对象序列化为 XML 或 Json 并以这种方式保存: https://www.google.com/search?q=c-sharp+how+to+serialize+objects+into+json&ie=&oe=

祝你好运 - 如果您遇到特定问题,请询问。

【讨论】:

  • 感谢您的回答。会投票,但我还没有足够的代表。
  • 没问题!我不是为了代表而来——只是想帮助和学习。干杯!
  • @Bonbin 为你做了。
猜你喜欢
  • 2013-08-07
  • 1970-01-01
  • 2017-07-14
  • 2016-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-12
相关资源
最近更新 更多