54 lines
1.6 KiB
C#
54 lines
1.6 KiB
C#
using CanvasCoreExpand;
|
|
using NodeCanvas.BehaviourTrees;
|
|
using NodeCanvas.Framework;
|
|
using NodeCanvas.Tasks.Actions;
|
|
|
|
namespace Logic.Editor
|
|
{
|
|
public class GraphExporter: IGraphExporter
|
|
{
|
|
private uint _recordId;
|
|
|
|
public void GenerateAiBtNodeId(Graph graph)
|
|
{
|
|
if (graph == null) return;
|
|
|
|
_recordId = 1;
|
|
var root = graph.primeNode;
|
|
if (root == null) return;
|
|
|
|
// 递归遍历所有节点
|
|
TraverseBTNodes(root);
|
|
}
|
|
|
|
private void TraverseBTNodes(Node node)
|
|
{
|
|
if (node == null) return;
|
|
|
|
// 检查当前节点是否是BaseActionTask类型
|
|
var actionNode = node as ActionNode;
|
|
if (actionNode != null && actionNode.task is BaseActionTask baseTask)
|
|
{
|
|
baseTask.NodeId = _recordId++;
|
|
node.tag = $"<color=red>{baseTask.NodeId}</color>";
|
|
}
|
|
|
|
// 处理子树节点
|
|
var subTree = node as SubTree;
|
|
if (subTree != null && subTree.subGraph != null && subTree.subGraph.primeNode != null)
|
|
{
|
|
TraverseBTNodes(subTree.subGraph.primeNode);
|
|
}
|
|
|
|
// 遍历所有子节点
|
|
if (node.outConnections != null)
|
|
{
|
|
foreach (var connection in node.outConnections)
|
|
{
|
|
if (connection.targetNode == null) continue;
|
|
TraverseBTNodes(connection.targetNode);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |