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) | HD レンダーパイプライン (HDRP) | カスタム SRP |
---|---|---|---|---|
ShaderLab: 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:
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.
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:
1.2.3
includes all versions starting with 1.2.3
;[1.2.3]
only includes version 1.2.3
;[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:
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]"
}
}
}
}
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.
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
"com.some.package.z" : "[10.2.1,11.0]" // Error, extra whitespace after the package name
"" : "[2.3.4,3.4.5]" // Error, no package name provided
}
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
}
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
}
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
}
}
}