# ShaderBuildSettings

> Settings configuration data for how the shaders are handled during the build process.

## Definition

* **Type:** Struct
* **Namespace:** [UnityEditor.Shaders](/engine/6000.7/script-reference/unityeditor/shaders.md)
* **Assembly:** UnityEditor.CoreModule

```csharp
public struct ShaderBuildSettings
```

## Remarks

Use `ShaderBuildSettings` to optimize shader variants for a project or build target.

A `ShaderBuildSettings` struct contains `KeywordDeclarationOverride` structs that either enable or disable shader keywords, strip keywords from the build entirely, or set whether Unity uses static or dynamic branching for keyword sets.

To set named constant numeric values in the shader, use an array of defines.

This settings data is applied either to [EditorGraphicsSettings.SetShaderBuildSettings](/engine/6000.7/script-reference/unityeditor/rendering/editorgraphicssettings/setshaderbuildsettings.md) or the build profile data.

To configure keyword overrides in the Editor, go to either the **Graphics** settings window or the **Build Profiles** window.

Additional Resources: [EditorGraphicsSettings.SetShaderBuildSettings](/engine/6000.7/script-reference/unityeditor/rendering/editorgraphicssettings/setshaderbuildsettings.md). [EditorGraphicsSettings.GetShaderBuildSettings](/engine/6000.7/script-reference/unityeditor/rendering/editorgraphicssettings/getshaderbuildsettings.md).

## Examples

```csharp
using System;
using System.Collections.Generic;
using UnityEditor.Rendering;
using UnityEditor.Shaders;
using UnityEngine;

using KeywordDeclarationOverride = UnityEditor.Shaders.ShaderBuildSettings.KeywordDeclarationOverride;
using KeywordOverrideInfo = UnityEditor.Shaders.ShaderBuildSettings.KeywordOverrideInfo;

public class ShaderBuildSettingsExample
{
    void AddKeywordDeclarationOverride()
    {
        // Get current shader build settings either from the currently active build profile
        // or if none is active, the global graphics settings.
        ShaderBuildSettings buildSettings = EditorGraphicsSettings.GetShaderBuildSettings();

        // Create a new keyword declaration override
        var newOverride = new KeywordDeclarationOverride
        {
            // Define the set of keywords to override
            keywords = new KeywordOverrideInfo[] { new("_", true), new("LINEAR_BEAUTY", true), new("EXPONENTIAL_BEAUTY", true) },

            // Use dynamic branching. Note: The shader import fails if the shader code isn't compatible with dynamic branching.
            variantGenerationMode = ShaderBuildSettings.ShaderVariantGenerationMode.SingleVariantWithDynamicBranching
        };

        // Check if each keyword is valid
        foreach (var kw in newOverride.keywords)
        {
            Debug.Log(kw.name + (kw.keepInBuild ? ": keep" : ": strip"));
            if (!kw.IsValid())
                return;
        }

        // Check if an override is valid.
        string validationErrors;
        if (!newOverride.IsValid(out validationErrors))
        {
            Debug.LogError(validationErrors);
            return;
        }

        // Get the copy of the existing overrides, and add the new overrides struct
        var kwDeclarationOverrides = new List<KeywordDeclarationOverride>(buildSettings.GetKeywordDeclarationOverridesCopy());
        kwDeclarationOverrides.Add(newOverride);

        // Set the modified keyword declaration overrides on the settings data struct. Throw an exception if the data is invalid.
        try
        {
            buildSettings.KeywordDeclarationOverrides = kwDeclarationOverrides.ToArray();
        }
        catch (ArgumentException ex)
        {
            Debug.LogError(ex.Message);
            return;
        }

        // Apply the modified settings data back to the Graphics settings or Build Profile settings
        EditorGraphicsSettings.SetShaderBuildSettings(buildSettings);
    }
}
```

## Constructors

| Constructor                                                                                              | Description                                                                                        |
| -------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
| [ShaderBuildSettings()](/engine/6000.7/script-reference/unityeditor/shaders/shaderbuildsettings/ctor.md) | Creates an empty settings data structure for controlling how shaders are handled during the build. |

## Properties

| Property                                                                                                                              | Description                                                                       |
| ------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- |
| [Defines](/engine/6000.7/script-reference/unityeditor/shaders/shaderbuildsettings/defines.md)                                         | An array of constant defines for the shaders in this settings configuration.      |
| [KeywordDeclarationOverrides](/engine/6000.7/script-reference/unityeditor/shaders/shaderbuildsettings/keyworddeclarationoverrides.md) | An array of all the keyword declaration overrides in this settings configuration. |

## Methods

| Method                                                                                                                                              | Description                                       |
| --------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------- |
| [GetDefinesCopy](/engine/6000.7/script-reference/unityeditor/shaders/shaderbuildsettings/getdefinescopy.md)                                         | Gets a copy of the constant defines.              |
| [GetKeywordDeclarationOverridesCopy](/engine/6000.7/script-reference/unityeditor/shaders/shaderbuildsettings/getkeyworddeclarationoverridescopy.md) | Gets a copy of the keyword declaration overrides. |

## Static Methods

| Method                                                                                                                                                | Description                                      |
| ----------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------ |
| [ValidateDefines](/engine/6000.7/script-reference/unityeditor/shaders/shaderbuildsettings/validatedefines.md)                                         | Validates the given array of constant defines.   |
| [ValidateKeywordDeclarationOverrides](/engine/6000.7/script-reference/unityeditor/shaders/shaderbuildsettings/validatekeyworddeclarationoverrides.md) | Validates the keyword declaration override data. |

## Structs

| Struct                                                                                                                                                  | Description                                                           |
| ------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------- |
| [ShaderBuildSettings.KeywordDeclarationOverride](/engine/6000.7/script-reference/unityeditor/shaders/shaderbuildsettings/keyworddeclarationoverride.md) | Shader build settings data element to override keyword declarations.  |
| [ShaderBuildSettings.KeywordOverrideInfo](/engine/6000.7/script-reference/unityeditor/shaders/shaderbuildsettings/keywordoverrideinfo.md)               | Data representing a single keyword in a keyword declaration override. |

## Enums

| Enum                                                                                                                                                      | Description                                                                                               |
| --------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------- |
| [ShaderBuildSettings.ShaderVariantGenerationMode](/engine/6000.7/script-reference/unityeditor/shaders/shaderbuildsettings/shadervariantgenerationmode.md) | Options for overriding how Unity generates shader variants for the keywords in the shader build settings. |
