Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


ShaderKeywordSet

A collection of ShaderKeyword that represents a specific shader variant.
Read time 2 minutesLast updated 4 days ago

Definition

public struct ShaderKeywordSet

Remarks

A
ShaderKeywordSet
represents a single shader variant, a unique combination of keywords which are used to enable or disable functionality within a shader. Note that this struct will not contain information on all keywords available to a shader -- for example, some platform-related keywords are stored in PlatformKeywordSet.
ShaderKeywordSet
is designed to be accessed at build time via IPreprocessShaders or IPreprocessComputeShaders. Do not instantiate nor use this struct in isolation. Use these callbacks in scriptable stripping to reduce build times by culling unused variants before they are compiled. For example, if you know that two distinct keywords will never need to be enabled at the same time, you may strip any variants with both keywords enabled.
For more information on shader keywords, see Changing how shaders work via branching and keywords.

Examples

using System.Collections.Generic;using UnityEditor;using UnityEngine;using UnityEngine.Rendering;using UnityEditor.Build;using UnityEditor.Rendering;class ShaderDebugBuildPreprocessor : IPreprocessShaders{ ShaderKeyword m_KeywordToStrip; public ShaderDebugBuildPreprocessor() { m_KeywordToStrip = new ShaderKeyword("DEBUG"); } // Use callbackOrder to set when Unity calls this shader preprocessor. Unity starts with the preprocessor that has the lowest callbackOrder value. public int callbackOrder { get { return 0; } } public void OnProcessShader( Shader shader, ShaderSnippetData snippet, IList<ShaderCompilerData> data) { for (int i = 0; i < data.Count; ++i) { if (data[i].shaderKeywordSet.IsEnabled(m_KeywordToStrip) && !EditorUserBuildSettings.development) { var foundKeywordSet = string.Join(" ", data[i].shaderKeywordSet.GetShaderKeywords()); Debug.Log("Found keyword DEBUG in variant " + i + " of shader " + shader); Debug.Log("Keyword set: " + foundKeywordSet); data.RemoveAt(i); --i; } } }}

Methods

Method

Description

DisableDisable a specific shader keyword.
EnableEnable a specific shader keyword.
GetShaderKeywordsReturn an array with all the enabled keywords in the ShaderKeywordSet.
IsEnabledCheck whether a specific shader keyword is enabled.