2
2
using System . Collections . Generic ;
3
3
using System . Text ;
4
4
using GraphVisualizer ;
5
- using UnityEngine . Experimental . Director ;
5
+ using UnityEngine . Playables ;
6
6
7
- public class PlayableGraphNode : Node
7
+ public class SharedPlayableNode : Node
8
8
{
9
- public PlayableGraphNode ( PlayableHandle content , float weight = 1 , bool active = false )
9
+ public SharedPlayableNode ( object content , float weight = 1 , bool active = false )
10
+ : base ( content , weight , active )
11
+ {
12
+ }
13
+
14
+ protected static string InfoString ( string key , double value )
15
+ {
16
+ return String . Format (
17
+ ( ( Math . Abs ( value ) < 100000.0 ) ? "<b>{0}:</b> {1:#.###}" : "<b>{0}:</b> {1:E4}" ) , key , value ) ;
18
+ }
19
+
20
+ protected static string InfoString ( string key , int value )
21
+ {
22
+ return String . Format ( "<b>{0}:</b> {1:D}" , key , value ) ;
23
+ }
24
+
25
+ protected static string InfoString ( string key , object value )
26
+ {
27
+ return "<b>" + key + ":</b> " + ( value ?? "(none)" ) ;
28
+ }
29
+
30
+ protected static string RemoveFromEnd ( string str , string suffix )
31
+ {
32
+ if ( str . EndsWith ( suffix ) )
33
+ {
34
+ return str . Substring ( 0 , str . Length - suffix . Length ) ;
35
+ }
36
+ return str ;
37
+ }
38
+ }
39
+
40
+ public class PlayableNode : SharedPlayableNode
41
+ {
42
+ public PlayableNode ( Playable content , float weight = 1 , bool active = false )
10
43
: base ( content , weight , active )
11
44
{
12
45
}
13
46
14
47
public override Type GetContentType ( )
15
48
{
16
- Playable p = null ;
49
+ Playable p = Playable . Null ;
17
50
try
18
51
{
19
- p = ( ( PlayableHandle ) content ) . GetObject < Playable > ( ) ;
52
+ p = ( ( Playable ) content ) ;
20
53
}
21
54
catch
22
55
{
23
56
// Ignore.
24
57
}
25
- return p == null ? null : p . GetType ( ) ;
58
+ return ! p . IsValid ( ) ? null : p . GetPlayableType ( ) ;
26
59
}
27
60
28
61
public override string GetContentTypeShortName ( )
@@ -39,48 +72,66 @@ public override string ToString()
39
72
40
73
sb . AppendLine ( InfoString ( "Handle" , GetContentTypeShortName ( ) ) ) ;
41
74
42
- var h = ( PlayableHandle ) content ;
75
+ var h = ( Playable ) content ;
43
76
44
77
sb . AppendLine ( InfoString ( "IsValid" , h . IsValid ( ) ) ) ;
45
78
46
79
if ( h . IsValid ( ) )
47
80
{
48
- sb . AppendLine ( InfoString ( "IsDone" , h . isDone ) ) ;
49
- sb . AppendLine ( InfoString ( "InputCount" , h . inputCount ) ) ;
50
- sb . AppendLine ( InfoString ( "OutputCount" , h . outputCount ) ) ;
51
- sb . AppendLine ( InfoString ( "PlayState" , h . playState ) ) ;
52
- sb . AppendLine ( InfoString ( "Speed" , h . speed ) ) ;
53
- sb . AppendLine ( InfoString ( "Duration" , h . duration ) ) ;
54
- sb . AppendLine ( InfoString ( "Time" , h . time ) ) ;
81
+ sb . AppendLine ( InfoString ( "IsDone" , h . IsDone ( ) ) ) ;
82
+ sb . AppendLine ( InfoString ( "InputCount" , h . GetInputCount ( ) ) ) ;
83
+ sb . AppendLine ( InfoString ( "OutputCount" , h . GetOutputCount ( ) ) ) ;
84
+ sb . AppendLine ( InfoString ( "PlayState" , h . GetPlayState ( ) ) ) ;
85
+ sb . AppendLine ( InfoString ( "Speed" , h . GetSpeed ( ) ) ) ;
86
+ sb . AppendLine ( InfoString ( "Duration" , h . GetDuration ( ) ) ) ;
87
+ sb . AppendLine ( InfoString ( "Time" , h . GetTime ( ) ) ) ;
55
88
// sb.AppendLine(InfoString("Animation", h.animatedProperties));
56
89
}
57
90
58
91
return sb . ToString ( ) ;
59
92
}
93
+ }
60
94
61
- private static string InfoString ( string key , double value )
95
+ public class PlayableOutputNode : SharedPlayableNode
96
+ {
97
+ public PlayableOutputNode ( PlayableOutput content )
98
+ : base ( content , content . GetWeight ( ) , true )
62
99
{
63
- return String . Format (
64
- ( ( Math . Abs ( value ) < 100000.0 ) ? "<b>{0}:</b> {1:#.###}" : "<b>{0}:</b> {1:E4}" ) , key , value ) ;
65
100
}
66
101
67
- private static string InfoString ( string key , int value )
102
+ public override Type GetContentType ( )
68
103
{
69
- return String . Format ( "<b>{0}:</b> {1:D}" , key , value ) ;
104
+ PlayableOutput p = PlayableOutput . Null ;
105
+ try
106
+ {
107
+ p = ( ( PlayableOutput ) content ) ;
108
+ }
109
+ catch
110
+ {
111
+ // Ignore.
112
+ }
113
+ return ! p . IsOutputValid ( ) ? null : p . GetPlayableOutputType ( ) ;
70
114
}
71
115
72
- private static string InfoString ( string key , object value )
116
+ public override string GetContentTypeShortName ( )
73
117
{
74
- return "<b>" + key + ":</b> " + ( value ?? "(none)" ) ;
118
+ // Remove the extra Playable at the end of the Playable types.
119
+ string shortName = base . GetContentTypeShortName ( ) ;
120
+ string cleanName = RemoveFromEnd ( shortName , "PlayableOutput" ) + "Output" ;
121
+ return string . IsNullOrEmpty ( cleanName ) ? shortName : cleanName ;
75
122
}
76
123
77
- private static string RemoveFromEnd ( string str , string suffix )
124
+ public override string ToString ( )
78
125
{
79
- if ( str . EndsWith ( suffix ) )
80
- {
81
- return str . Substring ( 0 , str . Length - suffix . Length ) ;
82
- }
83
- return str ;
126
+ var sb = new StringBuilder ( ) ;
127
+
128
+ sb . AppendLine ( InfoString ( "Handle" , GetContentTypeShortName ( ) ) ) ;
129
+
130
+ var h = ( PlayableOutput ) content ;
131
+
132
+ sb . AppendLine ( InfoString ( "IsValid" , h . IsOutputValid ( ) ) ) ;
133
+
134
+ return sb . ToString ( ) ;
84
135
}
85
136
}
86
137
@@ -95,47 +146,81 @@ public PlayableGraphVisualizer(PlayableGraph playableGraph)
95
146
96
147
protected override void Populate ( )
97
148
{
98
- if ( ! m_PlayableGraph . IsValid ( ) ) return ;
99
- int roots = m_PlayableGraph . rootPlayableCount ;
100
- for ( int i = 0 ; i < roots ; i ++ )
149
+ if ( ! m_PlayableGraph . IsValid ( ) )
150
+ return ;
151
+
152
+ int outputs = m_PlayableGraph . GetOutputCount ( ) ;
153
+ for ( int i = 0 ; i < outputs ; i ++ )
101
154
{
102
- AddNodeHierarchy ( CreateNodeFromPlayableHandle ( m_PlayableGraph . GetRootPlayable ( i ) , 1.0f ) ) ;
155
+ var output = m_PlayableGraph . GetOutput ( i ) ;
156
+ if ( output . IsOutputValid ( ) )
157
+ {
158
+ AddNodeHierarchy ( CreateNodeFromPlayableOutput ( output ) ) ;
159
+ }
103
160
}
104
161
}
105
162
106
163
protected override IEnumerable < Node > GetChildren ( Node node )
107
164
{
108
- // Children are the PlayableHandle Inputs.
109
- return GetInputsNode ( ( PlayableHandle ) node . content ) ;
165
+ // Children are the Playable Inputs.
166
+ if ( node is PlayableNode )
167
+ return GetInputsFromPlayableNode ( ( Playable ) node . content ) ;
168
+ else if ( node is PlayableOutputNode )
169
+ return GetInputsFromPlayableOutputNode ( ( PlayableOutput ) node . content ) ;
170
+
171
+ return new List < Node > ( ) ;
110
172
}
111
173
112
- private List < Node > GetInputsNode ( PlayableHandle h )
174
+ private List < Node > GetInputsFromPlayableNode ( Playable h )
113
175
{
114
176
var inputs = new List < Node > ( ) ;
115
- for ( int port = 0 ; port < h . inputCount ; ++ port )
177
+ if ( h . IsValid ( ) )
116
178
{
117
- PlayableHandle playableHandle = h . GetInput ( port ) ;
118
- if ( playableHandle . IsValid ( ) )
179
+ for ( int port = 0 ; port < h . GetInputCount ( ) ; ++ port )
180
+ {
181
+ Playable playable = h . GetInput ( port ) ;
182
+ if ( playable . IsValid ( ) )
183
+ {
184
+ float weight = h . GetInputWeight ( port ) ;
185
+ Node node = CreateNodeFromPlayable ( playable , weight ) ;
186
+ inputs . Add ( node ) ;
187
+ }
188
+ }
189
+ }
190
+ return inputs ;
191
+ }
192
+
193
+ private List < Node > GetInputsFromPlayableOutputNode ( PlayableOutput h )
194
+ {
195
+ var inputs = new List < Node > ( ) ;
196
+ if ( h . IsOutputValid ( ) )
197
+ {
198
+ Playable playable = h . GetSourcePlayable ( ) ;
199
+ if ( playable . IsValid ( ) )
119
200
{
120
- float weight = h . GetInputWeight ( port ) ;
121
- Node node = CreateNodeFromPlayableHandle ( playableHandle , weight ) ;
201
+ Node node = CreateNodeFromPlayable ( playable , 1 ) ;
122
202
inputs . Add ( node ) ;
123
203
}
124
204
}
125
205
return inputs ;
126
206
}
127
207
128
- private PlayableGraphNode CreateNodeFromPlayableHandle ( PlayableHandle h , float weight )
208
+ private PlayableNode CreateNodeFromPlayable ( Playable h , float weight )
209
+ {
210
+ return new PlayableNode ( h , weight , h . GetPlayState ( ) == PlayState . Playing ) ;
211
+ }
212
+
213
+ private PlayableOutputNode CreateNodeFromPlayableOutput ( PlayableOutput h )
129
214
{
130
- return new PlayableGraphNode ( h , weight , h . playState == PlayState . Playing ) ;
215
+ return new PlayableOutputNode ( h ) ;
131
216
}
132
217
133
- private static bool HasValidOuputs ( PlayableHandle h )
218
+ private static bool HasValidOuputs ( Playable h )
134
219
{
135
- for ( int port = 0 ; port < h . outputCount ; ++ port )
220
+ for ( int port = 0 ; port < h . GetOutputCount ( ) ; ++ port )
136
221
{
137
- PlayableHandle playableHandle = h . GetOutput ( port ) ;
138
- if ( playableHandle . IsValid ( ) )
222
+ Playable playable = h . GetOutput ( port ) ;
223
+ if ( playable . IsValid ( ) )
139
224
{
140
225
return true ;
141
226
}
0 commit comments