-
Notifications
You must be signed in to change notification settings - Fork 878
Expand file tree
/
Copy pathAssetCreationUtil.cs
More file actions
110 lines (99 loc) · 4.64 KB
/
Copy pathAssetCreationUtil.cs
File metadata and controls
110 lines (99 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.IO;
using UnityEngine;
namespace UnityEditor.RenderPipelines.Core
{
internal static class AssetCreationUtil
{
/// <summary>
/// Prompts the user to save a new <seealso cref="Shader"/> asset created from the shader template, and creates and <seealso cref="Material"/> with it.
/// </summary>
/// <param name="name">Default material name.</param>
/// <param name="callback">A delegate (callback) that will be invoked with the <see cref="Material"/>.</param>
/// <param name="shaderTemplateAssetPath">Path of the Shader Template file.</param>
internal static void CreateShaderAndMaterial(string name, Action<Material> 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);
}
/// <summary>
/// Prompts the user to save a new <seealso cref="Material"/> asset created using the <paramref name="shader"/>.
/// </summary>
/// <param name="name">Default material name.</param>
/// <param name="callback">A delegate (callback) that will be invoked with the <see cref="Material"/>.</param>
/// <param name="shader"><seealso cref="Shader"/> to create the new <seealso cref="Material"/> with.</param>
internal static void CreateMaterial(string name, Action<Material> 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<Shader> 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<Shader>(shaderPath);
callback?.Invoke(shader);
},
"shader",
typeof(Shader)
);
}
static void CreateAsset(string name, Action<string> callback = null, string extension = "asset", Type type = null)
{
AssetCreationCallback assetCreationCallback = ScriptableObject.CreateInstance<AssetCreationCallback>();
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<string> callback;
public string extension;
public override void Action(int instanceId, string pathName, string resourceFile)
{
string path = AssetDatabase.GenerateUniqueAssetPath(pathName + $".{extension}");
callback?.Invoke(path);
}
}
}
}