8
8
9
9
public class PlayableGraphVisualizerWindow : EditorWindow , IHasCustomMenu
10
10
{
11
- private struct PlayableGraphInfo
12
- {
13
- public PlayableGraph graph ;
14
- public string name ;
15
- }
16
-
17
11
private IGraphRenderer m_Renderer ;
18
12
private IGraphLayout m_Layout ;
19
13
20
- private PlayableGraphInfo m_CurrentGraphInfo ;
14
+ private List < PlayableGraph > m_Graphs ;
15
+ private PlayableGraph m_CurrentGraph ;
21
16
private GraphSettings m_GraphSettings ;
22
- private bool m_AutoScanScene = true ;
23
17
24
- #region Configuration
18
+ #region Configuration
25
19
26
20
private static readonly float s_ToolbarHeight = 17f ;
27
21
private static readonly float s_DefaultMaximumNormalizedNodeSize = 0.8f ;
28
22
private static readonly float s_DefaultMaximumNodeSizeInPixels = 100.0f ;
29
23
private static readonly float s_DefaultAspectRatio = 1.5f ;
30
24
31
- #endregion
25
+ #endregion
26
+
32
27
private PlayableGraphVisualizerWindow ( )
33
28
{
34
29
m_GraphSettings . maximumNormalizedNodeSize = s_DefaultMaximumNormalizedNodeSize ;
35
30
m_GraphSettings . maximumNodeSizeInPixels = s_DefaultMaximumNodeSizeInPixels ;
36
31
m_GraphSettings . aspectRatio = s_DefaultAspectRatio ;
37
32
m_GraphSettings . showLegend = true ;
38
- m_AutoScanScene = true ;
39
33
}
40
34
41
35
[ MenuItem ( "Window/PlayableGraph Visualizer" ) ]
@@ -44,27 +38,28 @@ public static void ShowWindow()
44
38
GetWindow < PlayableGraphVisualizerWindow > ( "PlayableGraph Visualizer" ) ;
45
39
}
46
40
47
- private PlayableGraphInfo GetSelectedGraphInToolBar ( IList < PlayableGraphInfo > graphs , PlayableGraphInfo currentGraph )
41
+ private PlayableGraph GetSelectedGraphInToolBar ( List < PlayableGraph > graphs , PlayableGraph currentGraph )
48
42
{
49
43
EditorGUILayout . BeginHorizontal ( EditorStyles . toolbar , GUILayout . Width ( position . width ) ) ;
50
44
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 )
53
47
{
54
- options . Add ( g . name ) ;
48
+ string name = graph . GetEditorName ( ) ;
49
+ options . Add ( name . Length != 0 ? name : "[Unnamed]" ) ;
55
50
}
56
51
57
52
int currentSelection = graphs . IndexOf ( currentGraph ) ;
58
53
int newSelection = EditorGUILayout . Popup ( currentSelection != - 1 ? currentSelection : 0 , options . ToArray ( ) , GUILayout . Width ( 200 ) ) ;
59
54
60
- PlayableGraphInfo selectedDirector = new PlayableGraphInfo ( ) ;
55
+ PlayableGraph selectedGraph = new PlayableGraph ( ) ;
61
56
if ( newSelection != - 1 )
62
- selectedDirector = graphs [ newSelection ] ;
57
+ selectedGraph = graphs [ newSelection ] ;
63
58
64
59
GUILayout . FlexibleSpace ( ) ;
65
60
EditorGUILayout . EndHorizontal ( ) ;
66
61
67
- return selectedDirector ;
62
+ return selectedGraph ;
68
63
}
69
64
70
65
private static void ShowMessage ( string msg )
@@ -98,78 +93,52 @@ void OnInspectorUpdate()
98
93
Repaint ( ) ;
99
94
}
100
95
101
- void OnGUI ( )
96
+ void OnEnable ( )
102
97
{
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 ( ) ) ;
105
99
106
- PlayableGraphInfo info ;
100
+ UnityEditor . Playables . Utility . graphCreated += OnGraphCreated ;
101
+ UnityEditor . Playables . Utility . destroyingGraph += OnDestroyingGraph ;
102
+ }
107
103
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
+ }
141
109
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
+ }
154
114
115
+ void OnDisable ( )
116
+ {
117
+ UnityEditor . Playables . Utility . graphCreated -= OnGraphCreated ;
118
+ UnityEditor . Playables . Utility . destroyingGraph -= OnDestroyingGraph ;
119
+ }
120
+
121
+ void OnGUI ( )
122
+ {
155
123
// Early out if there is no graphs.
156
- if ( graphInfos . Count == 0 )
124
+ var selectedGraphs = GetGraphList ( ) ;
125
+ if ( selectedGraphs . Count == 0 )
157
126
{
158
127
ShowMessage ( "No PlayableGraph in the scene" ) ;
159
128
return ;
160
129
}
161
130
162
131
GUILayout . BeginVertical ( ) ;
163
- m_CurrentGraphInfo = GetSelectedGraphInToolBar ( graphInfos , m_CurrentGraphInfo ) ;
132
+ m_CurrentGraph = GetSelectedGraphInToolBar ( selectedGraphs , m_CurrentGraph ) ;
164
133
GUILayout . EndVertical ( ) ;
165
134
166
- if ( ! m_CurrentGraphInfo . graph . IsValid ( ) )
135
+ if ( ! m_CurrentGraph . IsValid ( ) )
167
136
{
168
137
ShowMessage ( "Selected PlayableGraph is invalid" ) ;
169
138
return ;
170
139
}
171
140
172
- var graph = new PlayableGraphVisualizer ( m_CurrentGraphInfo . graph ) ;
141
+ var graph = new PlayableGraphVisualizer ( m_CurrentGraph ) ;
173
142
graph . Refresh ( ) ;
174
143
175
144
if ( graph . IsEmpty ( ) )
@@ -191,21 +160,32 @@ void OnGUI()
191
160
m_Renderer . Draw ( m_Layout , graphRect , m_GraphSettings ) ;
192
161
}
193
162
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
+
194
178
#region Custom_Menu
195
179
196
180
public virtual void AddItemsToMenu ( GenericMenu menu )
197
181
{
198
182
menu . AddItem ( new GUIContent ( "Legend" ) , m_GraphSettings . showLegend , ToggleLegend ) ;
199
- menu . AddItem ( new GUIContent ( "Auto Scan Scene" ) , m_AutoScanScene , ToggleAutoScanScene ) ;
200
183
}
184
+
201
185
void ToggleLegend ( )
202
186
{
203
187
m_GraphSettings . showLegend = ! m_GraphSettings . showLegend ;
204
188
}
205
- void ToggleAutoScanScene ( )
206
- {
207
- m_AutoScanScene = ! m_AutoScanScene ;
208
- }
209
189
210
190
#endregion
211
191
}
0 commit comments