70 lines
2.1 KiB
C#
70 lines
2.1 KiB
C#
using System.Collections.Generic;
|
|
using CanvasCoreExpand;
|
|
using NodeCanvas.BehaviourTrees;
|
|
using NodeCanvas.Framework;
|
|
using NodeCanvas.Tasks.Actions;
|
|
using UnityEditor;
|
|
|
|
|
|
namespace Logic.Editor
|
|
{
|
|
public class GraphExporter: IGraphExporter
|
|
{
|
|
private uint _recordId;
|
|
private HashSet<Node> _visitedNodes;
|
|
|
|
|
|
public void GenerateAiBtNodeId(Graph graph)
|
|
{
|
|
if (graph == null) return;
|
|
|
|
_recordId = 1;
|
|
_visitedNodes = new HashSet<Node>();
|
|
|
|
var root = graph.primeNode;
|
|
if (root == null) return;
|
|
|
|
// 标记主图为脏
|
|
EditorUtility.SetDirty(graph);
|
|
|
|
// 递归遍历所有节点
|
|
TraverseBTNodes(root);
|
|
|
|
// 保存所有修改的资源
|
|
AssetDatabase.SaveAssets();
|
|
}
|
|
|
|
private void TraverseBTNodes(Node node)
|
|
{
|
|
if (node == null) return;
|
|
|
|
// 检查当前节点是否是BaseActionTask类型
|
|
var actionNode = node as ActionNode;
|
|
if (actionNode != null && !_visitedNodes.Contains(actionNode) && actionNode.task is BaseActionTask baseTask)
|
|
{
|
|
_visitedNodes.Add(actionNode);
|
|
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)
|
|
{
|
|
// 标记子图为脏
|
|
EditorUtility.SetDirty(subTree.subGraph);
|
|
TraverseBTNodes(subTree.subGraph.primeNode);
|
|
}
|
|
|
|
// 遍历所有子节点
|
|
if (node.outConnections != null)
|
|
{
|
|
foreach (var connection in node.outConnections)
|
|
{
|
|
if (connection.targetNode == null) continue;
|
|
TraverseBTNodes(connection.targetNode);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |