diff --git a/com.unity.shadergraph/Editor/Drawing/SearchWindowProvider.cs b/com.unity.shadergraph/Editor/Drawing/SearchWindowProvider.cs index 9eeaeb35554..51969b4caeb 100644 --- a/com.unity.shadergraph/Editor/Drawing/SearchWindowProvider.cs +++ b/com.unity.shadergraph/Editor/Drawing/SearchWindowProvider.cs @@ -31,6 +31,7 @@ class SearchWindowProvider : ScriptableObject public bool nodeNeedsRepositioning { get; set; } public SlotReference targetSlotReference { get; internal set; } public Vector2 targetPosition { get; internal set; } + public bool regenerateEntries { get; set; } private const string k_HiddenFolderName = "Hidden"; public void Initialize(EditorWindow editorWindow, GraphData graph, GraphView graphView) @@ -54,30 +55,26 @@ void OnDestroy() m_Icon = null; } } - + List m_Ids; List m_Slots = new List(); public void GenerateNodeEntries() { // First build up temporary data structure containing group & title as an array of strings (the last one is the actual title) and associated node type. - List nodeEntries = new List(); - foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) + var nodeEntries = new List(); + foreach (var type in TypeCache.GetTypesDerivedFrom()) { - foreach (var type in assembly.GetTypesOrNothing()) + if ((!type.IsClass || type.IsAbstract) + || type == typeof(PropertyNode) + || type == typeof(KeywordNode) + || type == typeof(SubGraphNode)) + continue; + + if (type.GetCustomAttributes(typeof(TitleAttribute), false) is TitleAttribute[] attrs && attrs.Length > 0) { - if (type.IsClass && !type.IsAbstract && (type.IsSubclassOf(typeof(AbstractMaterialNode))) - && type != typeof(PropertyNode) - && type != typeof(KeywordNode) - && type != typeof(SubGraphNode)) - { - var attrs = type.GetCustomAttributes(typeof(TitleAttribute), false) as TitleAttribute[]; - if (attrs != null && attrs.Length > 0) - { - var node = (AbstractMaterialNode)Activator.CreateInstance(type); - AddEntries(node, attrs[0].title, nodeEntries); - } - } + var node = (AbstractMaterialNode) Activator.CreateInstance(type); + AddEntries(node, attrs[0].title, nodeEntries); } } @@ -86,7 +83,7 @@ public void GenerateNodeEntries() var asset = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid)); var node = new SubGraphNode { asset = asset }; var title = asset.path.Split('/').ToList(); - + if (asset.descendents.Contains(m_Graph.assetGuid) || asset.assetGuid == m_Graph.assetGuid) { continue; @@ -206,8 +203,11 @@ class SearcherProvider : SearchWindowProvider { public Searcher.Searcher LoadSearchWindow() { - GenerateNodeEntries(); - + if (regenerateEntries) + { + GenerateNodeEntries(); + regenerateEntries = false; + } //create empty root for searcher tree var root = new List(); var dummyEntry = new NodeEntry(); @@ -262,7 +262,7 @@ public bool OnSearcherSelectEntry(SearcherItem entry, Vector2 screenMousePositio return false; var nodeEntry = (entry as SearchNodeItem).NodeGUID; - var node = nodeEntry.node; + var node = CopyNodeForGraph(nodeEntry.node); var drawState = node.drawState; @@ -293,6 +293,27 @@ public bool OnSearcherSelectEntry(SearcherItem entry, Vector2 screenMousePositio return true; } + public AbstractMaterialNode CopyNodeForGraph(AbstractMaterialNode oldNode) + { + var newNode = (AbstractMaterialNode)Activator.CreateInstance(oldNode.GetType()); + if (newNode is SubGraphNode subgraphNode) + { + subgraphNode.asset = ((SubGraphNode)oldNode).asset; + } + else if(newNode is PropertyNode propertyNode) + { + propertyNode.owner = m_Graph; + propertyNode.propertyGuid = ((PropertyNode)oldNode).propertyGuid; + propertyNode.owner = null; + } + else if(newNode is KeywordNode keywordNode) + { + keywordNode.owner = m_Graph; + keywordNode.keywordGuid = ((KeywordNode)oldNode).keywordGuid; + keywordNode.owner = null; + } + return newNode; + } } } diff --git a/com.unity.shadergraph/Editor/Drawing/Views/GraphEditorView.cs b/com.unity.shadergraph/Editor/Drawing/Views/GraphEditorView.cs index 6cdd1b8dd0b..87fdfdca02f 100644 --- a/com.unity.shadergraph/Editor/Drawing/Views/GraphEditorView.cs +++ b/com.unity.shadergraph/Editor/Drawing/Views/GraphEditorView.cs @@ -261,6 +261,11 @@ public GraphEditorView(EditorWindow editorWindow, GraphData graph, MessageManage item => (m_SearchWindowProvider as SearcherProvider).OnSearcherSelectEntry(item, c.screenMousePosition - editorWindow.position.position), c.screenMousePosition - editorWindow.position.position, null); }; + m_GraphView.RegisterCallback( evt => + { + //regenerate entries when graph view is refocused, to propogate subgraph changes + m_SearchWindowProvider.regenerateEntries = true; + }); m_EdgeConnectorListener = new EdgeConnectorListener(m_Graph, m_SearchWindowProvider, editorWindow); @@ -562,6 +567,8 @@ public void HandleGraphChanges() } previewManager.RenderPreviews(); + if(m_Graph.addedInputs.Count() > 0 || m_Graph.removedInputs.Count() > 0) + m_SearchWindowProvider.regenerateEntries = true; m_BlackboardProvider.HandleGraphChanges(); m_GroupHashSet.Clear();