浏览代码

Move "Copy shader" into context menu

/main
Peter Bay Bastian 7 年前
当前提交
2c5ae2d4
共有 2 个文件被更改,包括 47 次插入53 次删除
  1. 62
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/SearchWindowProvider.cs
  2. 38
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs

62
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/SearchWindowProvider.cs


public class SearchWindowProvider : ScriptableObject, ISearchWindowProvider
{
EditorWindow m_EditorWindow;
IGraph m_Graph;
AbstractMaterialGraph m_Graph;
GraphView m_GraphView;
Texture2D m_Icon;

const string k_ConvertToInlineNode = "Convert To Inline Node";
const string k_ConvertToSubgraph = "Convert To Sub-graph";
const string k_CopyShader = "Copy Shader To Clipboard";
public void Initialize(EditorWindow editorWindow, IGraph graph, GraphView graphView)
public void Initialize(EditorWindow editorWindow, AbstractMaterialGraph graph, GraphView graphView)
{
m_EditorWindow = editorWindow;
m_Graph = graph;

var asset = AssetDatabase.LoadAssetAtPath<MaterialSubGraphAsset>(AssetDatabase.GUIDToAssetPath(guid));
nestedEntries.Add(new NestedEntry
{
title = new [] {"Sub-graph Assets", asset.name},
title = new[] { "Sub-graph Assets", asset.name },
userData = asset
});
}

// Add in contextual node actions
var selection = m_GraphView.selection.OfType<MaterialNodeView>().ToList();
if (selection.Any())
tree.Add(new SearchTreeEntry(new GUIContent(k_ConvertToSubgraph, m_Icon)) { level = 1 });
if (selection.Count == 1 && selection.First().node.hasPreview)
tree.Add(new SearchTreeEntry(new GUIContent(k_CopyShader, m_Icon)) { level = 1 });
foreach (var nestedEntry in nestedEntries)
{

return OnConvertToProperty();
if (entry.name == k_ConvertToInlineNode)
return OnConvertToInlineNode();
if (entry.name == k_ConvertToSubgraph)
return OnConvertToSubgraph();
if (entry.name == k_CopyShader)
return OnCopyShader();
bool OnCopyShader()
{
var copyFromNode = m_GraphView.selection.OfType<MaterialNodeView>().First().node;
List<PropertyCollector.TextureInfo> textureInfo;
var masterNode = copyFromNode as MasterNode;
if (masterNode != null)
{
var shader = masterNode.GetShader(GenerationMode.ForReals, masterNode.name, out textureInfo);
GUIUtility.systemCopyBuffer = shader;
}
else
{
PreviewMode previewMode;
FloatShaderProperty outputIdProperty;
var shader = m_Graph.GetShader(copyFromNode, GenerationMode.ForReals, copyFromNode.name, out textureInfo, out previewMode, out outputIdProperty);
GUIUtility.systemCopyBuffer = shader;
}
return true;
}
bool OnConvertToSubgraph()
{
return true;
}
var graph = m_Graph as AbstractMaterialGraph;
if (graph == null)
return false;
var selectedNodeViews = m_GraphView.selection.OfType<MaterialNodeView>().Select(x => x.node);
foreach (var node in selectedNodeViews.ToArray())
var selectedNodeViews = m_GraphView.selection.OfType<MaterialNodeView>().Select(x => x.node).ToList();
foreach (var node in selectedNodeViews)
{
if (!(node is IPropertyFromNode))
continue;

graph.AddShaderProperty(prop);
m_Graph.AddShaderProperty(prop);
graph.AddNode(propNode);
m_Graph.AddNode(propNode);
var edges = graph.GetEdges(oldSlot.slotReference).ToArray();
var edges = m_Graph.GetEdges(oldSlot.slotReference).ToArray();
graph.Connect(newSlot.slotReference, edge.inputSlot);
m_Graph.Connect(newSlot.slotReference, edge.inputSlot);
graph.RemoveNode(node);
m_Graph.RemoveNode(node);
}
return true;

38
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs


GraphInspectorView m_GraphInspectorView;
ToolbarView m_ToolbarView;
ToolbarButtonView m_TimeButton;
ToolbarButtonView m_CopyToClipboardButton;
AbstractMaterialGraph m_Graph;
PreviewManager m_PreviewManager;

m_TimeButton.text = "Preview rate: " + previewRate;
}));
m_ToolbarView.Add(m_TimeButton);
m_ToolbarView.Add(new ToolbarSeparatorView());
m_CopyToClipboardButton = new ToolbarButtonView() { text = "Copy shader to clipboard" };
m_CopyToClipboardButton.AddManipulator(new Clickable(() =>
{
AbstractMaterialNode copyFromNode = graph.GetNodes<MasterNode>().First();
if (graphView.selection.Count == 1)
{
MaterialNodeView selectedNodeView = graphView.selection[0] as MaterialNodeView;
if (selectedNodeView.node != null && selectedNodeView.node.hasPreview)
{
copyFromNode = selectedNodeView.node;
}
}
var textureInfo = new List<PropertyCollector.TextureInfo>();
PreviewMode previewMode;
FloatShaderProperty outputIdProperty;
if (copyFromNode is MasterNode)
{
var shader = ((MasterNode)copyFromNode).GetShader(GenerationMode.ForReals, copyFromNode.name, out textureInfo);
GUIUtility.systemCopyBuffer = shader;
}
else
{
string shader = graph.GetShader(copyFromNode, GenerationMode.ForReals, assetName, out textureInfo, out previewMode, out outputIdProperty);
GUIUtility.systemCopyBuffer = shader;
}
}
));
m_ToolbarView.Add(m_CopyToClipboardButton);
m_ToolbarView.Add(new ToolbarSeparatorView());
}
Add(m_ToolbarView);

正在加载...
取消
保存