Skip to content

Commit b28a83f

Browse files
committed
Interactivity: fixed Tick Nodes cleanup (wrong sockets after cleanup)
1 parent f8cf257 commit b28a83f

4 files changed

Lines changed: 116 additions & 66 deletions

File tree

Editor/Scripts/Interactivity/VisualScriptingExport/CleanUp/TickNodeCleanUp.cs

Lines changed: 54 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -23,103 +23,91 @@ public void OnCleanUp(CleanUpTask task)
2323
if (tickNodes.Length <= 1)
2424
return;
2525

26+
GltfInteractivityUnitExporterNode firstDeltaTimeNode = null;
27+
GltfInteractivityUnitExporterNode firstTimeSinceStartNode = null;
2628

2729
// Ensure the first OnTickNode is from a TimeUnitExports, so we have access to additional helper nodes, like isNaN check
2830
for (int i = 0; i < tickNodes.Length; i++)
2931
{
3032
if (tickNodes[i] is GltfInteractivityUnitExporterNode exporterNode)
3133
{
32-
if (exporterNode.Exporter.exporter is TimeUnitExports)
34+
if (exporterNode.Exporter.exporter is TimeUnitExports timeUnitExport)
3335
{
34-
var tmp = tickNodes[i];
35-
tickNodes[i] = tickNodes[0];
36-
tickNodes[0] = tmp;
37-
break;
36+
if (timeUnitExport.ValueOption == TimeHelpers.GetTimeValueOption.DeltaTime
37+
&& firstDeltaTimeNode == null)
38+
firstDeltaTimeNode = exporterNode;
39+
if (timeUnitExport.ValueOption == TimeHelpers.GetTimeValueOption.TimeSinceStartup
40+
&& firstTimeSinceStartNode == null)
41+
firstTimeSinceStartNode = exporterNode;
42+
3843
}
3944
}
40-
4145
}
42-
var firstTickNode = tickNodes[0];
43-
44-
var firstTickNodeFlowOut = firstTickNode.FlowConnections[Event_OnTickNode.IdFlowOut];
45-
46-
GltfInteractivityNode firstTickNodeSelectNode = null;
4746

48-
if (firstTickNode is GltfInteractivityUnitExporterNode firstTickNodeExport)
47+
GltfInteractivityExportNode firstDeltaTimeSelectNode = null;
48+
GltfInteractivityExportNode firstTimeSinceStartSelectNode = null;
49+
if (firstDeltaTimeNode != null)
4950
{
50-
var selectNode = firstTickNodeExport.Exporter.Nodes.FirstOrDefault(n => n.Schema is Math_SelectNode);
51-
if (selectNode != null)
52-
{
53-
firstTickNodeSelectNode = selectNode;
54-
}
51+
firstDeltaTimeSelectNode = firstDeltaTimeNode.Exporter.Nodes.FirstOrDefault(n => n.Schema is Math_SelectNode);
5552
}
56-
57-
for (int i = 1; i < tickNodes.Length; i++)
53+
if (firstTimeSinceStartNode != null)
54+
{
55+
firstTimeSinceStartSelectNode = firstTimeSinceStartNode.Exporter.Nodes.FirstOrDefault(n => n.Schema is Math_SelectNode);
56+
}
57+
58+
for (int i = 0; i < tickNodes.Length; i++)
5859
{
60+
if (tickNodes[i] == firstDeltaTimeNode || tickNodes[i] == firstTimeSinceStartNode)
61+
continue; // skip the first tick node, which is used for delta time or time since startup
62+
63+
TimeHelpers.GetTimeValueOption timeValue = TimeHelpers.GetTimeValueOption.DeltaTime;
64+
5965
var tickNode = tickNodes[i];
60-
bool dontDelete = false;
61-
var flowOut = tickNode.FlowConnections[Event_OnTickNode.IdFlowOut];
6266
GltfInteractivityNode tickNodeSelectNode = null;
63-
64-
if (tickNode is GltfInteractivityUnitExporterNode tickNodeExport)
65-
{
66-
if (tickNodeExport.Exporter.exporter is not TimeUnitExports)
67-
continue;
68-
var selectNode = tickNodeExport.Exporter.Nodes.FirstOrDefault(n => n.Schema is Math_SelectNode);
69-
if (selectNode != null)
70-
{
71-
tickNodeSelectNode = selectNode;
72-
}
73-
}
74-
75-
if (flowOut.Node != null && flowOut.Node.Value != -1)
76-
{
77-
if (firstTickNodeFlowOut.Node == null || firstTickNodeFlowOut.Node.Value == -1)
78-
{
79-
firstTickNodeFlowOut.Node = flowOut.Node;
80-
firstTickNodeFlowOut.Socket = flowOut.Socket;
81-
}
82-
else
83-
{
84-
dontDelete = true;
85-
}
86-
}
87-
67+
68+
if (tickNode is not GltfInteractivityUnitExporterNode tickNodeExport)
69+
continue;
70+
71+
if (tickNodeExport.Exporter.exporter is not TimeUnitExports timeUnitExport)
72+
continue;
73+
74+
timeValue = timeUnitExport.ValueOption;
75+
76+
var selectNode = tickNodeExport.Exporter.Nodes.FirstOrDefault(n => n.Schema is Math_SelectNode);
77+
if (selectNode == null)
78+
continue;
79+
80+
tickNodeSelectNode = selectNode;
81+
8882
foreach (var node in nodes)
8983
{
9084
foreach (var socket in node.ValueInConnection)
9185
{
92-
if (tickNodeSelectNode != null && firstTickNodeSelectNode != null)
86+
if (timeValue == TimeHelpers.GetTimeValueOption.DeltaTime && firstDeltaTimeNode != null && firstDeltaTimeSelectNode != null)
9387
{
9488
if (socket.Value.Node != null && socket.Value.Node.Value == tickNodeSelectNode.Index)
9589
{
96-
socket.Value.Node = firstTickNodeSelectNode.Index;
90+
socket.Value.Node = firstDeltaTimeSelectNode.Index;
9791
}
98-
9992
}
100-
else
101-
if (socket.Value.Node != null && socket.Value.Node.Value == tickNode.Index)
93+
94+
if (timeValue == TimeHelpers.GetTimeValueOption.TimeSinceStartup && firstTimeSinceStartNode != null && firstTimeSinceStartSelectNode != null)
10295
{
103-
socket.Value.Node = firstTickNode.Index;
96+
if (socket.Value.Node != null && socket.Value.Node.Value == tickNodeSelectNode.Index)
97+
{
98+
socket.Value.Node = firstTimeSinceStartSelectNode.Index;
99+
}
104100
}
105101
}
106102
}
107103

108-
if (!dontDelete)
109-
{
110-
if (tickNodeSelectNode != null && firstTickNodeSelectNode != null && tickNode is GltfInteractivityUnitExporterNode tickNodeExport2)
111-
{
112-
// also remove isNaN check
113-
var exporterNode = tickNodeExport2.Exporter.Nodes;
114-
foreach (var n in exporterNode)
115-
n.ValueInConnection.Clear();
104+
// also remove isNaN check
105+
var exporterNode = tickNodeExport.Exporter.Nodes;
106+
foreach (var n in exporterNode)
107+
n.ValueInConnection.Clear();
116108

117-
foreach (var n in exporterNode)
118-
task.RemoveNode(n);
119-
}
120-
else
121-
task.RemoveNode(tickNode);
122-
}
109+
foreach (var n in exporterNode)
110+
task.RemoveNode(n);
123111
}
124112
}
125113
}

Editor/Scripts/Interactivity/VisualScriptingExport/UnitExporters/Misc/TimeUnitExports.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class TimeUnitExports : IUnitExporter
1212
public Type unitType { get; }
1313
private TimeHelpers.GetTimeValueOption _valueOption;
1414

15+
public TimeHelpers.GetTimeValueOption ValueOption => _valueOption;
16+
1517

1618
[InitializeOnLoadMethod]
1719
private static void Register()
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Linq;
3+
using UnityGLTF.Interactivity.Schema;
4+
5+
namespace UnityGLTF.Interactivity.Export
6+
{
7+
public class TickNodeDeduplicationCleanUp : ICleanUp
8+
{
9+
#if UNITY_EDITOR
10+
[UnityEditor.InitializeOnLoadMethod]
11+
#else
12+
[RuntimeInitializeOnLoadMethod]
13+
#endif
14+
private static void Register()
15+
{
16+
CleanUpRegistry.RegisterCleanUp(new TickNodeDeduplicationCleanUp());
17+
}
18+
19+
public void OnCleanUp(CleanUpTask task)
20+
{
21+
var nodes = task.context.Nodes;
22+
var tickNodes = nodes.FindAll(node => node.Schema is Event_OnTickNode).ToArray();
23+
24+
25+
var firstTickNodeWithFlow = tickNodes.FirstOrDefault( n => n.FlowOut().socket.Value.Node != null);
26+
if (firstTickNodeWithFlow != null)
27+
{
28+
var index = Array.IndexOf(tickNodes, firstTickNodeWithFlow);
29+
if (index > 0)
30+
{
31+
// Move the first tick node to the first position
32+
var temp = tickNodes[0];
33+
tickNodes[0] = tickNodes[index];
34+
tickNodes[index] = temp;
35+
}
36+
}
37+
38+
for (int i = 1; i < tickNodes.Length; i++)
39+
{
40+
if (tickNodes[i].FlowOut().socket.Value.Node != null)
41+
continue;
42+
43+
foreach (var node in nodes)
44+
{
45+
foreach (var valueSocket in node.ValueInConnection)
46+
{
47+
if (valueSocket.Value.Node != null && valueSocket.Value.Node == tickNodes[i].Index)
48+
valueSocket.Value.Node = tickNodes[0].Index;
49+
}
50+
}
51+
52+
task.RemoveNode(tickNodes[i]);
53+
54+
}
55+
}
56+
}
57+
}

Runtime/Scripts/Interactivity/Export/CleanUp/TickNodeDeduplicationCleanUp.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)