Skip to content

Commit 3f20575

Browse files
committed
Ported GraphVisualizer to new PlayableAPI
- Updated code to reflect new API - Added support for PlayableOutput
1 parent 7f1e0ab commit 3f20575

File tree

2 files changed

+130
-48
lines changed

2 files changed

+130
-48
lines changed

Assets/Editor/GraphVisualizer/PlayableGraphVisualizer.cs

Lines changed: 129 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,58 @@
44
using GraphVisualizer;
55
using UnityEngine.Playables;
66

7-
public class PlayableGraphNode : Node
7+
public class SharedPlayableNode : Node
88
{
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)
1043
: base(content, weight, active)
1144
{
1245
}
1346

1447
public override Type GetContentType()
1548
{
16-
IPlayable p = null;
49+
Playable p = Playable.Null;
1750
try
1851
{
19-
p = ((PlayableHandle)content).GetObject<IPlayable>();
52+
p = ((Playable)content);
2053
}
2154
catch
2255
{
2356
// Ignore.
2457
}
25-
return p == null ? null : p.GetType();
58+
return !p.IsValid() ? null : p.GetPlayableType();
2659
}
2760

2861
public override string GetContentTypeShortName()
@@ -39,48 +72,66 @@ public override string ToString()
3972

4073
sb.AppendLine(InfoString("Handle", GetContentTypeShortName()));
4174

42-
var h = (PlayableHandle)content;
75+
var h = (Playable)content;
4376

4477
sb.AppendLine(InfoString("IsValid", h.IsValid()));
4578

4679
if (h.IsValid())
4780
{
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()));
5588
// sb.AppendLine(InfoString("Animation", h.animatedProperties));
5689
}
5790

5891
return sb.ToString();
5992
}
93+
}
6094

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)
6299
{
63-
return String.Format(
64-
((Math.Abs(value) < 100000.0) ? "<b>{0}:</b> {1:#.###}" : "<b>{0}:</b> {1:E4}") , key, value);
65100
}
66101

67-
private static string InfoString(string key, int value)
102+
public override Type GetContentType()
68103
{
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();
70114
}
71115

72-
private static string InfoString(string key, object value)
116+
public override string GetContentTypeShortName()
73117
{
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;
75122
}
76123

77-
private static string RemoveFromEnd(string str, string suffix)
124+
public override string ToString()
78125
{
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();
84135
}
85136
}
86137

@@ -95,47 +146,81 @@ public PlayableGraphVisualizer(PlayableGraph playableGraph)
95146

96147
protected override void Populate()
97148
{
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++)
101154
{
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+
}
103160
}
104161
}
105162

106163
protected override IEnumerable<Node> GetChildren(Node node)
107164
{
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>();
110172
}
111173

112-
private List<Node> GetInputsNode(PlayableHandle h)
174+
private List<Node> GetInputsFromPlayableNode(Playable h)
113175
{
114176
var inputs = new List<Node>();
115-
for (int port = 0; port < h.inputCount; ++port)
177+
if (h.IsValid())
116178
{
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())
119200
{
120-
float weight = h.GetInputWeight(port);
121-
Node node = CreateNodeFromPlayableHandle(playableHandle, weight);
201+
Node node = CreateNodeFromPlayable(playable, 1);
122202
inputs.Add(node);
123203
}
124204
}
125205
return inputs;
126206
}
127207

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)
129214
{
130-
return new PlayableGraphNode(h, weight, h.playState == PlayState.Playing);
215+
return new PlayableOutputNode(h);
131216
}
132217

133-
private static bool HasValidOuputs(PlayableHandle h)
218+
private static bool HasValidOuputs(Playable h)
134219
{
135-
for (int port = 0; port < h.outputCount; ++port)
220+
for (int port = 0; port < h.GetOutputCount(); ++port)
136221
{
137-
PlayableHandle playableHandle = h.GetOutput(port);
138-
if (playableHandle.IsValid())
222+
Playable playable = h.GetOutput(port);
223+
if (playable.IsValid())
139224
{
140225
return true;
141226
}

Assets/Editor/GraphVisualizer/PlayableGraphVisualizerWindow.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ void OnGUI()
100100
// that are in the current scene.
101101
if (m_AutoScanScene)
102102
{
103-
#if PLAYABLE_DIRECTOR
104103
// This code could be generalized, maybe if we added a IHasPlayableGraph Interface.
105104
IList<PlayableDirector> directors = FindObjectsOfType<PlayableDirector>();
106105
if (directors != null)
@@ -115,8 +114,7 @@ void OnGUI()
115114
}
116115
}
117116
}
118-
#endif
119-
#if ANIMATOR_5_6
117+
120118
IList<Animator> animators = FindObjectsOfType<Animator>();
121119
if (animators != null)
122120
{
@@ -130,7 +128,6 @@ void OnGUI()
130128
}
131129
}
132130
}
133-
#endif
134131
}
135132

136133
if (GraphVisualizerClient.GetGraphs() != null)

0 commit comments

Comments
 (0)