浏览代码

redoing importers

/main
Tim Cooper 7 年前
当前提交
e94975d2
共有 5 个文件被更改,包括 130 次插入128 次删除
  1. 34
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/MasterRemapGraphImporterEditor.cs
  2. 116
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderGraphImporter.cs
  3. 37
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderSubGraphImporterEditor.cs
  4. 58
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderGraphImporterEditor.cs
  5. 13
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderGraphImporterEditor.cs.meta

34
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/MasterRemapGraphImporterEditor.cs


{
AssetImporter importer = target as AssetImporter;
Debug.Assert(importer != null, "importer != null");
ShowGraphEditWindow(importer.assetPath);
}
}
static bool ShowGraphEditWindow(string path)
{
var asset = AssetDatabase.LoadAssetAtPath<Object>(path) as MasterRemapGraphAsset;
if (asset == null)
return false;
var foundWindow = false;
foreach (var w in Resources.FindObjectsOfTypeAll<MaterialGraphEditWindow>())
{
if (w.selected == asset)
{
foundWindow = true;
w.Focus();
}
}
if (!foundWindow)
{
var window = CreateInstance<MaterialGraphEditWindow>();
window.Show();
window.ChangeSelection(asset, typeof(MasterRemapGraph));
ShaderGraphImporterEditor.ShowGraphEditWindow(importer.assetPath);
return true;
}
[OnOpenAsset]
static bool OnOpenAsset(int instanceID, int line)
{
var path = AssetDatabase.GetAssetPath(instanceID);
return ShowGraphEditWindow(path);
}
}

116
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderGraphImporter.cs


using System.IO;
using System.Text;
using UnityEditor;
using UnityEditor.ShaderGraph.Drawing;
using Object = UnityEngine.Object;
using UnityEditor.Experimental.AssetImporters;
class ShaderGraphImporter : ICustomShaderImporter
[ScriptedImporter(2, "shadergraph")]
public class ShaderGraphImporter : ScriptedImporter
{
private string errorShader = @"
Shader ""Hidden/GraphErrorShader2""
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#pragma multi_compile _ UNITY_SINGLE_PASS_STEREO STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON
#include ""UnityCG.cginc""
struct appdata_t {
float4 vertex : POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f {
float4 vertex : SV_POSITION;
UNITY_VERTEX_OUTPUT_STEREO
};
v2f vert (appdata_t v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return fixed4(1,0,1,1);
}
ENDCG
}
}
Fallback Off
}";
public override void OnImportAsset(AssetImportContext ctx)
{
var text = GetShaderText<MaterialGraph>(ctx.assetPath);
if (text == null)
text = errorShader;
var shader = ShaderUtil.CreateShaderAsset(text);
ctx.AddObjectToAsset("MainAsset", shader);
ctx.SetMainObject(shader);
}
private static string GetShaderText<T>(string path) where T : IShaderGraph
{
try

// ignored
}
return null;
}
public string GetShaderText(string path)
{
if (path.EndsWith("LayeredShaderGraph", StringComparison.InvariantCultureIgnoreCase))
return GetShaderText<LayeredShaderGraph>(path);
if (path.EndsWith("shaderGraph", StringComparison.InvariantCultureIgnoreCase))
return GetShaderText<MaterialGraph>(path);
return null;
}
public bool IsValidForPath(string path)
{
return
path.EndsWith("LayeredShaderGraph", StringComparison.InvariantCultureIgnoreCase)
|| path.EndsWith("shaderGraph", StringComparison.InvariantCultureIgnoreCase);
}
public void OpenAsset(string path)
{
ShowGraphEditWindow(path);
}
internal static void ShowGraphEditWindow(string path)
{
var asset = AssetDatabase.LoadAssetAtPath<Object>(path);
var extension = Path.GetExtension(path);
Type graphType;
if (extension == ".ShaderGraph")
graphType = typeof(MaterialGraph);
else if (extension == ".LayeredShaderGraph")
graphType = typeof(LayeredShaderGraph);
else if (extension == ".ShaderSubGraph")
graphType = typeof(SubGraph);
else if (extension == ".ShaderRemapGraph")
graphType = typeof(MasterRemapGraph);
else
return;
var foundWindow = false;
foreach (var w in Resources.FindObjectsOfTypeAll<MaterialGraphEditWindow>())
{
if (w.selected == asset)
{
foundWindow = true;
w.Focus();
}
}
if (!foundWindow)
{
var window = ScriptableObject.CreateInstance<MaterialGraphEditWindow>();
window.Show();
window.ChangeSelection(asset, graphType);
}
}
}

