Skip to content

Commit 862c7a3

Browse files
committed
Auto fill the graph list
1 parent aa56a4b commit 862c7a3

File tree

3 files changed

+67
-83
lines changed

3 files changed

+67
-83
lines changed

Assets/GraphVisualizer/Clients/GraphVisualizerClient.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public class GraphVisualizerClient
99
{
1010
private static GraphVisualizerClient s_Instance;
11-
private Dictionary<PlayableGraph, string> m_Graphs = new Dictionary<PlayableGraph, string>();
11+
private List<PlayableGraph> m_Graphs = new List<PlayableGraph>();
1212

1313
public static GraphVisualizerClient instance
1414
{
@@ -19,26 +19,29 @@ public static GraphVisualizerClient instance
1919
return s_Instance;
2020
}
2121
}
22+
2223
~GraphVisualizerClient()
2324
{
2425
m_Graphs.Clear();
2526
}
26-
public static void Show(PlayableGraph graph, string name)
27+
28+
public static void Show(PlayableGraph graph)
2729
{
28-
if (!instance.m_Graphs.ContainsKey(graph))
30+
if (!instance.m_Graphs.Contains(graph))
2931
{
30-
instance.m_Graphs.Add(graph, name);
32+
instance.m_Graphs.Add(graph);
3133
}
3234
}
35+
3336
public static void Hide(PlayableGraph graph)
3437
{
35-
if (instance.m_Graphs.ContainsKey(graph))
38+
if (instance.m_Graphs.Contains(graph))
3639
{
3740
instance.m_Graphs.Remove(graph);
3841
}
3942
}
4043

41-
public static IEnumerable<KeyValuePair<PlayableGraph, string>> GetGraphs()
44+
public static IEnumerable<PlayableGraph> GetGraphs()
4245
{
4346
return instance.m_Graphs;
4447
}

Assets/GraphVisualizer/Editor/PlayableGraphVisualizerWindow.cs

Lines changed: 56 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,28 @@
88

99
public class PlayableGraphVisualizerWindow : EditorWindow, IHasCustomMenu
1010
{
11-
private struct PlayableGraphInfo
12-
{
13-
public PlayableGraph graph;
14-
public string name;
15-
}
16-
1711
private IGraphRenderer m_Renderer;
1812
private IGraphLayout m_Layout;
1913

20-
private PlayableGraphInfo m_CurrentGraphInfo;
14+
private List<PlayableGraph> m_Graphs;
15+
private PlayableGraph m_CurrentGraph;
2116
private GraphSettings m_GraphSettings;
22-
private bool m_AutoScanScene = true;
2317

24-
#region Configuration
18+
#region Configuration
2519

2620
private static readonly float s_ToolbarHeight = 17f;
2721
private static readonly float s_DefaultMaximumNormalizedNodeSize = 0.8f;
2822
private static readonly float s_DefaultMaximumNodeSizeInPixels = 100.0f;
2923
private static readonly float s_DefaultAspectRatio = 1.5f;
3024

31-
#endregion
25+
#endregion
26+
3227
private PlayableGraphVisualizerWindow()
3328
{
3429
m_GraphSettings.maximumNormalizedNodeSize = s_DefaultMaximumNormalizedNodeSize;
3530
m_GraphSettings.maximumNodeSizeInPixels = s_DefaultMaximumNodeSizeInPixels;
3631
m_GraphSettings.aspectRatio = s_DefaultAspectRatio;
3732
m_GraphSettings.showLegend = true;
38-
m_AutoScanScene = true;
3933
}
4034

4135
[MenuItem("Window/PlayableGraph Visualizer")]
@@ -44,27 +38,28 @@ public static void ShowWindow()
4438
GetWindow<PlayableGraphVisualizerWindow>("PlayableGraph Visualizer");
4539
}
4640

47-
private PlayableGraphInfo GetSelectedGraphInToolBar(IList<PlayableGraphInfo> graphs, PlayableGraphInfo currentGraph)
41+
private PlayableGraph GetSelectedGraphInToolBar(List<PlayableGraph> graphs, PlayableGraph currentGraph)
4842
{
4943
EditorGUILayout.BeginHorizontal(EditorStyles.toolbar, GUILayout.Width(position.width));
5044

51-
List<string> options = new List<string>(graphs.Count);// = graphs.Select(d => d.ToString()).ToArray();
52-
foreach (var g in graphs)
45+
List<string> options = new List<string>(graphs.Count);
46+
foreach (var graph in graphs)
5347
{
54-
options.Add(g.name);
48+
string name = graph.GetEditorName();
49+
options.Add(name.Length != 0 ? name : "[Unnamed]");
5550
}
5651

5752
int currentSelection = graphs.IndexOf(currentGraph);
5853
int newSelection = EditorGUILayout.Popup(currentSelection != -1 ? currentSelection : 0, options.ToArray(), GUILayout.Width(200));
5954

60-
PlayableGraphInfo selectedDirector = new PlayableGraphInfo();
55+
PlayableGraph selectedGraph = new PlayableGraph();
6156
if (newSelection != -1)
62-
selectedDirector = graphs[newSelection];
57+
selectedGraph = graphs[newSelection];
6358

6459
GUILayout.FlexibleSpace();
6560
EditorGUILayout.EndHorizontal();
6661

67-
return selectedDirector;
62+
return selectedGraph;
6863
}
6964

7065
private static void ShowMessage(string msg)
@@ -98,78 +93,52 @@ void OnInspectorUpdate()
9893
Repaint();
9994
}
10095

