Skip to content

Commit f1f0653

Browse files
committed
feat: color nodes by their type
1 parent 4a4a525 commit f1f0653

6 files changed

Lines changed: 119 additions & 44 deletions

File tree

Editor/Scripts/Node/PlayableNode.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ public override void Update()
2727

2828
if (!Playable.IsValid())
2929
{
30-
foreach (var port in InternalOutputPorts)
31-
{
32-
port.portColor = GraphTool.GetPortInvalidColor(0);
33-
}
30+
style.backgroundColor = GraphTool.GetNodeInvalidColor();
3431
return;
3532
}
3633

@@ -43,9 +40,9 @@ public override void Update()
4340
// diff child nodes
4441
for (int i = 0; i < Playable.GetInputCount(); i++)
4542
{
46-
// update port color
43+
// update self port color
4744
var inputWeight = Playable.GetInputWeight(i);
48-
InternalInputPorts[i].portColor = GraphTool.GetPortColor(Playable, inputWeight);
45+
InternalInputPorts[i].portColor = GraphTool.GetPortColor(inputWeight);
4946

5047
var inputPlayable = Playable.GetInput(i);
5148
if (!inputPlayable.IsValid())
@@ -89,9 +86,9 @@ public override void Update()
8986
continue;
9087
}
9188

92-
// update port color
89+
// update child port color
9390
var inputWeight = Playable.GetInputWeight(input.PortIndex);
94-
input.Node.OutputPorts[0].portColor = GraphTool.GetPortColor(Playable, inputWeight);
91+
input.Node.OutputPorts[0].portColor = GraphTool.GetPortColor(inputWeight);
9592
input.Edge.UpdateEdgeControl();
9693

9794
InternalInputs[i].Node.Update();
@@ -124,7 +121,7 @@ private void CreatePorts()
124121
{
125122
var inputPort = InstantiatePort<Playable>(Direction.Input);
126123
inputPort.portName = $"Input {i}";
127-
inputPort.portColor = GraphTool.GetPortColor(Playable, Playable.GetInputWeight(i));
124+
inputPort.portColor = GraphTool.GetPortColor(Playable.GetInputWeight(i));
128125
inputContainer.Add(inputPort);
129126
InternalInputPorts.Add(inputPort);
130127
}
@@ -133,7 +130,7 @@ private void CreatePorts()
133130
{
134131
var outputPort = InstantiatePort<Playable>(Direction.Output);
135132
outputPort.portName = $"Output {i}";
136-
outputPort.portColor = GraphTool.GetPortColor(Playable, 1);
133+
outputPort.portColor = GraphTool.GetPortColor(1);
137134
outputContainer.Add(outputPort);
138135
InternalOutputPorts.Add(outputPort);
139136
}

Editor/Scripts/Node/PlayableNodeFactory.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using UnityEngine.Animations;
1+
using GBG.PlayableGraphMonitor.Editor.Utility;
2+
using UnityEngine.Animations;
23
using UnityEngine.Playables;
34

45
namespace GBG.PlayableGraphMonitor.Editor.Node
@@ -41,8 +42,9 @@ public static PlayableNode CreateNode(Playable playable)
4142

4243
SET_NODE_TITLE:
4344
playableNode.title = nodeTitle;
45+
playableNode.SetNodeStyle(playable.GetPlayableNodeColor());
4446

4547
return playableNode;
4648
}
4749
}
48-
}
50+
}

Editor/Scripts/Node/PlayableOutputNode.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using GBG.PlayableGraphMonitor.Editor.Utility;
22
using UnityEditor.Experimental.GraphView;
3+
using UnityEngine;
34
using UnityEngine.Playables;
45

56
namespace GBG.PlayableGraphMonitor.Editor.Node
@@ -96,7 +97,7 @@ private void CreatePorts()
9697

9798
var inputPort = InstantiatePort<Playable>(Direction.Input);
9899
inputPort.portName = "Source";
99-
inputPort.portColor = GraphTool.GetPortColor(PlayableOutput);
100+
inputPort.portColor = Color.white;
100101
inputContainer.Add(inputPort);
101102
InternalInputPorts.Add(inputPort);
102103
}

Editor/Scripts/Node/PlayableOutputNodeFactory.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using UnityEditor.Playables;
1+
using GBG.PlayableGraphMonitor.Editor.Utility;
2+
using UnityEditor.Playables;
23
using UnityEngine.Animations;
34
using UnityEngine.Playables;
45

@@ -28,8 +29,10 @@ public static PlayableOutputNode CreateNode(PlayableOutput playableOutput)
2829
// default node
2930
var playableOutputNode = new PlayableOutputNode(playableOutput)
3031
{
31-
title = nodeTitle
32+
title = nodeTitle,
3233
};
34+
playableOutputNode.SetNodeStyle(playableOutput.GetPlayableOutputNodeColor());
35+
3336
return playableOutputNode;
3437
}
3538
}
Lines changed: 98 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,129 @@
1-
using UnityEngine;
1+
using GBG.PlayableGraphMonitor.Editor.Node;
2+
using System;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
using UnityEngine.Animations;
26
using UnityEngine.Playables;
7+
using UnityEngine.UIElements;
8+
using URandom = UnityEngine.Random;
39

