【发布时间】:2014-04-08 18:49:12
【问题描述】:
我正在使用 Unity 的 protobuf-net 来加载关卡的数据。序列化/反序列化在编辑器中运行时有效,但是当我编译并创建构建然后运行它时,我在反序列化时遇到以下异常:
InvalidOperationException: No serializer defined for type:
System.Collections.Generic.KeyValuePair`2[[System.String,
mscorlib, Version=2.0.5.0, Culture=neutral,
PublicKeyToken=7cec85d7bea7798e],[System.Int32, mscorlib, Version=2.0.5.0,
Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]
我不确定 protobuf-net 在使用它们进行反序列化之前如何分析类型,所以我不确定如何解决这个问题。如果有人知道可能导致此问题的原因,我将非常感谢您的解释。我可以自己定义反序列化方法吗?某种类型的自定义反序列化器之类的东西?谢谢!
编辑:
这是我认为导致问题的课程:
[ProtoContract]
public class LevelData
{
[ProtoMember(1)]
public string Background = "";
[ProtoMember(2)]
public string Name = "";
[ProtoMember(3)]
public Dictionary<string, int> PreloadInfo {get; private set;}
[ProtoMember(4)]
public List<string> RoomFiles { get; private set; }
public LevelData()
{
PreloadInfo = new Dictionary<string, int>();
RoomFiles = new List<string>();
}
}
运行 protogen 后,我注意到 Unity 和我创建的编译后的 dll 之间的 String 和 Int32 的版本不匹配。 Unity 尝试使用 2.0.5.0 版本反序列化,而我编译的 dll 使用 2.0.0.0 版本。我假设这就是反序列化失败的原因。这是 protogen 的输出:
Adding System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, msc
orlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]...
Adding System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib, Versi
on=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, ms
corlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]...
用于反序列化的代码:
var serializer = GnomingSerializer.Create ();
int startMillis = System.Environment.TickCount;
TextAsset levelDoc = (TextAsset)Resources.Load(path);
MemoryStream mStream = new MemoryStream(levelDoc.bytes);
//LevelData levelData = Serializer.Deserialize<LevelData>(mStream);
LevelData levelData = new LevelData();
serializer.Deserialize (mStream, levelData, typeof(LevelData));
【问题讨论】:
-
切换到类似 protobuf-csharp-port 的东西会帮助这种情况吗? code.google.com/p/protobuf-csharp-port
-
您是否使用运行时处理?还是您使用的是protogen工具?基本上:我怎么能重现它?
-
我正在使用运行时序列化和反序列化数据。我也在为 Wii U 开发,这仅在我在硬件上运行时才会发生,因此这可能与 Unity 在硬件上提供的 API 子集有关。
-
我在移动设备上,但我建议在此处使用预编译器 - 搜索“引入 protobuf-net 预编译器 protogen”或类似内容
-
不走运。我使用了预编译器并切换了我的代码,但我仍然遇到同样的异常。有什么办法可以强制预编译器包含 KeyValuePair 的序列化程序?
标签: c# unity3d protobuf-net