Unity3d's Il2cpp platform could not find the default constructor of the pit

Source: Internet
Author: User

The initial discovery of the problem was that when the game was released on the iOS version with the Il2cpp platform, some DLL-formatted plugins could cause the game to crash, such as Fullinspector and behavior Designer. The exception thrown is that the default constructor for some classes cannot be found.

It was later discovered that not only some plug-ins reported this exception, but many of the JSON-formatted serialization features would also throw exceptions to the default constructor on the Il2cpp platform.

The cause of this problem is that the il2cpp version is caused by some optimization mechanisms at AOT compilation time. To learn more about this mechanism, you can read the documentation: http://docs.unity3d.com/Manual/iphone-playerSizeOptimization.html

In fact, the problem is the difference between the static invocation of the function and the dynamic invocation using reflection. When compiling in the AOT phase, the compiler discards a class or function when it is completely unused. This is an optimization mechanism, but the problem is that this check only checks for static references, which are not found by the call compiler that takes advantage of the reflection mechanism. This leads to a function that does not have a static reference, is discarded by the compiler, and then, in an attempt to invoke it through the reflection mechanism, causes the function to be called without being found.

For example, some serialization plugins, such as Fullserializer, use the Activator generic (dynamic reflection) instead of new (static reference) when generating the object. So the constructor of the class is discarded at compile time, and then the problem arises when Activator attempts to dynamically access its dynamic function.

To solve this problem, a more straightforward approach is to add a static reference to the missing constructor in the code, that is, simply find a new one. This prevents il2cpp from being discarded when optimized. The problem with this approach is that it will leave a useless object in memory.

A better approach is to use Unity3d's Link.xml function. The function of this link.xml is to tell the compiler at the time of the AOT compilation that the classes specified in the Link.xml are preserved, whether or not they are referenced. Although it is possible to point to a class precisely, it is easier to include all the namespaces of the entire problematic plugin.

For example, when it comes to fullinspector and behavior designer, Link.xml probably looks like this:

<linker>

<assembly fullname= "Fullinspector-core" >

<namespace fullname= "Fullinspector" preserve= "All"/>

</assembly>

<assembly fullname= "Behaviordesignerruntime" >

< namespace fullname= "Behaviordesigner.runtime" preserve= "All"/>

</assembly>

</linker>

In this way, the classes and functions in both plugins will be completely preserved, and the problem solved.

Unity3d's Il2cpp platform could not find the default constructor of the pit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.