Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions Editor/ShaderGraphPatcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
Expand Down Expand Up @@ -32,7 +32,7 @@ enum UVTypeChoiceEnum : int {
};

[MenuItem("Tools/Shader Graph Patcher")]
public static void Show() {
public static void ShowPatcher() {
ShaderGraphPatcherWindow wnd = GetWindow<ShaderGraphPatcherWindow>();
wnd.titleContent = new GUIContent("ShaderGraphPatcherWindow");
}
Expand Down Expand Up @@ -308,7 +308,7 @@ List<string> FixShader(string pathToShader, PatchData patchData) {
}

// Taken from https://discussions.unity.com/t/how-to-get-shader-source-code-from-script/839046/3
private static object GetGraphData(string shaderAssetPath) {
private static ScriptableObject GetGraphData(string shaderAssetPath) {
var importer = AssetImporter.GetAtPath(shaderAssetPath);

var textGraph = File.ReadAllText(importer.assetPath, System.Text.Encoding.UTF8);
Expand Down Expand Up @@ -357,8 +357,8 @@ private static object GetGraphData(string shaderAssetPath) {
// graphObject.graph.ValidateGraph();
graphDataType.GetMethod("ValidateGraph")!.Invoke(graphDataInstance, null);

// return graphData.graph
return graphDataInstance;
// return graphObject instead of graphDataInstance
return graphObject;
}

//Taken from https://discussions.unity.com/t/how-to-get-shader-source-code-from-script/839046/3
Expand All @@ -367,21 +367,41 @@ private static string GenerateShaderCodeFromShaderGraphAsset(string shaderAssetP
Type.GetType("UnityEditor.ShaderGraph.Generator, Unity.ShaderGraph.Editor")!;
Type modeType =
Type.GetType("UnityEditor.ShaderGraph.GenerationMode, Unity.ShaderGraph.Editor")!;
Type graphObjectType = // Need this type to get the 'graph' property
Type.GetType("UnityEditor.Graphing.GraphObject, Unity.ShaderGraph.Editor")!;
Type graphDataType = // Need this type to call OnDisable/Dispose
Type.GetType("UnityEditor.ShaderGraph.GraphData, Unity.ShaderGraph.Editor")!;

shaderName ??= Path.GetFileNameWithoutExtension(shaderAssetPath);

object graphData = GetGraphData(shaderAssetPath);
// 1. Get the GraphObject (which is a ScriptableObject)
ScriptableObject graphObject = GetGraphData(shaderAssetPath);

// 2. Get the 'graph' property (the GraphData) from the GraphObject
var graphObject_graphProperty = graphObjectType.GetProperty("graph")!;
object graphData = graphObject_graphProperty.GetValue(graphObject);

// new Generator(graphData, null, GenerationMode.ForReals, assetName, target:null, assetCollection:null, humanReadable: true);
// 3. Use the graphData to create the generator and get the code
object forReals = ((FieldInfo)modeType.GetMember("ForReals")[0]).GetValue(null);
object generator = Activator.CreateInstance(
generatorType,
new object[] { graphData, null, forReals, shaderName, null, null, true }
);
object shaderCode = generatorType
string shaderCode = (string)generatorType
.GetProperty("generatedShader", BindingFlags.Public | BindingFlags.Instance)!
.GetValue(generator);

var onDisableMethod = graphDataType.GetMethod("OnDisable");
if (onDisableMethod != null) {
onDisableMethod.Invoke(graphData, null);
}

if (graphData is IDisposable disposableGraph) {
disposableGraph.Dispose();
}

DestroyImmediate(graphObject);

return (string)shaderCode;
return shaderCode;
}
}
}