Version: 6000.3
语言: 中文
使用注释保留代码
Unity 链接器标记规则参考

链接 XML 格式参考

此参考提供了有关正确格式化的信息link.xml文件,包括有效的 XML 元素和属性以及用法示例。

支持的 XML 元素

link.xml文件支持以下 XML 元素:

元素 描述
<linker></linker> 必须用于所有link.xml文件作为包含所有其他元素的最外层元素。
<assembly></assembly> 声明特定于程序集的代码保留批注。
<type></type> 声明特定于类型的代码保留批注。
<field></field> 声明特定于字段的代码保留批注。
<method></method> 声明特定于方法的代码保留批注。
<property></property> 声明特定于属性的代码保留批注。
<event></event> 声明特定于事件的代码保留批注。

所有这些元素的用法示例,请参阅用法示例

支持的 XML 属性

属性 描述
accessors 应用于<property>元素来指定要保留的属性的访问器方法。
feature 根据当前版本的设置排除不支持的功能的保留。有关更多信息和用法示例,请参阅功能排除项。
fullname 按名称标识代码元素,包括其父程序集名称作为前缀。用fullname每当name单独可能是模棱两可的。
ignoreIfMissing 申请<assembly>元素来声明在所有 Player 生成期间不存在的程序集的保留。示例用法,请参阅忽略丢失的装配
ignoreIfUnreferenced 申请<assembly>元素仅当至少一个成员被另一个装配引用时,才保留装配中的图元。示例用法,请参阅忽略未引用的装配
name 仅按名称标识代码元素,而不将父程序集作为前缀。如果装配名称已指定fullname在父 XML 元素中,子 XML 元素可以通过以下方式标识代码元素name独自。
preserve 指定要保留的代码元素级别,以防止剥离。
signature 通过签名标识代码元素。方法签名由返回类型、名称和参数组成。字段、属性或事件签名由类型和名称组成。示例用法,请参阅使用示例
windowsruntime 必须添加到<assembly>元素中的link.xml每当为 Windows 运行时元数据 (.winmd) 程序集定义保留时,都会找到文件。示例用法,请参阅 Windows 运行时元数据程序集

有关所有这些属性的示例用法,请参阅使用示例

使用示例

以下示例演示了受支持的 XML 元素和属性的有效用法。

声明程序集根类型的不同方法

以下示例说明了使用link.xml文件:

<linker>
  <!--Preserve types and members in an assembly-->
  <assembly fullname="AssemblyName">
    <!--Preserve an entire type-->
    <type fullname="AssemblyName.TypeName" preserve="all"/>

    <!--No "preserve" attribute and no members specified means preserve all members-->
    <type fullname="AssemblyName.TypeName"/>

    <!--Preserve all fields on a type-->
    <type fullname="AssemblyName.TypeName" preserve="fields"/>

    <!--Preserve all methods on a type-->
    <type fullname="AssemblyName.TypeName" preserve="methods"/>

    <!--Preserve the type only-->
    <type fullname="AssemblyName.TypeName" preserve="nothing"/>

    <!--Preserving only specific members of a type-->
    <type fullname="AssemblyName.TypeName">
        
      <!--Fields-->
      <field signature="System.Int32 FieldName" />

      <!--Preserve a field by name rather than signature-->
      <field name="FieldName" />
      
      <!--Methods-->
      <method signature="System.Void MethodName()" />

      <!--Preserve a method with parameters-->
      <method signature="System.Void MethodName(System.Int32,System.String)" />

      <!--Preserve a method by name rather than signature-->
      <method name="MethodName" />

      <!--Properties-->

      <!--Preserve a property, its backing field (if present), 
          getter, and setter methods-->
      <property signature="System.Int32 PropertyName" />

      <property signature="System.Int32 PropertyName" accessors="all" />

      <!--Preserve a property, its backing field (if present), and getter method-->
      <property signature="System.Int32 PropertyName" accessors="get" />

      <!--Preserve a property, its backing field (if present), and setter method-->
      <property signature="System.Int32 PropertyName" accessors="set" />

      <!--Preserve a property by name rather than signature-->
      <property name="PropertyName" />

      <!--Events-->

      <!--Preserve an event, its backing field (if present), add, and remove methods-->
      <event signature="System.EventHandler EventName" />

      <!--Preserve an event by name rather than signature-->
      <event name="EventName" />

    </type>
  </assembly>
