【发布时间】:2019-09-02 16:17:30
【问题描述】:
我正在尝试使用此功能在 Unity3D 中构建自定义 protobuf-net RuntimeTypeModel:
private static RuntimeTypeModel GetModel()
{
RuntimeTypeModel typeModel = TypeModel.Create();
foreach (var t in GetTypes(CompilationPipeline.GetAssemblies()))
{
var contract = t.GetCustomAttributes(typeof(ProtoContractAttribute), false);
if (contract.Length > 0)
{
typeModel.Add(t, true);
//add unity types
typeModel.Add(typeof(Vector2), false).Add("x", "y");
typeModel.Add(typeof(Vector3), false).Add("x", "y", "z");
}
}
return typeModel;
}
模型已构建,但当它尝试使用它时,我在 Unity 编辑器中不断出现装配错误。 - 无法加载文件或程序集 UnityEngine.UI、UnityEngine.Purchasing、Assembly-CSharp 等。使用 RuntimeTypeModel.Default 时没有错误。
奇怪的是,只有在 Unity 启动后我第二次进入播放模式后才会显示错误。第一次播放没有错误,当我退出播放模式时,控制台中仍然没有错误。只有当我第二次进入播放模式时,错误才会显示并一直持续到我重新启动 Unity。
我不知道如何改进代码以获得不会导致错误的正确 RuntimeTypeModel。构建 RuntimeTypeModel 的代码来自 this 对我的另一个问题的回答。
编辑: 我没有使用以下几行,但这对问题无关紧要:
//add unity types
typeModel.Add(typeof(Vector2), false).Add("x", "y");
typeModel.Add(typeof(Vector3), false).Add("x", "y", "z");
【问题讨论】:
-
哪一行抛出异常?也许是
GetTypes(CompilationPipeline.GetAssemblies())的迭代?即,如果您删除foreach中的所有内容(可能是“write t.Name”除外) - 它仍然会失败吗? -
运行此代码时不会抛出异常。仅在使用使用此功能构建的实际新模型时才会出错。只有当我在 Unity3D 启动后第二次在编辑器中点击播放模式时。我忘了提到我将模型保存为 .dll 文件,稍后在代码中引用它。
-
观察:您似乎多次添加向量类型;我会期望在这里打破;这里的悲剧:我了解 protobuf-net 位,但我对统一性的了解几乎为零.... :( 这 看起来 主要与统一性相关?跨度>
-
我正在使用这个code。
-
是的,我已经用矢量删除了这些行。我不需要它们,因为我有自己的序列化就绪向量类,可以与 protobuf-net 一起使用。 Unity Vector 类很难序列化。
标签: c# .net unity3d protobuf-net