While this is a valid and commonly used type of code, the linker doesn’t know whether MyAssembly, MyType, and MyMethod are actually used at runtime. This can cause them to be stripped down, and by extension, result in a runtime error. Check out the stripping restrictions manual for more information.

Developers that use Dependency Injection frameworks like Zenject, or serialization libraries like Newtonsoft.Json, have to be aware that false-positive code stripping is a possibility, and should address it accordingly. Here are some of the most common approaches:

  • The linker recognizes a number of attributes and allows you to annotate dependencies when it cannot identify them. As such, you can add the [Preserve] attribute to assemblies, classes, and methods that should not be stripped down.
  • A link.xml file is a per-project list that describes how to preserve assemblies, types, and other code entities within them. You can manually add needed assemblies, classes, and methods to link.xml, or use the UnityEditor API GenerateAdditionalLinkXmlFile to generate the link.xml file during the build process.

Even the Addressables package harnesses the LinkXmlGenerator. The Addressables’ build script reviews the list of assets in the groups and adds types used by assets into the link.xml file. It also adds in types used by Addressables internally via Reflection at runtime. Consider reviewing the default build script, BuildScriptPackedMode.cs, for more details on implementing a similar solution as a step in your build process, like with the Scriptable Build Pipeline.

Unity supports multiple link.xml files located inside of the Assets folder or one of its subfolders. During the build process, entries of multiple link.xml files are merged and considered by the linker.

Source: Unity Technologies Blog