最近在項目中使用Json讀取存儲數據,本來想用Unity自帶的JsonUtility的,但是無奈Unity自帶的Json解析功能太少,比如不支持Dictionary等等。後來使用了另外一個JsonNet插件,比Unity好用很多很多,也很方便,性能也不錯。
Newtonsoft.JsonNet大家可以自己到Github下載,提供了dll和源代碼。GitHub地址:https://github.com/SaladLab/Json.Net.Unity3D/releases
現在要說的是碰到的一個坑,在ios打包到手機的時候運行報錯,報錯如下:
MissingMethodException: Method not found: 'Default constructor not found...ctor() of System.ComponentModel.TypeConverter'.
at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00000] in <filename unknown>:0
at System.ComponentModel.TypeDescriptor.GetConverter (System.Type type) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.DefaultContractResolver.CanConvertToString (System.Type type) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract (System.Type objectType) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract (System.Type type) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonSerializer.SerializeInternal (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonConvert.SerializeObjectInternal (System.Object value, System.Type type, Newtonsoft.Json.JsonSerializer jsonSerializer) [0x00000] in <filename unknown>:0
最開始猜測是因爲il2CPP的時候用的是dll而非源代碼,後面百度下了,有位仁兄也碰到了,猜測是因爲Unity代碼優化把沒用到的類刪除了,所以導致出錯了...
在JsonNet官方的下載包裏面找到了README,查看了,找到了解決方案。
## FAQ
- Q: App stops throwing MissingMethodException for ComponentModel.TypeConverter like this.
```
MissingMethodException: Method not found:
'Default constructor not found...ctor() of System.ComponentModel.TypeConverter'.
```
- A: link.xml should be added to your project.
也就是把下載包中的link.xml拉到項目中任何一個地方即可。