Create a material from shader graph asset in editor script

How can I create a material in an editor script in much the same way as I would from a shader asset?

Shader myShader = (Shader)AssetDatabase.LoadAssetAtPath(shaderPath, typeof(Shader));

Material mat = new Material(myshader);

Material mat2 = new Material( ?? myshader.shadergraph)

You can do that exactly like if it’s a shader asset, just as you said :slight_smile:

Material material = null;
Shader shader = Resources.Load<Shader>("MyShaderGraphAsset");
if (shader != null)
{
    material = new Material(shader);
}

This will fail unless the shader graph is in a resources folder. From the manual:

“Loads the asset of the requested type stored at path in a Resources folder using a generic parameter type filter of type T.”

I need to load it from a specific path that is not in a Resources folder and I don’t want it to be included in builds.

Try something like this, works for me!

                    // Create a new material with the same properties as the imported material
                    Material newMaterial = null;

                    defaultShader = Shader.Find("Shader Graphs/SphereMask");

                    if (defaultShader != null)
                    {
                        newMaterial = new Material(defaultShader);
                    }
                    else
                    {
                        Debug.LogError("Couldn't load shader");
                    }