Version: Unity 6.2 (6000.2)
LanguageEnglish
  • C#

ShaderKeywordSet

struct in UnityEngine.Rendering

/

Implemented in:UnityEngine.CoreModule

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

A collection of ShaderKeyword that represents a specific shader variant.

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.

Additional resources: ShaderKeyword, Shader.EnableKeyword, Shader.EnableKeyword.

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;
            }
        }
    }
}

Public 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.