Hello Unity users!
TL;DR:
In Unity 2021.2.0a17 makes it possible to specify package requirements for SubShaders and Passes directly in ShaderLab. A Pass or a SubShader that does not meet the requirements is excluded from further processing and compilation.
(EDIT 14.05.2021): This functionality is mainly aimed at asset store and tool developers that need to target multiple SRPs.
(EDIT 28.07.2021): 2021.1.17f1 and 2020.3.16f1 and later will also support the PackageRequirements block.
Example code:
Shader "MyShader" {
SubShader {
Pass {
PackageRequirements {
"com.unity.render-pipelines.universal":"[10.0,10.5.3]"
}
...
}
Pass {
PackageRequirements {
"com.unity.render-pipelines.universal":"[11.0,11.2.4]"
"com.my.other.package"
}
...
}
}
}
The first pass is compatible with URP 10.0 to 10.5.3 inclusive, the second pass is compatible with URP 11.0 to 11.2.4 and requires “com.my.other.package” to be installed.
There are multiple ways to declare package requirements:
- “”: specifies that the SubShader or Pass works with any version of the package.
- “”: “”: specifies that the SubShader or Pass only works with a subset of package versions.
- “”: “unity=”: specifies that the SubShader or Pass only works with a subset of Unity versions and requires a package with the given name.
- ”unity”:””: 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.
Specifying version restrictions
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:
- : includes the version you enter and all versions after that. For example, 1.2.3 includes all versions starting with 1.2.3;
- []: specifies the exact version. For example, [1.2.3] only includes version 1.2.3;
- [,]: specifies a range between and . Using square brackets and round brackets causes the range to include or exclude to version respectively. The opening bracket affects and the closing bracket affects . 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.
If you define package requirements that can never be satisfied, the shader import process fails with an error.
Stay tuned for more updates!