Robin Fischer

Game programmer that wants to share and document.

Additional link.xml definitions

Some undocumented features of the link.xml file used by Unity3d

The link.xml file can be used to specify code that should not be stripped by Unity in certain build steps. IL2CPP is a Unity technology that converts the managed C# code to C++ code. During the conversion process IL2CPP strips (removes) all code where the compiler is ‘sure’ that it will not be used. Examples are methods that are never called or platform dependent code that is never executed on certain platforms. But what the compiler cannot forsee is Reflection based code. For this reason we can tell Unity to not strip certain parts of our code regardless what the compiler says. Unity has some documentation on this HERE.

I was working with this link.xml file and noted that there are some undocumented definitions that are handy. The biggest is Wildcard support in a changing codebase. Check the commented definitions beneath.

<linker>
    <assembly fullname="MyAssemblyName"> // the name of the assembly
        <type fullname="MyNamespace" preserve="all"/> // excludes all classes that are direct children of MyNamespace
        <type fullname="MyNamespace.*" preserve="all"/> // excludes all namespaces and classes recursively under MyNamespace
        <type fullname="MyNamespace*" preserve="all"/> // excludes all namespaces and classes recursively under MyNamespace. Would also exclude the namespace MyNamespaceABC
        <type fullname="My*" preserve="all"/> // excludes all namespaces and classes that start with My and all children of those namespaces
        <type fullname="*" preserve="all"/> // excludes all namespaces and classes in the assembly
        <type fullname="MyNamespace.ObservedCollection`1" preserve="all"/> // Excludes generic ObservedCollection<T>
        <type fullname="MyNamespace.ObservedCollection`2" preserve="all"/> // Excludes generic ObservedCollection<T,K>
    </assembly>
    <assembly fullname="MySecondAssemblyName">
        <namespace fullname="My.Second.Namespace" preserve="all"/>
    </assembly>
</linker>