-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBehaviourTreeDebuggerWindow.cs
More file actions
768 lines (653 loc) · 25.8 KB
/
Copy pathBehaviourTreeDebuggerWindow.cs
File metadata and controls
768 lines (653 loc) · 25.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace GroveGames.BehaviourTree.Unity.Editor
{
public sealed class BehaviourTreeDebuggerWindow : EditorWindow
{
private const float LEFT_PANEL_WIDTH = 250f;
private const float NODE_WIDTH = 180f;
private const float NODE_HEIGHT = 40f;
private const float HORIZONTAL_SPACING = 60f;
private const float VERTICAL_SPACING = 20f;
private List<BehaviourTreeMono> _controllers = new();
private BehaviourTreeMono _selectedController;
private Vector2 _leftPanelScroll;
private Vector2 _treePanelScroll;
private GUIStyle _nodeStyleRunning;
private GUIStyle _nodeStyleSuccess;
private GUIStyle _nodeStyleFailure;
private GUIStyle _nodeStyleDefault;
private GUIStyle _selectedButtonStyle;
private bool _stylesInitialized;
[MenuItem("Tools/GroveGames/Behaviour Tree Debugger")]
public static void Open()
{
var window = GetWindow<BehaviourTreeDebuggerWindow>("BT Debugger");
window.minSize = new Vector2(800, 400);
}
private void OnEnable()
{
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
EditorApplication.hierarchyChanged += RefreshControllerList;
RefreshControllerList();
}
private void OnDisable()
{
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
EditorApplication.hierarchyChanged -= RefreshControllerList;
}
private void OnPlayModeStateChanged(PlayModeStateChange state)
{
RefreshControllerList();
Repaint();
}
private void RefreshControllerList()
{
_controllers.Clear();
var found = FindObjectsByType<BehaviourTreeMono>(FindObjectsSortMode.None);
_controllers.AddRange(found);
if (_selectedController != null && !_controllers.Contains(_selectedController))
{
_selectedController = null;
}
}
private void InitStyles()
{
if (_stylesInitialized)
{
return;
}
_nodeStyleRunning = CreateNodeStyle(new Color(0.9f, 0.75f, 0.2f));
_nodeStyleSuccess = CreateNodeStyle(new Color(0.3f, 0.8f, 0.3f));
_nodeStyleFailure = CreateNodeStyle(new Color(0.9f, 0.3f, 0.3f));
_nodeStyleDefault = CreateNodeStyle(new Color(0.4f, 0.4f, 0.4f));
_selectedButtonStyle = new GUIStyle(EditorStyles.miniButton)
{
normal = { background = CreateColorTexture(new Color(0.3f, 0.5f, 0.8f)) },
fontStyle = FontStyle.Bold
};
_stylesInitialized = true;
}
private GUIStyle CreateNodeStyle(Color bgColor)
{
var style = new GUIStyle(EditorStyles.helpBox)
{
alignment = TextAnchor.MiddleCenter,
fontStyle = FontStyle.Bold,
fontSize = 11,
normal =
{
background = CreateColorTexture(bgColor),
textColor = Color.white
},
border = new RectOffset(4, 4, 4, 4),
padding = new RectOffset(8, 8, 4, 4)
};
return style;
}
private Texture2D CreateColorTexture(Color color)
{
var texture = new Texture2D(1, 1);
texture.SetPixel(0, 0, color);
texture.Apply();
return texture;
}
private void OnGUI()
{
InitStyles();
EditorGUILayout.BeginHorizontal();
DrawLeftPanel();
DrawTreePanel();
EditorGUILayout.EndHorizontal();
if (Application.isPlaying)
{
Repaint();
}
}
private void DrawLeftPanel()
{
EditorGUILayout.BeginVertical(GUILayout.Width(LEFT_PANEL_WIDTH));
EditorGUILayout.LabelField("Behaviour Trees", EditorStyles.boldLabel);
EditorGUILayout.Space(5);
if (GUILayout.Button("Refresh", GUILayout.Height(25)))
{
RefreshControllerList();
}
EditorGUILayout.Space(10);
if (_controllers.Count == 0)
{
EditorGUILayout.HelpBox("No BehaviourTreeController found in scene.", MessageType.Info);
}
else
{
_leftPanelScroll = EditorGUILayout.BeginScrollView(_leftPanelScroll);
foreach (var controller in _controllers)
{
if (controller == null)
{
continue;
}
var isSelected = _selectedController == controller;
var style = isSelected ? _selectedButtonStyle : EditorStyles.miniButton;
var label = controller.gameObject.name;
if (GUILayout.Button(label, style, GUILayout.Height(28)))
{
_selectedController = controller;
Selection.activeGameObject = controller.gameObject;
}
}
EditorGUILayout.EndScrollView();
}
EditorGUILayout.EndVertical();
var rect = GUILayoutUtility.GetLastRect();
EditorGUI.DrawRect(new Rect(rect.xMax, rect.y, 1, rect.height), new Color(0.2f, 0.2f, 0.2f));
}
private void DrawTreePanel()
{
EditorGUILayout.BeginVertical();
if (_selectedController == null)
{
EditorGUILayout.HelpBox("Select a BehaviourTreeController from the list.", MessageType.Info);
EditorGUILayout.EndVertical();
return;
}
if (!Application.isPlaying)
{
EditorGUILayout.HelpBox("Enter Play Mode to see the behaviour tree visualization.", MessageType.Warning);
EditorGUILayout.EndVertical();
return;
}
var behaviourTree = _selectedController.Tree;
if (behaviourTree == null)
{
EditorGUILayout.HelpBox("BehaviourTree is not initialized.", MessageType.Warning);
EditorGUILayout.EndVertical();
return;
}
_treePanelScroll = EditorGUILayout.BeginScrollView(_treePanelScroll);
var treeData = ExtractTreeData(behaviourTree);
if (treeData != null)
{
DrawTreeVisualization(treeData);
}
else
{
EditorGUILayout.HelpBox("Could not extract tree data. Check if the BehaviourTree has a valid root.", MessageType.Error);
}
EditorGUILayout.EndScrollView();
EditorGUILayout.EndVertical();
}
private NodeData ExtractTreeData(object behaviourTree)
{
try
{
var rootProperty = behaviourTree.GetType().GetProperty("Root", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (rootProperty == null)
{
return null;
}
var root = rootProperty.GetValue(behaviourTree);
if (root == null)
{
return null;
}
return ExtractNodeData(root, 0);
}
catch (Exception e)
{
Debug.LogError($"Error extracting tree data: {e.Message}");
return null;
}
}
private NodeData ExtractNodeData(object node, int depth, NodeData parent = null)
{
if (node == null)
{
return null;
}
var nodeData = new NodeData
{
Name = GetNodeName(node),
State = GetNodeState(node),
Depth = depth,
Children = new List<NodeData>(),
Parent = parent
};
var children = GetNodeChildren(node);
if (children != null)
{
foreach (var child in children)
{
var childData = ExtractNodeData(child, depth + 1, nodeData);
if (childData != null)
{
nodeData.Children.Add(childData);
}
}
}
return nodeData;
}
private void CalculateActivePath(NodeData root)
{
// First, correct stale states (children of non-running parents should not show as running)
CorrectStaleStates(root);
// Reset all nodes
ResetActivePath(root);
// Find the deepest running or most recently evaluated node
var activeLeaf = FindActiveLeaf(root);
if (activeLeaf == null)
{
return;
}
// Mark path from active leaf to root
var current = activeLeaf;
while (current != null)
{
current.IsOnActivePath = true;
current = current.Parent;
}
// Calculate evaluation order
var order = 1;
CalculateEvaluationOrder(root, ref order);
}
private void CorrectStaleStates(NodeData node, bool parentIsActive = true)
{
// If parent is not active (Failure, None, or Success without Running children),
// then this node's Running state is stale
if (!parentIsActive && node.State == NodeState.Running)
{
node.State = NodeState.None;
}
// Determine if this node is "active" for its children
// A node is active if it's Running, or if it's the root
var isActiveForChildren = node.State == NodeState.Running || node.Parent == null;
// For Selector/Sequence that succeeded, only the path that led to success is valid
// Children showing Running when parent is Success are stale
if (node.State == NodeState.Success || node.State == NodeState.Failure || node.State == NodeState.None)
{
isActiveForChildren = false;
}
foreach (var child in node.Children)
{
CorrectStaleStates(child, isActiveForChildren);
}
}
private void ResetActivePath(NodeData node)
{
node.IsOnActivePath = false;
node.EvaluationOrder = 0;
foreach (var child in node.Children)
{
ResetActivePath(child);
}
}
private NodeData FindActiveLeaf(NodeData node)
{
// Only follow SUCCESS or RUNNING paths - FAILURE means "tried and failed, moved on"
if (node.State != NodeState.Success && node.State != NodeState.Running)
{
return null;
}
// If this node is RUNNING, it's the active leaf
if (node.State == NodeState.Running)
{
// But first check if any child is also running (deeper in the tree)
foreach (var child in node.Children)
{
var result = FindActiveLeaf(child);
if (result != null)
{
return result;
}
}
// No running child, this is the active leaf
return node;
}
// For SUCCESS nodes, check children for RUNNING or SUCCESS
foreach (var child in node.Children)
{
var result = FindActiveLeaf(child);
if (result != null)
{
return result;
}
}
// SUCCESS leaf node (no children or all children are None/Failure)
if (node.Children.Count == 0 || node.Children.TrueForAll(c => c.State == NodeState.None || c.State == NodeState.Failure))
{
return node;
}
return null;
}
private void CalculateEvaluationOrder(NodeData node, ref int order)
{
if (node.State != NodeState.None)
{
node.EvaluationOrder = order++;
}
foreach (var child in node.Children)
{
CalculateEvaluationOrder(child, ref order);
}
}
private string GetNodeName(object node)
{
var typeName = node.GetType().Name;
var nameProperty = node.GetType().GetProperty("Name", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (nameProperty != null)
{
var name = nameProperty.GetValue(node) as string;
if (!string.IsNullOrEmpty(name))
{
return name;
}
}
return typeName;
}
private NodeState GetNodeState(object node)
{
// Try property first
var stateProperty = FindPropertyInHierarchy(node.GetType(), "State");
if (stateProperty != null)
{
var state = stateProperty.GetValue(node);
return ParseNodeState(state);
}
// Try _nodeState field (used in GroveGames.BehaviourTree)
var nodeStateField = FindFieldInHierarchy(node.GetType(), "_nodeState");
if (nodeStateField != null)
{
var stateValue = nodeStateField.GetValue(node);
return ParseNodeState(stateValue);
}
// Try _state field
var stateField = FindFieldInHierarchy(node.GetType(), "_state");
if (stateField != null)
{
var stateValue = stateField.GetValue(node);
return ParseNodeState(stateValue);
}
return NodeState.None;
}
private NodeState ParseNodeState(object state)
{
if (state == null)
{
return NodeState.None;
}
var stateStr = state.ToString().ToLower();
return stateStr switch
{
"running" => NodeState.Running,
"success" => NodeState.Success,
"failure" => NodeState.Failure,
_ => NodeState.None
};
}
private IEnumerable<object> GetNodeChildren(object node)
{
// Try to find _children field (for Composite nodes like Selector, Sequence)
var childrenField = FindFieldInHierarchy(node.GetType(), "_children");
if (childrenField != null)
{
var children = childrenField.GetValue(node);
if (children is System.Collections.IEnumerable enumerable)
{
foreach (var child in enumerable)
{
if (IsValidNode(child))
{
yield return child;
}
}
yield break;
}
}
// Try to find Children property
var childrenProperty = FindPropertyInHierarchy(node.GetType(), "Children");
if (childrenProperty != null)
{
var children = childrenProperty.GetValue(node);
if (children is System.Collections.IEnumerable enumerable)
{
foreach (var child in enumerable)
{
if (IsValidNode(child))
{
yield return child;
}
}
yield break;
}
}
// Try to find _child field (for Decorator nodes like Conditional, Succeeder)
var childField = FindFieldInHierarchy(node.GetType(), "_child");
if (childField != null)
{
var child = childField.GetValue(node);
if (IsValidNode(child))
{
yield return child;
}
yield break;
}
// Try to find Child property
var childProperty = FindPropertyInHierarchy(node.GetType(), "Child");
if (childProperty != null)
{
var child = childProperty.GetValue(node);
if (IsValidNode(child))
{
yield return child;
}
}
}
private FieldInfo FindFieldInHierarchy(Type type, string fieldName)
{
var currentType = type;
while (currentType != null && currentType != typeof(object))
{
var field = currentType.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (field != null)
{
return field;
}
currentType = currentType.BaseType;
}
return null;
}
private PropertyInfo FindPropertyInHierarchy(Type type, string propertyName)
{
var currentType = type;
while (currentType != null && currentType != typeof(object))
{
var property = currentType.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (property != null)
{
return property;
}
currentType = currentType.BaseType;
}
return null;
}
private bool IsValidNode(object node)
{
if (node == null)
{
return false;
}
// Filter out BehaviourNode.Empty and similar empty/null object patterns
var typeName = node.GetType().Name;
if (typeName.Contains("Empty") || typeName.Contains("Null") || typeName == "EmptyNode")
{
return false;
}
return true;
}
private void DrawTreeVisualization(NodeData rootNode)
{
// Calculate active evaluation path
CalculateActivePath(rootNode);
var positions = new Dictionary<NodeData, Vector2>();
CalculateNodePositions(rootNode, positions, 0, 0);
float maxX = 0;
float maxY = 0;
foreach (var pos in positions.Values)
{
maxX = Mathf.Max(maxX, pos.x + NODE_WIDTH);
maxY = Mathf.Max(maxY, pos.y + NODE_HEIGHT);
}
// Calculate available space and center the tree vertically
var availableHeight = position.height - 100;
var treeHeight = maxY + 50;
var verticalOffset = Mathf.Max(20, (availableHeight - treeHeight) / 2);
var areaRect = GUILayoutUtility.GetRect(maxX + 50, Mathf.Max(maxY + 50, availableHeight));
var offsetRect = new Rect(areaRect.x, areaRect.y + verticalOffset, areaRect.width, areaRect.height);
GUI.BeginGroup(offsetRect);
// Draw connections - inactive first, then active on top
foreach (var kvp in positions)
{
var node = kvp.Key;
var pos = kvp.Value;
foreach (var child in node.Children)
{
if (positions.TryGetValue(child, out var childPos))
{
var isActivePath = node.IsOnActivePath && child.IsOnActivePath;
if (isActivePath) continue; // Draw active paths later
DrawConnection(pos, childPos, new Color(0.4f, 0.4f, 0.4f), 2f);
}
}
}
// Draw active path connections on top with glow effect
foreach (var kvp in positions)
{
var node = kvp.Key;
var pos = kvp.Value;
foreach (var child in node.Children)
{
if (positions.TryGetValue(child, out var childPos))
{
var isActivePath = node.IsOnActivePath && child.IsOnActivePath;
if (!isActivePath) continue;
// Glow effect - draw wider line behind
var glowColor = new Color(0.3f, 0.8f, 1f, 0.3f);
DrawConnection(pos, childPos, glowColor, 8f);
// Main active path line
var activeColor = new Color(0.3f, 0.8f, 1f);
DrawConnection(pos, childPos, activeColor, 3f);
}
}
}
// Draw nodes
foreach (var kvp in positions)
{
var node = kvp.Key;
var pos = kvp.Value;
var nodeRect = new Rect(pos.x, pos.y, NODE_WIDTH, NODE_HEIGHT);
var style = node.State switch
{
NodeState.Running => _nodeStyleRunning,
NodeState.Success => _nodeStyleSuccess,
NodeState.Failure => _nodeStyleFailure,
_ => _nodeStyleDefault
};
// Draw glow for active path nodes
if (node.IsOnActivePath && node.State != NodeState.None)
{
var glowRect = new Rect(nodeRect.x - 3, nodeRect.y - 3, nodeRect.width + 6, nodeRect.height + 6);
EditorGUI.DrawRect(glowRect, new Color(0.3f, 0.8f, 1f, 0.4f));
}
// Pulsing effect for Running nodes
if (node.State == NodeState.Running)
{
var pulse = Mathf.Sin((float)EditorApplication.timeSinceStartup * 4f) * 0.5f + 0.5f;
var pulseRect = new Rect(nodeRect.x - 2 - pulse * 3, nodeRect.y - 2 - pulse * 3,
nodeRect.width + 4 + pulse * 6, nodeRect.height + 4 + pulse * 6);
EditorGUI.DrawRect(pulseRect, new Color(1f, 0.9f, 0.2f, 0.3f * pulse));
}
GUI.Box(nodeRect, node.Name, style);
// State indicator
var indicatorRect = new Rect(nodeRect.x + 5, nodeRect.y + 5, 10, 10);
var indicatorColor = node.State switch
{
NodeState.Running => new Color(1f, 0.9f, 0.2f),
NodeState.Success => new Color(0.2f, 1f, 0.2f),
NodeState.Failure => new Color(1f, 0.2f, 0.2f),
_ => new Color(0.5f, 0.5f, 0.5f)
};
EditorGUI.DrawRect(indicatorRect, indicatorColor);
// Evaluation order number
if (node.EvaluationOrder > 0)
{
var orderRect = new Rect(nodeRect.xMax - 20, nodeRect.y + 5, 15, 15);
var orderStyle = new GUIStyle(EditorStyles.miniLabel)
{
alignment = TextAnchor.MiddleCenter,
normal = { textColor = Color.white },
fontStyle = FontStyle.Bold,
fontSize = 9
};
EditorGUI.DrawRect(orderRect, new Color(0.2f, 0.2f, 0.2f, 0.8f));
GUI.Label(orderRect, node.EvaluationOrder.ToString(), orderStyle);
}
}
GUI.EndGroup();
}
private void DrawConnection(Vector2 startPos, Vector2 endPos, Color color, float width)
{
var startPoint = new Vector3(startPos.x + NODE_WIDTH, startPos.y + NODE_HEIGHT / 2, 0);
var endPoint = new Vector3(endPos.x, endPos.y + NODE_HEIGHT / 2, 0);
var midX = (startPoint.x + endPoint.x) / 2;
Handles.BeginGUI();
Handles.color = color;
Handles.DrawAAPolyLine(width,
startPoint,
new Vector3(midX, startPoint.y, 0),
new Vector3(midX, endPoint.y, 0),
endPoint
);
Handles.EndGUI();
}
private float CalculateNodePositions(NodeData node, Dictionary<NodeData, Vector2> positions, int depth, float yOffset)
{
var x = 20 + depth * (NODE_WIDTH + HORIZONTAL_SPACING);
if (node.Children.Count == 0)
{
positions[node] = new Vector2(x, yOffset);
return yOffset + NODE_HEIGHT + VERTICAL_SPACING;
}
var startY = yOffset;
var currentY = yOffset;
foreach (var child in node.Children)
{
currentY = CalculateNodePositions(child, positions, depth + 1, currentY);
}
var firstChildY = positions[node.Children[0]].y;
var lastChildY = positions[node.Children[^1]].y;
var centerY = (firstChildY + lastChildY) / 2;
positions[node] = new Vector2(x, centerY);
return currentY;
}
private class NodeData
{
public string Name;
public NodeState State;
public int Depth;
public List<NodeData> Children;
public NodeData Parent;
public bool IsOnActivePath;
public int EvaluationOrder;
}
private enum NodeState
{
None,
Running,
Success,
Failure
}
}
}