At runtime, you can enable or disable shaderA program that runs on the GPU. More info
See in Glossary keywords. When you enable or disable a shader keyword, Unity uses the appropriate shader variant for rendering.
Changing shader variants at runtime can impact performance. If a change in keywords requires a variant to be used for the first time, it can lead to hitches while the graphics driver prepares the shader program. This can be a particular problem for large or complex shaders, or if a global keyword state change affects multiple shaders. To avoid this, ensure that you consider keyword variants in your shader loading and prewarming strategy. For more information, see Shader loading.
In Unity, there are local shader keywords and global shader keywords:
You set the scope of a shader keyword when you declare it. For more information, see Shader keywords: global and local scope.
Note: If there are global and local keywords with the same name, Unity prioritises the local keyword.
Warning: If a material has a local keyword enabled, and its shader changes to one that is no longer declared, Unity creates a new global keyword.
To enable or disable a global shader keyword, use Shader.EnableKeyword or Shader.DisableKeyword.
To enable or disable a global keyword with a Command Buffer, use CommandBuffer.EnableKeyword, or CommandBuffer.DisableKeyword.
To check the state, use [Shader.IsKeywordEnabled].
To enable or disable a local shader keyword for a graphics shader, use Material.EnableKeyword or Material.DisableKeyword. To check the state, use Material.IsKeywordEnabled.
To enable or disable a local shader keyword for a compute shader, use ComputeShader.EnableKeyword or ComputeShader.DisableKeyword. To check the state, use [ComputeShader.IsKeywordEnabled].
When you author your shader, you declare keywords in sets. A set contains mutually-exclusive keywords.
At runtime, Unity has no concept of these sets. It allows you to enable or disable any keyword independently, and enabling or disabling a keyword has no effect on the state of any other keyword. This means that it is possible to enable multiple keywords from the same set, or disable all the keywords in a set.
When more than one keyword in a set is enabled, or no keywords in a set are enabled, Unity chooses a variant that it considers a “good enough” match. There is no guarantee about what exactly happens, and it can lead to unintended results. It is best to avoid this situation by managing keyword state carefully.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.