410
namespace GBG.PlayableGraphMonitor.Editor.Utility
511
{
612
public static class GraphTool
713
{
8-
// Ensure edge is always visible.
9-
private const float _colorAlphaFactor = 1f / 9;
10-
11-
12-
public static Color GetPortColor(PlayableOutput playableOutput)
14+
public static bool IsEqual(ref PlayableGraph a, ref PlayableGraph b)
1315
{
14-
var portColor = Color.white;
15-
portColor.a = 1;
16+
if (!a.IsValid())
17+
{
18+
return !b.IsValid();
19+
}
1620

17-
var playableOutputType = playableOutput.GetPlayableOutputType();
18-
// todo set port color by playable output type
21+
if (!b.IsValid())
22+
{
23+
return false;
24+
}
25+
26+
var nameA = a.GetEditorName();
27+
var nameB = b.GetEditorName();
1928

20-
return portColor;
29+
return nameA.Equals(nameB);
2130
}
2231

23-
public static Color GetPortColor(Playable playable, float weight)
32+
33+
public static void SetNodeStyle(this GraphViewNode node, Color nodeColor,
34+
float titleFontSize = 15, Color? titleColor = null)
2435
{
25-
var portColor = Color.white;
26-
portColor.a = (weight + _colorAlphaFactor) / (1 + weight);
36+
// title
37+
var titleLabel = node.Q("title-label");
38+
titleLabel.style.fontSize = titleFontSize;
39+
titleLabel.style.color = titleColor ?? Color.black;
40+
var titlePanel = node.Q("title");
41+
titlePanel.style.backgroundColor = nodeColor;
42+
}
43+
44+
45+
// Ensure edge is always visible.
46+
public const float ColorAlphaFactor = 1f / 9;
2747

28-
var playableType = playable.GetPlayableType();
29-
// todo set port color by playable type
3048

31-
return portColor;
49+
public static Color GetPortColor(float weight)
50+
{
51+
var alpha = (weight + ColorAlphaFactor) / (1 + weight);
52+
return new Color(1, 1, 1, alpha);
53+
}
54+
55+
public static Color GetNodeInvalidColor()
56+
{
57+
return Color.red;
3258
}
3359

34-
public static Color GetPortInvalidColor(float weight)
60+
public static Color GetPlayableOutputNodeColor(this ref PlayableOutput playableOutput)
3561
{
36-
var portColor = Color.red;
37-
portColor.a = (weight + _colorAlphaFactor) / (1 + weight);
62+
if (playableOutput.IsPlayableOutputOfType<AnimationPlayableOutput>())
63+
{
64+
return new Color32(0, 255, 255, 255);
65+
}
66+
67+
if (playableOutput.IsPlayableOutputOfType<ScriptPlayableOutput>())
68+
{
69+
return new Color32(0, 204, 255, 255);
70+
}
3871

39-
return portColor;
72+
73+
return GetRandomColorForType(playableOutput.GetPlayableOutputType());
4074
}
4175

42-
public static bool IsEqual(ref PlayableGraph a, ref PlayableGraph b)
76+
public static Color GetPlayableNodeColor(this ref Playable playable)
4377
{
44-
if (!a.IsValid())
78+
if (playable.IsPlayableOfType<AnimationClipPlayable>())
4579
{
46-
return !b.IsValid();
80+
return new Color32(0, 255, 102, 255);
4781
}
4882

49-
if (!b.IsValid())
83+
if (playable.IsPlayableOfType<AnimationMixerPlayable>())
5084
{
51-
return false;
85+
return new Color32(0, 255, 153, 255);
5286
}
5387

54-
var nameA = a.GetEditorName();
55-
var nameB = b.GetEditorName();
88+
if (playable.IsPlayableOfType<AnimationLayerMixerPlayable>())
89+
{
90+
return new Color32(0, 255, 204, 255);
91+
}
5692

57-
return nameA.Equals(nameB);
93+
return GetRandomColorForType(playable.GetPlayableType());
94+
}
95+
96+
public static Color GetRandomColorForType(this Type type)
97+
{
98+
if (_colorCache.TryGetValue(type, out var color))
99+
{
100+
return color;
101+
}
102+
103+
color = ColorPool[URandom.Range(0, ColorPool.Count)];
104+
_colorCache[type] = color;
105+
106+
return color;
107+
}
108+
109+
110+
public static readonly IReadOnlyList<Color32> ColorPool = new Color32[]
111+
{
112+
new Color32(255,0,255,255), new Color32(255,0,153,255),
113+
new Color32(153,255,0,255), new Color32(204,255,0,255),
114+
new Color32(255,255,0,255), new Color32(255,204,0,255),
115+
new Color32(255,153,0,255), new Color32(255,102,0,255),
116+
new Color32(153,204,255,255), new Color32(153,255,102,255),
117+
new Color32(153,255,153,255), new Color32(153,255,204,255),
118+
new Color32(204,255,102,255), new Color32(204,255,255,255),
119+
};
120+
121+
private static readonly Dictionary<Type, Color32> _colorCache = new Dictionary<Type, Color32>();
122+
123+
124+
public static void ClearGlobalCache()
125+
{
126+
_colorCache.Clear();
58127
}
59128
}
60129
}

Editor/Scripts/Window/PlayableGraphMonitorWindow.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using GBG.PlayableGraphMonitor.Editor.GraphView;
2+
using GBG.PlayableGraphMonitor.Editor.Utility;
23
using System.Collections.Generic;
34
using UnityEditor;
45
using UnityEditor.UIElements;
@@ -62,6 +63,8 @@ private void OnDisable()
6263
{
6364
PlayableUtility.graphCreated -= OnGraphCreated;
6465
PlayableUtility.destroyingGraph -= OnDestroyingGraph;
66+
67+
GraphTool.ClearGlobalCache();
6568
}
6669

6770
private void Update()

0 commit comments

Comments
 (0)