Skip to content

Commit 9c2b2ea

Browse files
committed
create a variant importer
1 parent ac0cad6 commit 9c2b2ea

5 files changed

Lines changed: 80 additions & 1 deletion

File tree

Editor/Importer/GraphlitImporter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public override void OnImportAsset(AssetImportContext ctx)
6464

6565
}
6666

67-
private static void GenerateShaderVariant(AssetImportContext ctx, BuildTarget target, ShaderGraphView graphView, int id)
67+
public static void GenerateShaderVariant(AssetImportContext ctx, BuildTarget target, ShaderGraphView graphView, int id)
6868
{
6969
var shaderNodes = graphView.cachedNodesForBuilder;
7070
var dependencies = new HashSet<string>();

Editor/Importer/VariantImporter.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.IO;
2+
using UnityEditor;
3+
using UnityEditor.AssetImporters;
4+
using UnityEngine;
5+
using static Graphlit.GraphData;
6+
7+
namespace Graphlit
8+
{
9+
[ScriptedImporter(1, new[] { "graphlitvariant" }, -1)]
10+
public class VariantImporter : ScriptedImporter
11+
{
12+
[SerializeField] public string graphRelativePath = "";
13+
[SerializeField] public string nameSuffix = "Variant";
14+
[SerializeField] public OutlinePassMode outlinePass = OutlinePassMode.Disabled;
15+
[SerializeField] public bool depthFillPass = false;
16+
17+
internal void OverrideVariantData(GraphData data)
18+
{
19+
data.shaderName += " " + nameSuffix;
20+
data.outlinePass = outlinePass;
21+
data.depthFillPass = depthFillPass;
22+
}
23+
24+
public override void OnImportAsset(AssetImportContext ctx)
25+
{
26+
var target = ctx.selectedBuildTarget;
27+
28+
string assetPath = "Packages/com.z3y.graphlit/Shaders/Unlit.graphlit";
29+
30+
if (!string.IsNullOrEmpty(graphRelativePath))
31+
{
32+
var dir = Path.GetDirectoryName(ctx.assetPath);
33+
assetPath = Path.Combine(dir, graphRelativePath) + ".graphlit";
34+
}
35+
string guid = AssetDatabase.AssetPathToGUID(assetPath);
36+
37+
var data = GraphlitImporter.ReadGraphData(guid);
38+
OverrideVariantData(data.data);
39+
data.data.unlocked = false;
40+
var graphView = new ShaderGraphView(null, assetPath);
41+
data.PopulateGraph(graphView);
42+
43+
graphView.UpdateCachedNodesForBuilder();
44+
45+
GraphlitImporter.GenerateShaderVariant(ctx, target, graphView, 0);
46+
}
47+
48+
[MenuItem("Assets/Create/Graphlit/Variant")]
49+
public static void CreateVariantFile()
50+
{
51+
ProjectWindowUtil.CreateAssetWithContent($"Graphlit Variant.graphlitvariant", "");
52+
}
53+
}
54+
}

Editor/Importer/VariantImporter.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Shaders/Toon Outline.graphlitvariant

Whitespace-only changes.

Shaders/Toon Outline.graphlitvariant.meta

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)