问题
I am trying to build a custom protobuf-net RuntimeTypeModel in Unity3D with this function:
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;
}
The model built but when it try to use it I keep getting assembly errors in Unity Editor. - Could not load file or assembly UnityEngine.UI, UnityEngine.Purchasing, Assembly-CSharp etc. No errors when using RuntimeTypeModel.Default.
The strange thing is errors show only after second time I enter play mode after Unity starts. First play is without errors, when I exit the play mode still no errors in the console. Only when I enter the play mode second time the errors show and stay till I restart the Unity.
I have no idea how to improve the code to get a proper RuntimeTypeModel that wouldn't cause the errors. The code for building RuntimeTypeModel is from this answer to my other question.
EDIT: I am not using the following lines but it doesn't matter for the issue:
//add unity types
typeModel.Add(typeof(Vector2), false).Add("x", "y");
typeModel.Add(typeof(Vector3), false).Add("x", "y", "z");
回答1:
New RuntimeTypeModel works without a problem on the android device. So at the moment I decided to change model from MyProtoModel to RuntimeTypeModel.Default when in editor with a simple #if #else statement.
I don't get errors in the editor and I can still use my new model on android. I am not happy with makeshift solutions and still hope for a better answer.
来源:https://stackoverflow.com/questions/57760099/custom-protobuf-net-runtimetypemodel-in-unity3d-could-not-load-file-or-assembl