using System.Collections.Generic; using UnityEditor.Experimental.AssetImporters; using UnityEditor.ShaderGraph; using UnityEngine; using System.IO; using System.Linq; using System.Text; [ScriptedImporter(2, "ShaderSubGraph")] public class ShaderSubGraphImporter : ScriptedImporter { public override void OnImportAsset(AssetImportContext ctx) { var textGraph = File.ReadAllText(ctx.assetPath, Encoding.UTF8); var graph = JsonUtility.FromJson(textGraph); if (graph == null) return; var sourceAssetDependencyPaths = new List(); foreach (var node in graph.GetNodes()) node.GetSourceAssetDependencies(sourceAssetDependencyPaths); var graphAsset = ScriptableObject.CreateInstance(); graphAsset.subGraph = graph; ctx.AddObjectToAsset("MainAsset", graphAsset); ctx.SetMainObject(graphAsset); foreach (var sourceAssetDependencyPath in sourceAssetDependencyPaths.Distinct()) ctx.DependsOnSourceAsset(sourceAssetDependencyPath); } }