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+ }
0 commit comments