Version: 2021.3
ShaderLab:添加着色器程序
ShaderLab:命令

ShaderLab: specifying package requirements

Some shaders need to support multiple render pipelines simultaneously. Adding package requirements to SubShaders and Passes enables you to avoid compilation errors when shader code uses an include file from a package that is not installed or requires a specific version of a package to compile.

渲染管线兼容性

功能名称 内置渲染管线 通用渲染管线 (URP) 高清渲染管线 (HDRP) 自定义 SRP
ShaderLab: PackageRequirements block

Using the PackageRequirements block

To specify package requirements for a SubShader or a Pass, you use the PackageRequirements block. ShaderLab supports a single PackageRequirements block per SubShader or Pass, but each block can specify multiple package requirements.

Note: if you provide a PackageRequirements block, it must come before all other declarations inside the SubShader or Pass.

签名 功能
PackageRequirements{ [requirement definition]} Defines the package requirements for the Pass or SubShader.

There are multiple ways to declare package requirements. Each one provides a different behavior. They are:

  • “<package name>”: Specifies that the SubShader or Pass works with any version of the package.
  • “<package name>”: “<version restrictions>”: Specifies that the SubShader or Pass only works with a subset of package versions.
  • “<package name>”: “unity=<version restrictions>”: Specifies that the SubShader or Pass only works with a subset of Unity versions and requires a package with the given name.
  • “unity”:“<version restrictions>”: Specifies that the SubShader or Pass only works with a subset of Unity versions.

Version restrictions define a set of version ranges. If the installed version of a required package is not inside any of the ranges, the package requirement is not met. Similarly, if a requirement specifies a set of Unity version restrictions, the same applies to the current version of Unity. For information about the syntax of version restrictions, see Version syntax.

If a SubShader or Pass declares package requirements that the project does not meet, Unity excludes the SubShader or Pass from further processing and compilation. This happens if the project does not include the required packages, or includes them but not with a compatible version. If a shader does not contain a single SubShader that meets the requirements, or if no SubShader contains Passes that meet the requirements, the Console window shows a warning message.

Version syntax

In ShaderLab’s package requirements, a version uses the major.minor or major.minor.patch format. If you only use major.minor, Unity uses 0 for the patch. Package versions can also include a -preview or a -preview.n postfix where -preview is equivalent to -preview.0. Preview versions come before non-preview versions, so 1.2.3-preview.4 comes after 1.2.2 but before 1.2.3.

There are multiple ways to specify a version range. Each one provides a different behavior. They are:

  • <version>: Includes the version you enter and all versions after that. For example, 1.2.3 includes all versions starting with 1.2.3;
  • [<version>]: Specifies the exact version. For example, [1.2.3] only includes version 1.2.3;
  • [<version1>,<version2>]: Specifies a range between <version1> and <version2>. Using square brackets and round brackets causes the range to include or exclude to version respectively. The opening bracket affects <version1> and the closing bracket affects <version2>. Example: [1.2.3,2.3.4) includes all versions from 1.2.3 to 2.3.3.

You can also specify sets of version ranges for a single package. To create a set of version ranges from individual ranges, use a semicolon as a separator. For example, [2.0,3.4.5];[3.7];4.0 includes versions from 2.0.0 to 3.4.5, version 3.7.0, and version 4.0.0 and above.

When you set the versions for a package, be aware of the following:

  • Versions, version ranges, and sets of version ranges cannot contain any extra characters.
  • Version ranges cannot be empty.
  • Sets of version ranges cannot have intersections.

If the syntax does not adhere to the above, the version restriction is invalid. For more information on what happens with invalid package requirements, see Error checking.

示例

The following code example shows how to specify package requirements in both a SubShader and a Pass. The SubShader declares a single package requirement for a package called “com.my.package” and works with any version of this package starting from 2.2.0. The SubShader has two Passes:

  • The first Pass requires:

  • The Universal Render Pipeline package with a version between 10.2.1 and 11.0.

  • The Text Mesh Pro package with a version of 3.2 or above.

  • The second Pass requires:

  • The High-Definition Render Pipeline package with a version between 8.0 to 8.5

Shader "Examples/ExampleShader"
{
    SubShader
    {
        PackageRequirements
        {
            "com.my.package": "2.2"
        }
        Pass
        {
            PackageRequirements
            {
                "com.unity.render-pipelines.universal": "[10.2.1,11.0]"
                "com.unity.textmeshpro": "3.2"
            }
        }
        Pass
        {
            PackageRequirements
            {
                "com.unity.render-pipelines.high-definition": "[8.0,8.5]"
            }
        }
    }
}

Error checking

If you define package requirements that can never be satisfied, the shader import process fails with an error. This section provides examples of the most common invalid package requirement definitions.

Malformed versions or empty package name

PackageRequirements {
  "com.some.package.x": "[10.2.1,9.0]"      // Error, empty version range
  "com.some.package.y": "[10.2.1.9,11.0]"       // Error, incorrect version format
  "com.some.package.z": "[2.3,3.5],[3.0,4.0]"               // Error, ranges intersect
  "" : "[2.3.4,3.4.5]"                  // Error, no package name provided
}

Multiple dependencies on the same package

PackageRequirements {
  "com.some.package.x": "[10.2.1,11.0]"
  "com.some.package.x": "[11.2.1,12.0]" // Error, dependency on "com.some.package.x" declared twice
  "unity" : "2021.2"
  "unity" : "2021.3"    // Error, dependency on Unity version declared twice
}

Conflicting dependency declarations

PackageRequirements {
  "com.some.package.x": "unity=[2021.2.1,2021.3.3]"
  "unity" : "2021.2"    // Error: Unity version requirement cannot be declared on a package and on its own simultaneously
}

Conflicting dependency declarations between SubShaders and Passes

SubShader {
  PackageRequirements {
    "com.some.package.x": "[2.3.4,3.4.5]"
    "com.some.package.z": "[1.1,3.2]"
    "unity": "2021.2"
  }
  Pass {
    PackageRequirements {
      "com.some.package.y": "[1.2.2,2.5]"   // Fine, SubShader doesn’t declare a dependency on "com.some.package.y"
      "com.some.package.z": "[2.0,3.1]" // Fine, SubShader dependency intersects the provided version range
      "com.some.package.x": "[1.1.1, 2.2.2]"    // Error, SubShader version range for "com.some.package.x" does not intersect the provided range
      "com.some.package.w": "unity=[2021.2.1,2021.2.5]" // Fine, SubShader dependency intersects the provided version range
      "com.some.package.u": "unity=[2020.2.1,2020.2.5]" // Error, SubShader Unity version range does not intersect the provided range
    }
  }
}
ShaderLab:添加着色器程序
ShaderLab:命令
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961