Bootstrap

Unity 问题之 打包真机运行报错 MissingMethodException: Default constructor not found for type xxxxxx 问题处理

Unity 问题之 打包真机运行报错 MissingMethodException: Default constructor not found for type xxxxxx 问题处理

目录

Unity 问题之 打包真机运行报错 MissingMethodException: Default constructor not found for type xxxxxx 问题处理

一、简单介绍

二、问题现象

三、解决方式


一、简单介绍

Unity 在开发中,记录一些报错问题,以便后期遇到同样问题处理。

二、问题现象

1、可能大家会遇到类似 System.MissingMethodException: Default constructor not found for type XXFramework.AssetModule.AssetModule 报错

2、或者类似如下的报错

2020/10/30 14:09:09.050 20499 20540 Error Unity MissingMethodException: Default constructor not found for type UnityEngine.ResourceManagement.AsyncOperations.ProviderOperation`1[[UnityEngine.AddressableAssets.Initialization.ResourceManagerRuntimeData, Unity.Addressables, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]
2020/10/30 14:09:09.050 20499 20540 Error Unity   at System.RuntimeType.CreateInstanceMono (System.Boolean nonPublic) [0x00000] in <00000000000000000000000000000000>:0 
2020/10/30 14:09:09.050 20499 20540 Error Unity   at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic) [0x00000] in <00000000000000000000000000000000>:0 
2020/10/30 14:09:09.050 20499 20540 Error Unity   at UnityEngine.ResourceManagement.Util.LRUCacheAllocationStrategy.New (System.Type type, System.Int32 typeHash) [0x00000] in <00000000000000000000000000000000>:0 
2020/10/30 14:09:09.050 20499 20540 Error Unity   at UnityEngine.ResourceManagement.ResourceManager.CreateOperation[T] (System.Type actualType, System.Int32 typeHash, System.Int32 operationHash, System.Action`1[T] onDestroyAction) [0x00000] in <00000000000000000000000000000000>:0 
2020/10/30 14:09:09.050 20499 20540 Error Unity   at UnityEngine.ResourceManagement.ResourceManager.ProvideResource (UnityEngine.ResourceM
2020/10/30 14:09:09.057 20499 20540 Error Unity MissingMethodException: Default constructor not found for type UnityEngine.ResourceManagement.ResourceManager+CompletedOperation`1[[System.Collections.Generic.IList`1[[ThemeData, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
2020/10/30 14:09:09.057 20499 20540 Error Unity   at System.RuntimeType.CreateInstanceMono (System.Boolean nonPublic) [0x00000] in <00000000000000000000000000000000>:0 
2020/10/30 14:09:09.057 20499 20540 Error Unity   at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic) [0x00000] in <00000000000000000000000000000000>:0 
2020/10/30 14:09:09.057 20499 20540 Error Unity   at UnityEngine.ResourceManagement.Util.LRUCacheAllocationStrategy.New (System.Type type, System.Int32 typeHash) [0x00000] in <00000000000000000000000000000000>:0 
2020/10/30 14:09:09.057 20499 20540 Error Unity   at UnityEngine.ResourceManagement.ResourceManager.CreateOperation[T] (System.Type actualType, System.Int32 typeHash, System.Int32 operationHash, System.Action`1[T] onDestroyAction) [0x00000] in <00000000000000000000000000000000>:0 
2020/10/30 14:09:09.057 20499 20540 Error Unity   at UnityEngine.ResourceManagement.Res

三、解决方式

1、快速解决方式可以根据对应的添加,可以在 Assets 文件夹中创建 link.xml 文件

第一个是

<linker>
  <assembly fullname="XXFramework">
    <type fullname="XXFramework.AssetModule.AssetModule" preserve="all" />
    <!-- 添加其他需要保留的类型或方法 -->
  </assembly>
</linker>

第二个是

<linker>
    <assembly fullname="Unity.ResourceManager" preserve="all" />
    <assembly fullname="Unity.Addressables" preserve="all" />
</linker>

2、不过如果添加了如上的代码,还是不行的话,可能还需要给对应的类添加一个默认的无参构造函数

例如:

namespace XXFramework.AssetModule{

    public class AssetModule{
        private string m_Test;
        public AssetModule(){
            m_Test = "";
        }
    }

}

3、如果有太多的 MissingMethodException: Default constructor not found for type 报错,可以尝试如下的方式

<linker>
  <assembly fullname="XXFramework, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" preserve="all">
    <!--assembly fullname="XXFramework"-->
    <type fullname="XXFramework.AssetModule.AssetModule" preserve="all" />
    <!-- 添加其他需要保留的类型或方法 -->
  </assembly>
</linker>

更多 Unity link 相关信息(代码剥离的信息)可参见:Unity - Manual: Managed code stripping

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;