Files
Test/Library/PackageCache/com.unity.shadergraph@8b7e693cdbfe/Editor/AssetCallbacks/CreateShaderGraph.cs
T
2026-11-01 23:39:55 +02:00

125 lines
6.1 KiB
C#

using System;
using System.IO;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.Rendering;
namespace UnityEditor.ShaderGraph
{
static class CreateShaderGraph
{
[MenuItem("Assets/Create/Shader Graph/Blank Shader Graph", priority = CoreUtils.Sections.section1 + CoreUtils.Priorities.assetsCreateShaderMenuPriority)]
public static void CreateBlankShaderGraph()
{
CreateFromTemplate(null, string.Empty, useProjectBrowserIfAvailable:true);
}
[MenuItem("Assets/Create/Shader Graph/From Template...")]
public static void CreateFromTemplate()
{
CreateFromTemplate(null, useProjectBrowserIfAvailable:true);
}
/// <summary>
/// Prompts the user to create a new Shader Graph from a Template.
/// /// The Template Browser opens if <paramref name="templateSourcePath"/> is null.
/// </summary>
/// <param name="callback">Callback to execute with the created shader graph asset.</param>
/// <param name="templateSourcePath">Shader Graph template file. Use string.Empty for Blank Shader Graph.</param>
/// <param name="hiddenQuery">Defines a search query that will pre-filter the list of available templates</param>
/// <param name="initialQuery">Defines a search query that will filter the list of available templates (and will be editable by the user)</param>
/// <param name="useProjectBrowserIfAvailable">When false, the save file dialog is used, otherwise the project window is used (if visible)</param>
/// <param name="filename">New Shader Graph filename.</param>
internal static void CreateFromTemplate(Action<string> callback, string templateSourcePath = null, string filename = null, string hiddenQuery = null, string initialQuery = null, bool useProjectBrowserIfAvailable = false)
{
if (!string.IsNullOrEmpty(templateSourcePath))
{
if (!AssetDatabase.AssetPathExists(templateSourcePath))
{
Debug.LogError($"Shader Graph Template File missing at path: {templateSourcePath}.");
return;
}
if (Path.GetExtension(templateSourcePath) != $".{ShaderGraphImporter.Extension}")
{
Debug.LogError($"Shader Graph Template File {templateSourcePath} is not a Shader Graph asset.");
return;
}
}
bool projectWindowIsVisible = useProjectBrowserIfAvailable && EditorWindow.HasOpenInstances<ProjectBrowser>();
NewGraphFromTemplateAction action = ScriptableObject.CreateInstance<NewGraphFromTemplateAction>();
if (callback != null)
action.Callback = callback;
if (!string.IsNullOrEmpty(filename))
action.Filename = filename;
ShaderGraphTemplateHelper shaderGraphTemplateHelper = new ShaderGraphTemplateHelper();
if (templateSourcePath == null)
{
GraphViewTemplateWindow.ShowCreateFromTemplate(shaderGraphTemplateHelper, action.CreateAndRenameGraphFromTemplate, showSaveDialog: !projectWindowIsVisible, hiddenSearchQuery: hiddenQuery, initialSearchQuery: initialQuery);
}
else
{
if (shaderGraphTemplateHelper.TryGetTemplate(templateSourcePath, out GraphViewTemplateDescriptor graphViewTemplate))
shaderGraphTemplateHelper.RaiseTemplateUsed(graphViewTemplate);
if (projectWindowIsVisible)
{
action.CreateAndRenameGraphFromTemplate(templateSourcePath, null);
}
else
{
string graphDestinationPath = shaderGraphTemplateHelper.saveFileDialogHelper.OpenSaveFileDialog();
if (!string.IsNullOrEmpty(graphDestinationPath))
action.CreateAndRenameGraphFromTemplate(templateSourcePath, graphDestinationPath);
}
}
}
/// <summary>
/// Prompts the user to create a new Shader Graph from a Template and associated Material.
/// The Template Browser opens if <paramref name="templateSourcePath"/> is null.
/// </summary>
/// <param name="callback">Callback to execute with the created material.</param>
/// <param name="templateSourcePath">Shader Graph template file. Use string.Empty for Blank Shader Graph.</param>
/// /// <param name="filename">New Shader Graph filename.</param>
internal static void CreateGraphAndMaterialFromTemplate(Action<Material> callback = null, string templateSourcePath = null, string filename = null, string hiddenSearchQuery = null, string initialSearchQuery = null)
{
CreateFromTemplate(template =>
{
if (!string.IsNullOrEmpty(template))
{
Material material;
if (ShaderGraphPreferences.GetOrPromptGraphTemplateWorkflow() == ShaderGraphPreferences.GraphTemplateWorkflow.MaterialVariant)
{
var srcMaterial = AssetDatabase.LoadAssetAtPath<Material>(template);
material = new Material(srcMaterial);
material.parent = srcMaterial;
}
else
{
var shader = AssetDatabase.LoadAssetAtPath<Shader>(template);
material = new Material(shader);
}
var materialPath = Path.Combine(Path.GetDirectoryName(template), Path.GetFileNameWithoutExtension(template) + ".mat");
AssetDatabase.CreateAsset(material, materialPath);
AssetDatabase.SaveAssetIfDirty(material);
AssetDatabase.Refresh(ImportAssetOptions.Default);
callback?.Invoke(material);
}
},
templateSourcePath,
filename,
hiddenSearchQuery,
initialSearchQuery);
}
}
}