</linker>

将整个程序集声明为根

以下示例演示如何声明整个程序集:

<!--Preserve an entire assembly-->
  <assembly fullname="AssemblyName" preserve="all"/>

  <!--No "preserve" attribute and no types specified means preserve all-->
  <assembly fullname="AssemblyName"/>

  <!--Fully qualified assembly name-->
  <assembly fullname="AssemblyName, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
    <type fullname="AssemblyName.Foo" preserve="all"/>
  </assembly>

  <!--Force an assembly to be processed for roots but don't explicitly preserve anything in particular. Useful when the assembly isn't referenced.-->
  <assembly fullname="AssemblyName" preserve="nothing"/>

保留嵌套类型或泛型类型

以下示例演示如何保留嵌套类型或泛型类型:

<!--Examples with generics-->
    <type fullname="AssemblyName.G`1">

      <!--Preserve a field with generics in the signature-->
      <field signature="System.Collections.Generic.List`1<System.Int32> FieldName" />

      <field signature="System.Collections.Generic.List`1<T> FieldName" />

      <!--Preserve a method with generics in the signature-->
      <method signature="System.Void MethodName(System.Collections.Generic.List`1<System.Int32>)" />

      <!--Preserve an event with generics in the signature-->
      <event signature="System.EventHandler`1<System.EventArgs> EventName" />

    </type>

    <!--Preserve a nested type-->
    <type fullname="AssemblyName.H/Nested" preserve="all"/>

    <!--Preserve all fields of a type if the type is used.  If the type isn't used, it will be removed-->
    <type fullname="AssemblyName.I" preserve="fields" required="0"/>

    <!--Preserve all methods of a type if the type is used. If the type isn't used, it will be removed-->
    <type fullname="AssemblyName.J" preserve="methods" required="0"/>

    <!--Preserve all types in a namespace-->
    <type fullname="AssemblyName.SomeNamespace*" preserve="all"/>

    <!--Preserve all types with a common prefix in their name-->
    <type fullname="Prefix*" preserve="all"/>

忽略缺少的装配

使用ignoreIfMissing属性<assembly>元素,如果需要声明在所有 Player 构建期间不存在的程序集的保留:

<linker>
  <assembly fullname="Foo" ignoreIfMissing="1">
    <type name="TypeName"/>
  </assembly>
</linker>

忽略未引用的装配

使用ignoreIfUnreferenced属性<assembly>元素,仅当在另一个装配中至少引用一种类型时,才保留装配中的图元:

<linker>
  <assembly fullname="Bar" ignoreIfUnreferenced="1">
    <type name="TypeName"/>
  </assembly>
</linker>

Windows 运行时元数据程序集

您必须使用windowsruntime属性<assembly>每当定义 Windows 运行时元数据 (.winmd) 程序集的保留时,元素:

<linker>
  <assembly fullname="Windows" windowsruntime="true">
    <type name="TypeName"/>
 </assembly>
</linker>

功能排除项

mscorlib.xml文件嵌入在mscorlib.dll使用此属性,但您可以在任何link.xml在适当的时候提交。

“高离”级别,Unity 链接器根据当前版本的设置排除不支持的功能的保留:

  1. remoting— 在定位IL2CPPUnity 开发的脚本后端,在为某些平台构建项目时,可以将其用作 Mono 的替代品。更多信息
    请参阅术语表
    脚本后端为Unity中的脚本提供支持的框架。Unity 支持三种不同的脚本后端,具体取决于目标平台:Mono、.NET 和 IL2CPP。但是,通用 Windows 平台仅支持两个:.NET 和 IL2CPP。更多信息
    请参阅术语表
    .
  2. sre— 针对 IL2CPP 脚本后端时排除。
  3. com— 针对不支持 COM 的平台时排除。

例如,以下内容link.xmlfile 在支持 COM 的平台上保留一种类型的一种方法,并在所有平台上保留一种方法:

<linker>

    <assembly fullname="Foo">

        <type fullname="Type1">

            <!--Preserve FeatureOne on platforms that support COM-->

            <method signature="System.Void FeatureOne()" feature="com"/>

            <!--Preserve FeatureTwo on all platforms-->

            <method signature="System.Void FeatureTwo()"/>

        </type>

    </assembly>

</linker>

其他资源

使用注释保留代码
Unity 链接器标记规则参考