How to save emissive Material assets using "_EMISSION" keyword by script?

I have an editor script that generates emissive Material assets using the Standard shader. This works by first creating a temporary Material, then enabling the keyword “_EMISSION” and finally saving the asset using AssetDatabase (example code below).

 public static class EmissiveMaterialCreator
 {
     [MenuItem("Debug/Create Emissive Material Asset")]
     public static void CreateEmissiveMaterialAsset()
     {
         Material material = CreateEmissiveMaterial();
         AssetDatabase.CreateAsset(material, "Assets/EmissiveMaterial.asset");
     }
     private static Material CreateEmissiveMaterial()
     {
         Material material = new Material(Shader.Find("Standard"));
 
         material.name = "Emissive Material";
         material.color = Color.white;
 
         material.EnableKeyword("_EMISSION");
         material.SetColor("_EmissionColor", Color.blue);
 
         return material;
     }
 }

However when using this script, the asset created doesn’t have an enabled toggle at the property “Emission”, and the material does not render correctly in the scene and during GI baking.

I realized when playing with the asset file that toggling this property manually in the inspector, a property “m_LightmapFlags” in the file changes from 4 to 2, but when created by script this value is never set to 2, which would enable to “Emission” toggle.

 %YAML 1.1
 %TAG !u! tag:unity3d.com,2011:
 --- !u!21 &2100000
 Material:
   serializedVersion: 6
   m_ObjectHideFlags: 0
   m_CorrespondingSourceObject: {fileID: 0}
   m_PrefabInstance: {fileID: 0}
   m_PrefabAsset: {fileID: 0}
   m_Name: EmissiveMaterial
   m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
   m_ShaderKeywords: _EMISSION
   m_LightmapFlags: 4
   ...

My question now is how should I proceed to change this property so that the materials get saved properly ?

A quick and dirty fix for this issue would be to directly edit the asset file, here is an basic example of how this would work :

  public static class EmissiveMaterialCreator
  {
    public static void CreateEmissiveCubePrefab()
    {
        Material material = CreateEmissiveMaterial();
        CreateMaterialAsset(material, "Assets/EmissiveMaterial.asset");
    }

    public static void CreateMaterialAsset(Material material, string path)
    {
        // First, create normally the asset using AssetDatabase...
        AssetDatabase.CreateAsset(material, path);

        // ... then read the file content and edit it if the keyword for emission is found
        string text = File.ReadAllText(path);
        if (text.Contains("_EMISSION"))
        {
            text = text.Replace("m_LightmapFlags: 4", "m_LightmapFlags: 2");
            File.WriteAllText(path, text);
        }

        // ... finally refresh the asset to avoid any issue in the editor
        AssetDatabase.Refresh();
    }
  }

This doesn’t account for the potential variation of the flag value (1, 3, 5, etc.), but a basic parsing improvement would take care of this detail.
The point is in the end, the Material asset is now behaving as expected.

The solution is to set the field globalIlluminationFlags of the Material to whichever value is required. In my case, the emission has to be baked, so the value to set is BakedEmissive.

Tip : if the material is not emissive, the value would be EmissiveIsBlack.

private static Material CreateEmissiveMaterial()
{
    Material material = new Material(Shader.Find("Standard"));

    material.name = "Emissive Material";
    material.color = Color.white;

    material.EnableKeyword("_EMISSION");
    material.globalIlluminationFlags = MaterialGlobalIlluminationFlags.BakedEmissive;

    material.SetColor("_EmissionColor", Color.blue);

    return material;
}