using System;
using System.IO;
using UnityEngine;
namespace UnityEditor.RenderPipelines.Core
{
internal static class AssetCreationUtil
{
///
/// Prompts the user to save a new asset created from the shader template, and creates and with it.
///
/// Default material name.
/// A delegate (callback) that will be invoked with the .
/// Path of the Shader Template file.
internal static void CreateShaderAndMaterial(string name, Action callback, string shaderTemplateAssetPath)
{
CreateShader(
name,
(shader) =>
{
Material material = new Material(shader);
var path = AssetDatabase.GetAssetPath(shader);
AssetDatabase.CreateAsset(material, Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path) + ".mat"));
AssetDatabase.SaveAssetIfDirty(material);
AssetDatabase.Refresh(ImportAssetOptions.Default);
callback?.Invoke(material);
},
shaderTemplateAssetPath);
}
///
/// Prompts the user to save a new asset created using the .
///
/// Default material name.
/// A delegate (callback) that will be invoked with the .
/// to create the new with.
internal static void CreateMaterial(string name, Action callback, Shader shader)
{
if (shader == null)
{
Debug.LogError($"Null Shader reference. Cannot create Material {name}.");
return;
}
CreateAsset(
name,
(materialPath) =>
{
Material material = new Material(shader);
AssetDatabase.CreateAsset(material, materialPath);
AssetDatabase.SaveAssetIfDirty(material);
AssetDatabase.Refresh(ImportAssetOptions.Default);
callback?.Invoke(material);
},
"mat",
typeof(Material)
);
}
internal static void CreateShader(string name, Action callback, string shaderTemplateAssetPath)
{
if (!AssetDatabase.AssetPathExists(shaderTemplateAssetPath))
{
Debug.LogError($"Shader Template File missing at path: {shaderTemplateAssetPath}.");
return;
}
CreateAsset(
name,
(shaderPath) =>
{
string fileName = Path.GetFileNameWithoutExtension(shaderPath);
string templateCode = File.ReadAllText(shaderTemplateAssetPath);
templateCode = templateCode.Replace("#SCRIPTNAME#", fileName);
File.WriteAllText(shaderPath, templateCode);
AssetDatabase.Refresh();
AssetDatabase.ImportAsset(shaderPath);
Shader shader = AssetDatabase.LoadAssetAtPath(shaderPath);
callback?.Invoke(shader);
},
"shader",
typeof(Shader)
);
}
static void CreateAsset(string name, Action callback = null, string extension = "asset", Type type = null)
{
AssetCreationCallback assetCreationCallback = ScriptableObject.CreateInstance();
assetCreationCallback.callback = callback;
assetCreationCallback.extension = extension;
var icon = AssetPreview.GetMiniTypeThumbnail(type);
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, assetCreationCallback, name, icon, null, false);
}
class AssetCreationCallback : ProjectWindowCallback.EndNameEditAction
{
public Action callback;
public string extension;
public override void Action(int instanceId, string pathName, string resourceFile)
{
string path = AssetDatabase.GenerateUniqueAssetPath(pathName + $".{extension}");
callback?.Invoke(path);
}
}
}
}