37
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderSubGraphImporterEditor.cs


using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.ShaderGraph.Drawing;
using UnityEditor.ShaderGraph;
using Debug = System.Diagnostics.Debug;
[CustomEditor(typeof(ShaderSubGraphImporter))]

{
AssetImporter importer = target as AssetImporter;
Debug.Assert(importer != null, "importer != null");
ShowGraphEditWindow(importer.assetPath);
ShaderGraphImporterEditor.ShowGraphEditWindow(importer.assetPath);
}
private static bool ShowGraphEditWindow(string path)
{
var asset = AssetDatabase.LoadAssetAtPath<Object>(path) as MaterialSubGraphAsset;
if (asset == null)
return false;
var foundWindow = false;
foreach (var w in Resources.FindObjectsOfTypeAll<MaterialGraphEditWindow>())
{
if (w.selected == asset)
{
foundWindow = true;
w.Focus();
}
}
if (!foundWindow)
{
var window = CreateInstance<MaterialGraphEditWindow>();
window.Show();
window.ChangeSelection(asset, typeof(SubGraph));
}
return true;
}
[OnOpenAsset]
static bool OnOpenAsset(int instanceID, int line)
{
var path = AssetDatabase.GetAssetPath(instanceID);
return ShowGraphEditWindow(path);
}
}

58
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderGraphImporterEditor.cs


using System;
using System.IO;
using UnityEditor;
using UnityEditor.Experimental.AssetImporters;
using UnityEditor.ShaderGraph;
using UnityEditor.ShaderGraph.Drawing;
using UnityEngine;
using Debug = System.Diagnostics.Debug;
using Object = UnityEngine.Object;
[CustomEditor(typeof(ShaderGraphImporter))]
public class ShaderGraphImporterEditor : ScriptedImporterEditor
{
public override void OnInspectorGUI()
{
if (GUILayout.Button("Open Shader Editor"))
{
AssetImporter importer = target as AssetImporter;
Debug.Assert(importer != null, "importer != null");
ShowGraphEditWindow(importer.assetPath);
}
}
internal static void ShowGraphEditWindow(string path)
{
var asset = AssetDatabase.LoadAssetAtPath<Object>(path);
var extension = Path.GetExtension(path);
Type graphType;
if (extension == ".ShaderGraph")
graphType = typeof(MaterialGraph);
else if (extension == ".LayeredShaderGraph")
graphType = typeof(LayeredShaderGraph);
else if (extension == ".ShaderSubGraph")
graphType = typeof(SubGraph);
else if (extension == ".ShaderRemapGraph")
graphType = typeof(MasterRemapGraph);
else
return;
var foundWindow = false;
foreach (var w in Resources.FindObjectsOfTypeAll<MaterialGraphEditWindow>())
{
if (w.selected == asset)
{
foundWindow = true;
w.Focus();
}
}
if (!foundWindow)
{
var window = ScriptableObject.CreateInstance<MaterialGraphEditWindow>();
window.Show();
window.ChangeSelection(asset, graphType);
}
}
}

13
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderGraphImporterEditor.cs.meta


fileFormatVersion: 2
guid: 575791f1eb8b54602829004c8bcbe802
timeCreated: 1503169819
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存