Version: Unity 6.6 Alpha (6000.6)
LanguageEnglish
  • C#

ShaderBuildSettings.GetDefinesCopy

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

Declaration

public string[] GetDefinesCopy();

Returns

string[] An array of constant defines.

Description

Gets a copy of the constant defines.

This method gets the constant define data from a ShaderBuildSettings struct. To make sure data stays valid, you can't read or write the original data. To apply the modified data back to the original data, use ShaderBuildSettings.Defines. The valid format for the array element data is a pair of identifier and numeric value, separated by a whitespace.

Additional resources: ShaderBuildSettings.

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


public class ConstantDefinesExample
{
    void AddShaderConstantDefines()
    {
        // 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();

        // Get the copy of the existing defines and append the new ones
        var allDefines = new List<string>(buildSettings.GetDefinesCopy());
        allDefines.Add("MAX_NUM_LIGHTS 8");
        allDefines.Add("EPSILON 0.1f");
        var defineArray = allDefines.ToArray();

        // Check if the define array is valid
        string validationErrors;
        if (!ShaderBuildSettings.ValidateDefines(defineArray, out validationErrors))
        { 
            Debug.LogWarning(validationErrors);
            return;
        }

        // Set the modified defines. Throw an exception if the data is invalid.
        try
        {
            buildSettings.Defines = defineArray;
        }
        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);
    }
}