101-
void OnGUI()
96+
void OnEnable()
10297
{
103-
// Create a list of all the playable graphs extracted.
104-
IList<PlayableGraphInfo> graphInfos = new List<PlayableGraphInfo>();
98+
m_Graphs = new List<PlayableGraph>(UnityEditor.Playables.Utility.GetAllGraphs());
10599

106-
PlayableGraphInfo info;
100+
UnityEditor.Playables.Utility.graphCreated += OnGraphCreated;
101+
UnityEditor.Playables.Utility.destroyingGraph += OnDestroyingGraph;
102+
}
107103

108-
// If we requested, we extract automatically the PlayableGraphs from all the components
109-
// that are in the current scene.
110-
if (m_AutoScanScene)
111-
{
112-
// This code could be generalized, maybe if we added a IHasPlayableGraph Interface.
113-
IList<PlayableDirector> directors = FindObjectsOfType<PlayableDirector>();
114-
if (directors != null)
115-
{
116-
foreach (var director in directors)
117-
{
118-
if (director.playableGraph.IsValid())
119-
{
120-
info.name = director.name;
121-
info.graph = director.playableGraph;
122-
graphInfos.Add(info);
123-
}
124-
}
125-
}
126-
127-
IList<Animator> animators = FindObjectsOfType<Animator>();
128-
if (animators != null)
129-
{
130-
foreach (var animator in animators)
131-
{
132-
if (animator.playableGraph.IsValid())
133-
{
134-
info.name = animator.name;
135-
info.graph = animator.playableGraph;
136-
graphInfos.Add(info);
137-
}
138-
}
139-
}
140-
}
104+
void OnGraphCreated(PlayableGraph graph)
105+
{
106+
if (!m_Graphs.Contains(graph))
107+
m_Graphs.Add(graph);
108+
}
141109

142-
if (GraphVisualizerClient.GetGraphs() != null)
143-
{
144-
foreach (var clientGraph in GraphVisualizerClient.GetGraphs())
145-
{
146-
if (clientGraph.Key.IsValid())
147-
{
148-
info.name = clientGraph.Value;
149-
info.graph = clientGraph.Key;
150-
graphInfos.Add(info);
151-
}
152-
}
153-
}
110+
void OnDestroyingGraph(PlayableGraph graph)
111+
{
112+
m_Graphs.Remove(graph);
113+
}
154114

115+
void OnDisable()
116+
{
117+
UnityEditor.Playables.Utility.graphCreated -= OnGraphCreated;
118+
UnityEditor.Playables.Utility.destroyingGraph -= OnDestroyingGraph;
119+
}
120+
121+
void OnGUI()
122+
{
155123
// Early out if there is no graphs.
156-
if (graphInfos.Count == 0)
124+
var selectedGraphs = GetGraphList();
125+
if (selectedGraphs.Count == 0)
157126
{
158127
ShowMessage("No PlayableGraph in the scene");
159128
return;
160129
}
161130

162131
GUILayout.BeginVertical();
163-
m_CurrentGraphInfo = GetSelectedGraphInToolBar(graphInfos, m_CurrentGraphInfo);
132+
m_CurrentGraph = GetSelectedGraphInToolBar(selectedGraphs, m_CurrentGraph);
164133
GUILayout.EndVertical();
165134

166-
if (!m_CurrentGraphInfo.graph.IsValid())
135+
if (!m_CurrentGraph.IsValid())
167136
{
168137
ShowMessage("Selected PlayableGraph is invalid");
169138
return;
170139
}
171140

172-
var graph = new PlayableGraphVisualizer(m_CurrentGraphInfo.graph);
141+
var graph = new PlayableGraphVisualizer(m_CurrentGraph);
173142
graph.Refresh();
174143

175144
if (graph.IsEmpty())
@@ -191,21 +160,32 @@ void OnGUI()
191160
m_Renderer.Draw(m_Layout, graphRect, m_GraphSettings);
192161
}
193162

163+
private List<PlayableGraph> GetGraphList()
164+
{
165+
var selectedGraphs = new List<PlayableGraph>();
166+
foreach (var clientGraph in GraphVisualizerClient.GetGraphs())
167+
{
168+
if (clientGraph.IsValid())
169+
selectedGraphs.Add(clientGraph);
170+
}
171+
172+
if (selectedGraphs.Count == 0)
173+
selectedGraphs = m_Graphs.ToList();
174+
175+
return selectedGraphs;
176+
}
177+
194178
#region Custom_Menu
195179

196180
public virtual void AddItemsToMenu(GenericMenu menu)
197181
{
198182
menu.AddItem(new GUIContent("Legend"), m_GraphSettings.showLegend, ToggleLegend);
199-
menu.AddItem(new GUIContent("Auto Scan Scene"), m_AutoScanScene, ToggleAutoScanScene);
200183
}
184+
201185
void ToggleLegend()
202186
{
203187
m_GraphSettings.showLegend = !m_GraphSettings.showLegend;
204188
}
205-
void ToggleAutoScanScene()
206-
{
207-
m_AutoScanScene = !m_AutoScanScene;
208-
}
209189

210190
#endregion
211191
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ Playable Handles in the graph are represented by colored nodes, varying accordin
1818
## Usage
1919

2020
- Open any scene that contains at least one `PlayableGraph`.
21-
- Register your `PlayableGraph` with the method `GraphVisualizerClient.Show(PlayableGraph, string)`.
21+
- By default, all the `PlayableGraph`s of your scene will be listed in the editor's top-left list.
22+
- You can show just your `PlayableGraph` using `GraphVisualizerClient.Show(PlayableGraph)`.
2223
- Select the `PlayableGraph` to display in the top-left list.
2324
- Click on a Node to display more information about the associated Playable Handle.
2425

0 commit comments

Comments
 (0)