我遇到了一个问题: 【问题描述】:
unity使用il2cpp打包apk在安卓测试机上测试。
etmodel层,在OnApplicationQuit时调用bson obj.ToJson(new JsonWriterSettings() { OutputMode = JsonOutputMode.Strict });报错
ExecutionEngineException: Attempting to call method ‘MongoDB.Bson.Serialization.Serializers.DictionaryInterfaceImplementerSerializer3[[System.Collections.Generic.Dictionary
2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]::.ctor’ for which no ahead of time (AOT) code was generated.
以下是我的代码
private async ETVoid StartAsync()
{
try
{
SynchronizationContext.SetSynchronizationContext(OneThreadSynchronizationContext.Instance);
DontDestroyOnLoad(gameObject);
Game.EventSystem.Add(DLLType.Model, typeof(Init).Assembly);
// 启动页显示游戏开始
UI uiLoading = UILoadingFactory.Create();
Game.Scene.UILoading = uiLoading;
// 省略了一些代码
// 调用一下,避免被裁剪
var txt = JsonHelper.ToJsonWithBson(ArchiveComponent.Instance.archive);
//Game.Hotfix.GotoHotfix();
}
catch (Exception e)
{
Log.Error(e);
}
}
public static string ToJsonWithBson(object obj)
{
return obj.ToJson(new JsonWriterSettings() { OutputMode = JsonOutputMode.Strict });
}
public void OnApplicationPause()
{
if (archive != null)
{
var archiveTxt = JsonHelper.ToJsonWithBson(archive);
ETModel.CommonHelper.Save(archiveTxt);
}
Log.Info("游戏暂停!!");
}
public void OnApplicationQuit()
{
if (archive != null)
{
var archiveTxt = JsonHelper.ToJsonWithBson(archive);
ETModel.CommonHelper.Save(archiveTxt);
}
Log.Info("游戏退出了!!");
}
我想要实现【X功能】:
在游戏退出和暂停时,调用bson,将对象转化成json字符串存档
但出现【Y现象】:
如上报错
我经过了以下尝试【思路细节与代码实现】:
1、在etmodel的init.cs文件中,手动调用JsonHelper.ToJsonWithBson(ArchiveComponent.Instance.archive)以期望不被裁剪
2、修改link.xml
<linker>
<assembly fullname=“Unity.Model” preserve=“all”/>
<assembly fullname=“Unity.ThirdParty” preserve=“all”/>
<assembly fullname=“UnityEngine” preserve=“all”/>
<assembly fullname=“System” preserve=“all”/>
</linker>
但不能解决,报错如下【报错信息/截图】:
我使用【关键词】:
谷歌/百度/必应但找不到答案。请问我该怎么解决?【截图相关代码片段】: