浏览代码

Merge pull request #1355 from Unity-Technologies/sg/show-generated-code

Show generated code for shader graph
/main
GitHub 6 年前
当前提交
57c37d06
共有 5 个文件被更改,包括 71 次插入13 次删除
  1. 2
      ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/AbstractMaterialGraph.cs
  2. 36
      ShaderGraph/com.unity.shadergraph/Editor/Data/Util/GraphUtil.cs
  3. 2
      ShaderGraph/com.unity.shadergraph/Editor/Drawing/MaterialGraphEditWindow.cs
  4. 3
      ShaderGraph/com.unity.shadergraph/Editor/Drawing/Views/GraphEditorView.cs
  5. 41
      ShaderGraph/com.unity.shadergraph/Editor/Drawing/Views/MaterialNodeView.cs

2
ShaderGraph/com.unity.shadergraph/Editor/Data/Graphs/AbstractMaterialGraph.cs


set { m_PreviewData = value; }
}
public string name { get; set; }
[SerializeField]
string m_Path;

36
ShaderGraph/com.unity.shadergraph/Editor/Data/Util/GraphUtil.cs


using System;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityEditorInternal;
using Debug = UnityEngine.Debug;
using System.Reflection;
namespace UnityEditor.ShaderGraph

}
return string.Format(duplicateFormat, name, duplicateNumber);
}
public static bool WriteToFile(string path, string content)
{
try
{
File.WriteAllText(path, content);
return true;
}
catch (Exception e)
{
Debug.LogError(e);
return false;
}
}
public static void OpenFile(string path)
{
if (!File.Exists(Path.GetFullPath(path)))
{
Debug.LogError(string.Format("Path {0} doesn't exists", path));
return;
}
string file = Path.GetFullPath(path);
ProcessStartInfo pi = new ProcessStartInfo(file);
pi.Arguments = Path.GetFileName(file);
pi.UseShellExecute = true;
pi.WorkingDirectory = Path.GetDirectoryName(file);
pi.FileName = ScriptEditorUtility.GetExternalScriptEditor();
pi.Verb = "OPEN";
Process.Start(pi);
}
}
}

2
ShaderGraph/com.unity.shadergraph/Editor/Drawing/MaterialGraphEditWindow.cs


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

using UnityEditor.Graphing;
using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;
using Edge = UnityEditor.Experimental.UIElements.GraphView.Edge;
#if UNITY_2018_1

3
ShaderGraph/com.unity.shadergraph/Editor/Drawing/Views/GraphEditorView.cs


if (showInProjectRequested != null)
showInProjectRequested();
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
graph.name = assetName;
m_GraphView = new MaterialGraphView(graph) { name = "GraphView", persistenceKey = "MaterialGraphView" };
m_GraphView.SetupZoom(0.05f, ContentZoomer.DefaultMaxScale);
m_GraphView.AddManipulator(new ContentDragger());

41
ShaderGraph/com.unity.shadergraph/Editor/Drawing/Views/MaterialNodeView.cs


public override void BuildContextualMenu(ContextualMenuPopulateEvent evt)
{
if (evt.target is Node)
evt.menu.AppendAction("Copy shader", ConvertToShader, node.hasPreview ? ContextualMenu.MenuAction.StatusFlags.Normal : ContextualMenu.MenuAction.StatusFlags.Hidden);
{
evt.menu.AppendAction("Copy Shader", CopyToClipboard, node.hasPreview ? ContextualMenu.MenuAction.StatusFlags.Normal : ContextualMenu.MenuAction.StatusFlags.Hidden);
evt.menu.AppendAction("Show Generated Code", ShowGeneratedCode, node.hasPreview ? ContextualMenu.MenuAction.StatusFlags.Normal : ContextualMenu.MenuAction.StatusFlags.Hidden);
}
void ConvertToShader()
void CopyToClipboard()
{
GUIUtility.systemCopyBuffer = ConvertToShader();
}
public string SanitizeName(string name)
{
return new string(name.Where( c => !Char.IsWhiteSpace(c) ).ToArray());
}
public void ShowGeneratedCode()
{
var graph = (AbstractMaterialGraph)node.owner;
string path = String.Format("Temp/GeneratedFromGraph-{0}-{1}-{2}.shader", SanitizeName(graph.name), SanitizeName(node.name), node.guid );
if( GraphUtil.WriteToFile(path, ConvertToShader()) )
GraphUtil.OpenFile(path);
}
string ConvertToShader()
{
var shader = masterNode.GetShader(GenerationMode.ForReals, node.name, out textureInfo);
GUIUtility.systemCopyBuffer = shader;
}
else
{
var graph = (AbstractMaterialGraph)node.owner;
GUIUtility.systemCopyBuffer = graph.GetShader(node, GenerationMode.ForReals, node.name).shader;
}
return masterNode.GetShader(GenerationMode.ForReals, node.name, out textureInfo);
var graph = (AbstractMaterialGraph)node.owner;
return graph.GetShader(node, GenerationMode.ForReals, node.name).shader;
}
void UpdateSettingsExpandedState()

正在加载...
取消
保存